我正在使用OpenCV和Zxing,我想添加二维码扫描。我有几种类型的图像可以发送。最好的可能是 Bitmat(另一个选项是 OpenCV Mat)。
看起来您以前可以这样转换:
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 看起来不再将位图作为输入。那么我还能如何将输入图像转换为 BinaryBitmap???
编辑:
好的,我相信我已经取得了一些进展,但我仍然遇到问题。我想我有将位图转换为正确格式的代码,但是我现在得到一个 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;
}
我得到了错误
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)
我记录了byte[]的大小,就是上面显示的长度。我不明白为什么 zxing 期望它更大
最佳答案
好的,我明白了。正如 Sean Owen 所说,PlanarYUVLuminaceSource 仅适用于默认的 android 相机格式,我猜 OpenCV 不会使用它。简而言之,您可以按照以下方式进行操作:
//(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 位图转换为整数数组,这简直是小菜一碟。
关于android - ZXing 将位图转换为二进制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861553/