使用zxing

时间:2015-08-04 23:05:57

标签: java android zxing decoding datamatrix


经过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;
    }

使用DataMatrixReader替换QRCodeReader并使用qr代码尝试或使用MultiFormatReader进行尝试时,这甚至不起作用。
我尝试处理的每个图像都被zxing条形码扫描器应用程序正确解码,因此问题在于代码。

如果有人能告诉我这是如何工作的,我会很高兴,因为我相信在此之后我是创造性地诅咒的世界冠军 - java ^^

奔奔

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

1 个答案:

答案 0 :(得分: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;
    }

}