Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fernandocyder/d95e55bd5c3a3b8d8c45944afe6b4ac3 to your computer and use it in GitHub Desktop.

Select an option

Save fernandocyder/d95e55bd5c3a3b8d8c45944afe6b4ac3 to your computer and use it in GitHub Desktop.

Revisions

  1. fernandocyder created this gist Mar 2, 2021.
    32 changes: 32 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    public class BoofCVQRCodeReader {

    public static String read(String filePath) {

    // read the image to memory
    BufferedImage input = UtilImageIO.loadImage(filePath);

    // gray-out the image, easy to process
    GrayU8 gray = ConvertBufferedImage.convertFrom(input, (GrayU8) null);
    QrCodeDetector<GrayU8> detector = FactoryFiducial.qrcode(null, GrayU8.class);
    detector.process(gray);

    // get a list of all the qr codes it could successfully detect and decode
    List<QrCode> detections = detector.getDetections();

    Graphics2D g2 = input.createGraphics();
    int strokeWidth = Math.max(4, input.getWidth() / 200); // in large images the line can be too thin
    g2.setColor(Color.GREEN);
    g2.setStroke(new BasicStroke(strokeWidth));

    // get the first detected QR code and return its text
    for (QrCode qr : detections) {
    // The message encoded in the marker
    System.out.println("found message: " + qr.message);
    return qr.message;
    }

    // not able to detect any QR code, return NULL
    return null;
    }

    }