ZXing转换成位图BinaryBitmap [英] ZXing convert Bitmap to BinaryBitmap
问题描述
我使用的OpenCV和Zxing,我想补充的二维code扫描。我有几个类型,我可以发送图像。也许最好的是Bitmat(另一种选择是OpenCV的垫)。
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 Mat).
看起来你曾经是能够将这样的:
It looks like you used to be able to convert like this:
Bitmap frame = //this is the frame coming in
LuminanceSource source = new RGBLuminanceSource(frame);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
//then I can use reader.decode(bitmap) to decode the BinaryBitmap
然而,RGBLuminaceSource看起来它不再需要的位图作为输入。因此,如何还能将输入图像I转换为BinaryBitmap ???
However, RGBLuminaceSource looks like it no longer takes a bitmap as an input. So how else can I convert an input image to BinaryBitmap???
好了,所以我相信我已经取得了一些进展,但我仍然有一个问题。我想我有code,它转换成位图格式是否正确,但我现在得到一个的ArrayIndexOutOfBounds
Ok so I believe I've made some progress, but I'm still having an issue. I think I have the code that converts the Bitmap into the correct format, however I am now getting an arrayIndexOutOfBounds
public void zxing(){
Bitmap bMap = Bitmap.createBitmap(frame.width(), frame.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(frame, bMap);
byte[] array = BitmapToArray(bMap);
LuminanceSource source = new PlanarYUVLuminanceSource(array, bMap.getWidth(), bMap.getHeight(), 0, 0, bMap.getWidth(), bMap.getHeight(), false);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new DataMatrixReader();
String sResult = "";
try {
Result result = reader.decode(bitmap);
sResult = result.getText();
Log.i("Result", sResult);
}
catch (NotFoundException e) {
Log.d(TAG, "Code Not Found");
e.printStackTrace();
}
}
public byte[] BitmapToArray(Bitmap bmp){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
我得到的错误
I get the error
02-14 10:19:27.469: E/AndroidRuntime(29736): java.lang.ArrayIndexOutOfBoundsException: length=33341; index=34560 02-14 10:19:27.469: E/AndroidRuntime(29736): at
com.google.zxing.common.HybridBinarizer.calculateBlackPoints(HybridBinarizer.java:199)
我已经登录的字节[]的大小,它是上面所示的长度。我无法弄清楚,为什么zxing期待它更大
I have logged the size of the byte[], and it is the length shown above. I cant figure out why zxing is expecting it to be bigger
推荐答案
好吧,我知道了。肖恩欧文说,PlanarYUVLuminaceSource只会是默认的Android摄像头格式,这是我猜的OpenCV不使用。因此,在短期,这里是你将如何做到这一点:
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);
所以完蛋了,不难的。刚把到Android位图转换为整数数组,它的一块蛋糕。
So thats it, not hard at all. Just had to convert the Android Bitmap to an integer array, and its a piece of cake.
这篇关于ZXing转换成位图BinaryBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!