1


经过10多个小时的搜索和尝试,我终于决定在这里问。我正在使用该android.hardware.camera2库从设备摄像头获取图像。现在我想使用 zxing 库自动处理位图并解码数据矩阵代码(如果图片上有任何代码)。有一个计时器每秒处理图像五次,一切正常,但它无法识别任何数据矩阵代码。到目前为止,我有以下代码:

public String readDataMatrix(Bitmap bitmap) {
        int width = bitmap.getWidth(),
                height = bitmap.getHeight();

        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);

        BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
        DataMatrixReader reader = new DataMatrixReader();

        Result rawResult = null;

        try {
            rawResult = reader.decode(bBitmap);
            String result = reader.decode(bBitmap).getText();
            return result;
        } catch (NotFoundException | ChecksumException | FormatException e) {
            e.printStackTrace();
        }

        if (rawResult != null) {
            Log.i(TAG, "==============================================");
                Log.i(TAG, rawResult.getText());
            Log.i(TAG, "==============================================");
        }

        return rawResult != null ? rawResult.getText() : null;
    }

这甚至在用 qr-code 替换DataMatrixReaderQRCodeReader尝试它或用MultiFormatReader.
我尝试处理的每张图像都被 zxing 条码扫描仪应用程序正确解码,所以问题出在代码中。

如果有人能告诉我这是如何工作的,我会非常高兴,因为我相信在此之后我是 creatively-cursing-java 的世界冠军 ^^

Benni

PS:我在每个关于 zxing 的线程中都尝试了每一个解决方案,所以这个真的是我最后的选择。

4

1 回答 1

1
public String readDataMatrix(Bitmap bitmap) {
        int width = bitmap.getWidth();
        height = bitmap.getHeight();
        byte[] data = bitmap.getRowBytes();

        Result rawResult = null;
        Log.e("C2", data.length + " (" + width + "x" + height + ")");
        PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = mQrReader.decode(bitmap);
            onQRCodeRead(rawResult.getText());
        } catch (ReaderException ignored) {
            /* Ignored */
        } finally {
            mQrReader.reset();
        }

        Result rawResult = null;

        if (rawResult != null) {
            Log.i(TAG, "==============================================");
                Log.i(TAG, rawResult.getText());
            Log.i(TAG, "==============================================");
        } 

        return rawResult != null ? rawResult.getText() : null;
    }

这对我有用,使用自定义 PlanarYUVLuminanceSource :

    final public class PlanarYUVLuminanceSource extends LuminanceSource {

    private final byte[] mYuvData;

    public PlanarYUVLuminanceSource(byte[] yuvData, int width, int height) {
        super(width, height);

        mYuvData = yuvData;
    }

    @Override
    public byte[] getRow(int y, byte[] row) {
        if (y < 0 || y >= getHeight()) {
            throw new IllegalArgumentException("Requested row is outside the image: " + y);
        }
        final int width = getWidth();
        if (row == null || row.length < width) {
            row = new byte[width];
        }
        final int offset = y * width;
        System.arraycopy(mYuvData, offset, row, 0, width);
        return row;
    }

    @Override
    public byte[] getMatrix() {
        return mYuvData;
    }

    @Override
    public boolean isCropSupported() {
        return true;
    }

}
于 2015-10-23T20:34:04.863 回答