<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://boofcv.org/index.php?action=history&amp;feed=atom&amp;title=Example_QR_Code_Binary_Data</id>
	<title>Example QR Code Binary Data - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://boofcv.org/index.php?action=history&amp;feed=atom&amp;title=Example_QR_Code_Binary_Data"/>
	<link rel="alternate" type="text/html" href="http://boofcv.org/index.php?title=Example_QR_Code_Binary_Data&amp;action=history"/>
	<updated>2026-04-30T06:52:53Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.2</generator>
	<entry>
		<id>http://boofcv.org/index.php?title=Example_QR_Code_Binary_Data&amp;diff=3184&amp;oldid=prev</id>
		<title>Peter: Created page with &quot;As is explained below, things are a bit tricky when you encode binary data inside a QR code. This example shows you multiple ways to decode the binary data without corrupting...&quot;</title>
		<link rel="alternate" type="text/html" href="http://boofcv.org/index.php?title=Example_QR_Code_Binary_Data&amp;diff=3184&amp;oldid=prev"/>
		<updated>2022-01-24T19:11:21Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;As is explained below, things are a bit tricky when you encode binary data inside a QR code. This example shows you multiple ways to decode the binary data without corrupting...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;As is explained below, things are a bit tricky when you encode binary data inside a QR code. This example shows you multiple ways to decode the binary data without corrupting it. Hopefully the person who created the QR Code knew what they were doing and didn&amp;#039;t corrupt the data either...&lt;br /&gt;
&lt;br /&gt;
Example Code:&lt;br /&gt;
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.40.1/examples/src/main/java/boofcv/examples/fiducial/ExampleQrCodeBinaryData.java ExampleQrCodeBinaryData.java]&lt;br /&gt;
&lt;br /&gt;
Concepts:&lt;br /&gt;
* QR Codes&lt;br /&gt;
* Binaries&lt;br /&gt;
&lt;br /&gt;
Relevant Examples/Tutorials:&lt;br /&gt;
* [[Tutorial_QRCodes|Tutorial QR Codes]]&lt;br /&gt;
* [[Example_Render_QR_Code|Rendering QR Codes]]&lt;br /&gt;
* [[Performance:QrCode|Performance Study]]&lt;br /&gt;
&lt;br /&gt;
= Example Code =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * When dealing with binary data embedded in a QR code things do get more complicated. You will need to convert&lt;br /&gt;
 * the string message into a byte array. By default, it&amp;#039;s assumed that BYTE data is a text string and it will&lt;br /&gt;
 * encode it into a string. In general the conversion to a string will not modify the data but it&amp;#039;s not defined&lt;br /&gt;
 * how illegal characters are encoded. To be safe you can force the encoding to be &amp;quot;binary&amp;quot;.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Peter Abeles&lt;br /&gt;
 */&lt;br /&gt;
public class ExampleQrCodeBinaryData {&lt;br /&gt;
	public static void main( String[] args ) throws UnsupportedEncodingException {&lt;br /&gt;
		// Let&amp;#039;s generate some random data. In the real world this could be a zip file or similar&lt;br /&gt;
		byte[] originalData = new byte[500];&lt;br /&gt;
		for (int i = 0; i &amp;lt; originalData.length; i++) {&lt;br /&gt;
			originalData[i] = (byte)i;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Generate a marking that&amp;#039;s encoded with this raw data&lt;br /&gt;
		QrCode original = new QrCodeEncoder().addBytes(originalData).fixate();&lt;br /&gt;
		var gray = new QrCodeGeneratorImage(/* pixel per module */ 5).render(original).getGray();&lt;br /&gt;
&lt;br /&gt;
		// Let&amp;#039;s detect and then decode the image&lt;br /&gt;
		var config = new ConfigQrCode();&lt;br /&gt;
		// If you force the encoding to &amp;quot;binary&amp;quot; then you turn off auto encoding you know the bit values will not&lt;br /&gt;
		// be modified. It&amp;#039;s undefined how illegal values are handled in different encodings, but often still work.&lt;br /&gt;
		config.forceEncoding = EciEncoding.BINARY;&lt;br /&gt;
		QrCodeDetector&amp;lt;GrayU8&amp;gt; detector = FactoryFiducial.qrcode(config, GrayU8.class);&lt;br /&gt;
		detector.process(gray);&lt;br /&gt;
&lt;br /&gt;
		for (QrCode qr : detector.getDetections()) {&lt;br /&gt;
			// Only consider QR codes that are encoded with BYTE data&lt;br /&gt;
			if (qr.mode != QrCode.Mode.BYTE)&lt;br /&gt;
				continue;&lt;br /&gt;
&lt;br /&gt;
			// Convert the message from a String to byte data. This only works if the &amp;#039;binary&amp;#039; encoding is used&lt;br /&gt;
			byte[] data;&lt;br /&gt;
			if (qr.byteEncoding.equals(EciEncoding.BINARY)) {&lt;br /&gt;
				data = BoofMiscOps.castStringToByteArray(qr.message);&lt;br /&gt;
			} else {&lt;br /&gt;
				// If it thought the byte data was UTF-8 you need to decode with UTF-8 because a single character&lt;br /&gt;
				// can be multiple bytes.&lt;br /&gt;
				data = qr.message.getBytes(qr.byteEncoding);&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			// See if the data got corrupted or not&lt;br /&gt;
			boolean perfectMatch = data.length == originalData.length;&lt;br /&gt;
			for (int i = 0; i &amp;lt; data.length; i++) {&lt;br /&gt;
				perfectMatch &amp;amp;= data[i] == originalData[i];&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			// Print the results&lt;br /&gt;
			System.out.println(&amp;quot;Encoding: &amp;#039;&amp;quot; + qr.byteEncoding + &amp;quot;&amp;#039; Matched: &amp;quot; + perfectMatch);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Peter</name></author>
	</entry>
</feed>