I have a jpeg file which has 2D bar code. Image resolution is 1593X1212. I am using xing library to decode this barcode from image. I got following code on net.
I had the same problem. When I was running nearly exactly the same code on the Java SE libs it worked. When I run the Android code using the same picture it didn't work. Spend a lot of hours trying to find out...
You can Scale a bitmap using
Bitmap resize = Bitmap.createScaledBitmap(srcBitmap, dstWidth,dstHeight,false);
problem: You have to turn on some flags. Playing around with nearly all the flags this solution worked for me:
Map<DecodeHintType, Object> tmpHintsMap = new EnumMap<DecodeHintType, Object>(
DecodeHintType.class);
tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS,
EnumSet.allOf(BarcodeFormat.class));
tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
...
MultiFormatReader mfr = null;
mfr = new MultiFormatReader();
result = mfr.decode(binaryBitmap, tmpHintsMap);
problem: The Android library of ZXing run the barcode scan once, supposing the barcode on the picture already has the right orientation. If this is not the case you have to run it four times, each time rotation the picture around 90 degree!
For rotation you can use this method. Angle is the angle in degrees.
public 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);
}
I had the same problem, I was calling a readQRCode(filePath, charset, hintMap); and was getting the same message. I was calling a library I had written using the zxing libraries. To fix it just add the (zxing) jars to your top level code, even if the libraries are not accessed there.
try {
String a = textField_1.getText(); //my image path
InputStream barCodeInputStream = new FileInputStream(""+a);
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
com.google.zxing.Result result = reader.decode(bitmap);
System.out.println("Barcode text is " + result.getText());
textField.setText(""+result.getText());
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "This image does not contain barcode", "Warning", JOptionPane.WARNING_MESSAGE);
e.printStackTrace();
}