4

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.

1 Answer 1

5

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.

5
  • This code works well when the QR code occupies large part of the image. The QR code I want to scan is on the top left corner of a page and is not very big so the code is not able to detect it.Can you suggest some solution for this. Apr 5, 2017 at 10:45
  • @PrakharGanesh Now that is an entirely different issue. May be your QRCodeReader works better when you add the TRY_HARDER decode hint. Apr 5, 2017 at 18:33
  • what represents the estimatedModuleSize? the square on this QR are very symmetric but I have found some QR codes where the squares finder are smaller that the space in between them.. I am not so sure how to find the corners on that case, I am thinking that the estimatedModuleSize maybe can help there, but how?
    – delkant
    Dec 28, 2018 at 15:24
  • 1
    @delkant The "modules" are the small elementary black or white squares of the QR-code. In the shown example image each module has size of 9x9 image pixels. (Just open the image in an image editor and count the pixels.) Dec 28, 2018 at 17:20
  • One more word, the finder pattern has a width ratio of 1:1:3:1:1 (black vs white). That is, the black square in the center has width of 3*moduleSize pixels.
    – zhy
    Apr 22, 2022 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.