Difference between revisions of "Example Detect QR Code"

From BoofCV
Jump to navigationJump to search
(Created page with "<center> <gallery widths=807px heights=331px> file:Example_qrcode_detection.jpg | Several QR Codes are detected in this image. Correctly decoded ones are highlighted in green...")
 
m
(5 intermediate revisions by the same user not shown)
Line 8: Line 8:


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.28/examples/src/main/java/boofcv/examples/fiducial/ExampleDetectQrCode.java ExampleDetectQrCode.java]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.37/examples/src/main/java/boofcv/examples/fiducial/ExampleDetectQrCode.java ExampleDetectQrCode.java]


Concepts:
Concepts:
* Fiducials
* Fiducials
* QR Codes
* QR Codes
Relevant Videos:
* [https://youtu.be/TGg-xgTyaU8?t=272 Youtube Demo]


Relevant Examples/Tutorials:
Relevant Examples/Tutorials:
* [[Tutorial_Fiducials|Tutorial Fiducials]]
* [[Tutorial_QRCodes|Tutorial QR Codes]]
 
* [[Example_Render_QR_Code|Rendering QR Codes]]
* [[Performance:QrCode|Performance Study]]


= Example Code =
= Example Code =
Line 29: Line 33:
  */
  */
public class ExampleDetectQrCode {
public class ExampleDetectQrCode {
public static void main(String[] args) {
public static void main( String[] args ) {
BufferedImage input = UtilImageIO.loadImage(UtilIO.pathExample("fiducial/qrcode/image01.jpg"));
BufferedImage input = UtilImageIO.loadImage(UtilIO.pathExample("fiducial/qrcode/image01.jpg"));
GrayU8 gray = ConvertBufferedImage.convertFrom(input,(GrayU8)null);
GrayU8 gray = ConvertBufferedImage.convertFrom(input, (GrayU8)null);


QrCodeDetector<GrayU8> detector = FactoryFiducial.qrcode(null,GrayU8.class);
QrCodeDetector<GrayU8> detector = FactoryFiducial.qrcode(null, GrayU8.class);


detector.process(gray);
detector.process(gray);
Line 41: Line 45:


Graphics2D g2 = input.createGraphics();
Graphics2D g2 = input.createGraphics();
int strokeWidth = Math.max(4,input.getWidth()/200); // in large images the line can be too thin
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));
g2.setColor(Color.GREEN); g2.setStroke(new BasicStroke(strokeWidth));
for( QrCode qr : detections ) {
for (QrCode qr : detections) {
// The message encoded in the marker
// The message encoded in the marker
System.out.println("message: "+qr.message);
System.out.println("message: " + qr.message);


// Visualize its location in the image
// Visualize its location in the image
VisualizeShapes.drawPolygon(qr.bounds,true,1,g2);
VisualizeShapes.drawPolygon(qr.bounds, true, 1, g2);
}
}


Line 54: Line 58:
List<QrCode> failures = detector.getFailures();
List<QrCode> failures = detector.getFailures();
g2.setColor(Color.RED);
g2.setColor(Color.RED);
for( QrCode qr : failures ) {
for (QrCode qr : failures) {
// If the 'cause' is ERROR_CORRECTION or later then it's probably a real QR Code that
// If the 'cause' is ERROR_CORRECTION or later then it's probably a real QR Code that
if( qr.failureCause.ordinal() < QrCode.Failure.ERROR_CORRECTION.ordinal() )
if (qr.failureCause.ordinal() < QrCode.Failure.ERROR_CORRECTION.ordinal())
continue;
continue;


VisualizeShapes.drawPolygon(qr.bounds,true,1,g2);
VisualizeShapes.drawPolygon(qr.bounds, true, 1, g2);
}
}


ShowImages.showWindow(input,"Example QR Codes", true);
ShowImages.showWindow(input, "Example QR Codes", true);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 18:59, 21 December 2020

QR Codes are found on many consumer products and often encode information such as a website. BoofCV provides a QR Code detector is designed to be fast on large images, detect small markers in large images, and be rotation invariant. It also provides much more internal information on the QR codes than other popular detectors. It even returns the rejected markers.

Example Code:

Concepts:

  • Fiducials
  • QR Codes

Relevant Videos:

Relevant Examples/Tutorials:

Example Code

/**
 * Shows you how to detect a QR Code inside an image and process the extracted data. Much of the information that
 * is computed while detecting and decoding a QR Code is saved inside the {@link QrCode} class. This can be useful
 * for application developers.
 *
 * @author Peter Abeles
 */
public class ExampleDetectQrCode {
	public static void main( String[] args ) {
		BufferedImage input = UtilImageIO.loadImage(UtilIO.pathExample("fiducial/qrcode/image01.jpg"));
		GrayU8 gray = ConvertBufferedImage.convertFrom(input, (GrayU8)null);

		QrCodeDetector<GrayU8> detector = FactoryFiducial.qrcode(null, GrayU8.class);

		detector.process(gray);

		// Get's 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));
		for (QrCode qr : detections) {
			// The message encoded in the marker
			System.out.println("message: " + qr.message);

			// Visualize its location in the image
			VisualizeShapes.drawPolygon(qr.bounds, true, 1, g2);
		}

		// List of objects it thinks might be a QR Code but failed for various reasons
		List<QrCode> failures = detector.getFailures();
		g2.setColor(Color.RED);
		for (QrCode qr : failures) {
			// If the 'cause' is ERROR_CORRECTION or later then it's probably a real QR Code that
			if (qr.failureCause.ordinal() < QrCode.Failure.ERROR_CORRECTION.ordinal())
				continue;

			VisualizeShapes.drawPolygon(qr.bounds, true, 1, g2);
		}

		ShowImages.showWindow(input, "Example QR Codes", true);
	}
}