我在我的应用程序中集成了ZXing 3.1.1,使其能够拍摄条形码并对输入数据进行操作。
我可以用它扫描二维码(如QR码和数据矩阵),但所有一维条码都无法扫描。
我的代码如下:
public void onPictureTaken(byte[] data, Camera camera){
BinaryBitmap bbm = BinaryBitmapFromJpegData(data); // Impl. below
MultiFormatReader reader = new MultiFormatReader();
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
Collection<BarcodeFormat> possible_types = new ArrayList<BarcodeFormat>();
possible_types.add(BarcodeFormat.UPC_A);
possible_types.add(BarcodeFormat.UPC_E);
possible_types.add(BarcodeFormat.EAN_8);
possible_types.add(BarcodeFormat.EAN_13);
possible_types.add(BarcodeFormat.QR_CODE);
possible_types.add(BarcodeFormat.DATA_MATRIX);
hints.put(DecodeHintType.POSSIBLE_FORMAT, possible_types);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
try {
Result result = reader.decode(bbm, hints);
Toast.makeText(context, "Found barcode: " + result.getText(), Toast.LENGTH_LONG).show();
} catch (NotFoundException e) {
Toast.makeText(context, "No barcode found", Toast.LENGTH_LONG).show();
Log.d("myapp", "Not found! (" + e.getMessage() + ")", e);
}
}
private BinaryBitmap BinaryBitmapFromJpegData(byte[] data){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap bbm = new BinaryBitmap(new HybridBinarizer(source));
return bbm;
}
堆栈跟踪也不是很有帮助:
有没有人有任何想法?我正在三星Galaxy Note 2上测试它。
答案 0 :(得分:0)
解决方案是相机正在侧向拍摄,而ZXing似乎无法以一定角度检查1D条码。
以90度的间隔旋转位图为我解决了问题。
新功能如下:
public void onPictureTaken(byte[] data, Camera camera){
MultiFormatReader reader = new MultiFormatReader();
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
Collection<BarcodeFormat> possible_types = new ArrayList<BarcodeFormat>();
possible_types.add(BarcodeFormat.UPC_A);
possible_types.add(BarcodeFormat.UPC_E);
possible_types.add(BarcodeFormat.EAN_8);
possible_types.add(BarcodeFormat.EAN_13);
possible_types.add(BarcodeFormat.QR_CODE);
possible_types.add(BarcodeFormat.DATA_MATRIX);
hints.put(DecodeHintType.POSSIBLE_FORMAT, possible_types);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = null;
for(int i = 0; i < 4; i++){
BinaryBitmap bitmap = BinaryBitmapFromJpegData(data, i * 90);
try {
result = reader.decode(bitmap, hints);
break;
} catch (NotFoundException e) {}
}
if(result == null){
Toast.makeText(context, "No Barcode Found", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Found barcode: " + result.getText(), Toast.LENGTH_LONG).show();
}
}
private BinaryBitmap BinaryBitmapFromJpegData(byte[] data, int rotation){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if(rotation != 0){
bitmap = RotateBitmap(bitmap, rotation);
}
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap bbm = new BinaryBitmap(new HybridBinarizer(source));
return bbm;
}
public static Bitmap RotateBitmap(Bitmap source, float angle){
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}