Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the coordinates of QR code using ZXing?

I can get the value of the QR code using ZXing library but I also need the coordinates of the corner points of the QR code. How can I access those coordinates.

like image 870
Prakhar Ganesh Avatar asked Oct 17 '25 07:10

Prakhar Ganesh


1 Answers

I don't know exactly which corner coordinates you want to extract. But I'm quite sure you can calculate them from the result points got from the Result.getResultPoints() method.

Example: With the following image (200x200 pixels) and code snippet

QRCode

BufferedImage image = ImageIO.read(new File("QRCode-1.png"));
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
        new BufferedImageLuminanceSource(image)));
Reader reader = new QRCodeReader();
Result result = reader.decode(binaryBitmap);
System.out.println("text: " + result.getText());
ResultPoint[] resultPoints = result.getResultPoints();
System.out.println("resultPoints:");
for (int i = 0; i < resultPoints.length; i++) {
    ResultPoint resultPoint = resultPoints[i];
    System.out.print("  [" + i + "]:");
    System.out.print(" x = " + resultPoint.getX());
    System.out.print(", y = " + resultPoint.getY());
    if (resultPoint instanceof FinderPattern)
        System.out.print(", estimatedModuleSize = "
                + ((FinderPattern) resultPoint).getEstimatedModuleSize());
    System.out.println();
}

I get this output:

text: Hello World!
resultPoints:
  [0]: x = 36.5, y = 162.5, estimatedModuleSize = 9.0
  [1]: x = 36.5, y = 36.5, estimatedModuleSize = 9.0
  [2]: x = 162.5, y = 36.5, estimatedModuleSize = 9.0

x and y are the center coordinates of the square finder patterns. Knowing that the QRCode finder patterns have size of 7x7 modules, you can easily calculate the corner coordinates.

like image 94
Thomas Fritsch Avatar answered Oct 18 '25 23:10

Thomas Fritsch