ZXing convert Bitmap to BinaryBitmap

后端 未结 3 1100
难免孤独
难免孤独 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: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.

提交回复
热议问题