1


after more than 10 hours of searching and trying I finally decided to ask here. I am using the android.hardware.camera2 library to get the image from the device camera. Now I want to automatically process the bitmap and decode a datamatrix code if there is any on the picture with the zxing library. There is a timer processing the image five times a second, and everything works well, but it doesn't recognize any datamatrix code. Until now i have the following code:

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

This even does not work when replacing DataMatrixReader with QRCodeReader and trying it with a qr-code or trying it with MultiFormatReader.
Every image I try to process is being decoded properly by the zxing barcode scanner app, so the problem is in the code.

I would be very happy if someone could tell me how this could work because I believe after this I'm the world champion in creatively-cursing-java ^^

Benni

P.S.: I tried every single solution in every single thread about zxing, so this really was my last choice.

Benni
  • 778
  • 8
  • 22

1 Answers1

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

this worked for me, using a custom 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;
    }

}
Gutyn
  • 461
  • 4
  • 22