【问题标题】:Decode datamatrix out of picture taken by camera2 with zxing使用 zxing 从 camera2 拍摄的图片中解码数据矩阵
【发布时间】:2015-10-27 12:56:50
【问题描述】:


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

当用QRCodeReader 替换DataMatrixReader 并用二维码尝试或用MultiFormatReader 尝试时,这甚至不起作用。
我尝试处理的每张图像都被 zxing 条码扫描仪应用程序正确解码,所以问题出在代码中。

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

本尼

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

【问题讨论】:

    标签: java android zxing decoding datamatrix


    【解决方案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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多