Example Detect QR Code

From BoofCV
Jump to navigationJump to search

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:


  • 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.loadImageNotNull(UtilIO.pathExample("fiducial/qrcode/image01.jpg"));
		GrayU8 gray = ConvertBufferedImage.convertFrom(input, (GrayU8)null);

		var config = new ConfigQrCode();
//		config.considerTransposed = false; // by default, it will consider incorrectly encoded markers. Faster if false
		QrCodeDetector<GrayU8> detector = FactoryFiducial.qrcode(config, GrayU8.class);


		// Gets 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.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();
		for (QrCode qr : failures) {
			// If the 'cause' is ERROR_CORRECTION or higher, then there's a decent chance it's a real marker
			if (qr.failureCause.ordinal() < QrCode.Failure.ERROR_CORRECTION.ordinal())

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

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