1


10時間以上検索して試した後、最終的にここで質問することにしました. ライブラリを使用してandroid.hardware.camera2、デバイスのカメラから画像を取得しています。zxingライブラリを使用して、ビットマップを自動的に処理し、画像にデータマトリックスコードがある場合はそれをデコードしたいと考えています。画像を 1 秒間に 5 回処理するタイマーがあり、すべて正常に動作しますが、datamatrix コードを認識しません。今まで私は次のコードを持っています:

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これは、qrコードに置き換えてQRCodeReader試したり、 で試したりしても機能しません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 に答える