Can't scan non perfect images in Java using zxing

97 views
Skip to first unread message

Ani Srikanth

unread,
May 15, 2019, 3:10:08 PM5/15/19
to zxing
I am currently trying to use Zxing to scan and read PDF417 Type barcodes. My code works with perfect barcodes but fails to work with smartphone images of a barcode.

I am currently using the Barcode Detector method to find where the corners of the barcode is and then using those corners to decode the barcode.

The following is my current solution:

```Java
public class WhiteRectangleMethod {

public static String decodeBarcode(String filepath) throws IOException, NotFoundException, FormatException, ChecksumException {
String rawText = "";
BufferedImage image = ImageIO.read(new File(filepath));

BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

BufferedImageLuminanceSource bils = new BufferedImageLuminanceSource(image);

HybridBinarizer hb = new HybridBinarizer(bils);
BitMatrix bitmatrix = hb.getBlackMatrix();

Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.TRY_HARDER,Boolean.TRUE);
hints.put(DecodeHintType.PURE_BARCODE,Boolean.FALSE);
hints.put(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.PDF_417);

PDF417DetectorResult corners = Detector.detect(bb, hints, false);

//Result res = new PDF417Reader().decode(bb,hints);
//return res.getText();
//ResultPoint corner = corners.getPoints().get(0)[0];

if (corners.getPoints().size() > 0) {
try {
DecoderResult dr = PDF417ScanningDecoder.decode(bitmatrix, corners.getPoints().get(0)[4], corners.getPoints().get(0)[5], corners.getPoints().get(0)[6], corners.getPoints().get(0)[7], 0, 999999999);
return dr.getText();
} catch (Exception e) {
e.printStackTrace();
try {
DecoderResult dr = PDF417ScanningDecoder.decode(bitmatrix, corners.getPoints().get(0)[0], corners.getPoints().get(0)[1], corners.getPoints().get(0)[2], corners.getPoints().get(0)[3], 0, 999999999);
return dr.getText();
} catch (Exception b) {
b.printStackTrace();
try {

} catch (Exception g) {
g.printStackTrace();
}
}

}
}
return null;
}

public static void main(String[] args) {
String filepath = "/Users/asrikanth/Desktop/test.png";
try {
System.out.println("Raw Text: " + decodeBarcode(filepath));
}catch (Exception e){
e.printStackTrace();
}
}
}
```
How can I get it so that any image's barcode is recognized?

perfect.png
non-perfect.JPG
Reply all
Reply to author
Forward
0 new messages