ZXing convert Bitmap to BinaryBitmap

后端 未结 3 1041
难免孤独
难免孤独 2020-12-04 20:19

I am using OpenCV and Zxing, and I\'d like to add 2d code scanning. I have a few types of images that I could send. Probably the best is Bitmat (the other option is OpenCV

相关标签:
3条回答
  • 2020-12-04 21:06
    public static String readQRImage(Bitmap bMap) {
        String contents = null;
    
        int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];  
        //copy pixel data from the Bitmap into the 'intArray' array  
        bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());  
    
        LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    
        Reader reader = new MultiFormatReader();// use this otherwise ChecksumException
        try {
            Result result = reader.decode(bitmap);
            contents = result.getText(); 
            //byte[] rawBytes = result.getRawBytes(); 
            //BarcodeFormat format = result.getBarcodeFormat(); 
            //ResultPoint[] points = result.getResultPoints();
        } catch (NotFoundException e) { e.printStackTrace(); } 
        catch (ChecksumException e) { e.printStackTrace(); }
        catch (FormatException e) { e.printStackTrace(); } 
        return contents;
    }
    
    0 讨论(0)
  • 2020-12-04 21:11

    Bitmap is an Android class. Android's default image format from the camera is a planar YUV format. That is why only PlanarYUVLuminanceSource is needed and exists for Android. RGBLuminanceSource would have to be ported.

    You are putting completely the wrong kind of data into the class. It is expecting pixels in YUV planar format. You are passing compressed bytes of a JPEG file.

    0 讨论(0)
  • 2020-12-04 21:20

    Ok I got it. As Sean Owen said, PlanarYUVLuminaceSource would only be for the default android camera format, which I guess OpenCV does not use. So in short, here is how you would do it:

    //(note, mTwod is the CV Mat that contains my datamatrix code)
    
    Bitmap bMap = Bitmap.createBitmap(mTwod.width(), mTwod.height(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mTwod, bMap);
    int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];  
    //copy pixel data from the Bitmap into the 'intArray' array  
    bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());  
    
    LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray);
    
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Reader reader = new DataMatrixReader();     
    //....doing the actually reading
    Result result = reader.decode(bitmap);
    

    So thats it, not hard at all. Just had to convert the Android Bitmap to an integer array, and its a piece of cake.

    0 讨论(0)
提交回复
热议问题