{"id":8916,"date":"2017-11-03T09:00:58","date_gmt":"2017-11-03T13:00:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8916"},"modified":"2017-11-03T18:23:19","modified_gmt":"2017-11-03T22:23:19","slug":"qr-code-encoderdecoder","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/11\/03\/qr-code-encoderdecoder\/","title":{"rendered":"QR Code Encoder\/Decoder"},"content":{"rendered":"<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29239-qr-code-encode-and-decode\">QR Code encode and decode<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2577900\">Lior Shapira<\/a>.<\/p><p>On October 31, we held our biggest annual event of the year, the MATLAB EXPO 2017 Japan. This year, I took part in organizing a couple of new sessions: a Lightning Talk session around fun\/hobby uses of MATLAB, and a MATLAB Coding Contest.<\/p><p>As part of these sessions, I wanted to make use of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/QR_code\">QR code<\/a> (which encoded the registration ID number) that each attendee was given. For the lightning talk, I needed to quickly capture the IDs of the audience to do a drawing at the end for prizes. For the coding contest, I needed a way to capture the IDs of the players to keep score.<\/p><p>This is what the EXPO participants had hanging around their necks.<\/p><pre class=\"codeinput\">im = imread(<span class=\"string\">'qr_example.jpg'<\/span>);\r\nimshow(im)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_qr_encode_decode\/potw_qr_encode_decode_01.png\" alt=\"\"> <p>Rather than try to detect and do <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/ocr.html\">OCR<\/a> on the ID number, I figured it would be easier to try to read the QR code. So, how do I read a QR code? Well, my colleague, Takeyoshi, took on the challenge to do that. After searching the web to find the encoding\/decoding specs, he put together a nice function that used Lior&#8217;s entry on the File Exchange. The entry is a wrapper around a Java library <a href=\"https:\/\/github.com\/zxing\/zxing\">ZXing<\/a>, and we were pleasantly surprised at the accuracy it gave.<\/p><pre class=\"codeinput\"><span class=\"comment\">% Add java path<\/span>\r\njavaaddpath(<span class=\"string\">'core-3.3.0.jar'<\/span>)\r\njavaaddpath(<span class=\"string\">'javase-3.3.0.jar'<\/span>)\r\n\r\nstr = decode_qr(im)\r\n<\/pre><pre class=\"codeoutput\">str =\r\n    '10007'\r\n<\/pre><p>With a little bit of modification, I was able to create an ID Scanner for the purpose of the EXPO sessions.<\/p><div style=\"width: 640px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-8916-1\" width=\"640\" height=\"360\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_qr_encode_decode\/QRReader.mp4?_=1\" \/><a href=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_qr_encode_decode\/QRReader.mp4\">https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_qr_encode_decode\/QRReader.mp4<\/a><\/video><\/div><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8916#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29239-qr-code-encode-and-decode#comment\">comment<\/a> for Lior.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_95617a3f09144eb2823c32d00f01b393() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='95617a3f09144eb2823c32d00f01b393 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 95617a3f09144eb2823c32d00f01b393';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_95617a3f09144eb2823c32d00f01b393()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><\/div><!--\r\n95617a3f09144eb2823c32d00f01b393 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29239-qr-code-encode-and-decode QR Code encode\r\n% and decode> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2577900 Lior\r\n% Shapira>.\r\n%\r\n% On October 31, we held our biggest annual event of the year, the MATLAB\r\n% EXPO 2017 Japan. This year, I took part in organizing a couple of new\r\n% sessions: a Lightning Talk session around fun\/hobby uses of MATLAB, and a\r\n% MATLAB Coding Contest.\r\n%\r\n% As part of these sessions, I wanted to make use of the\r\n% <https:\/\/en.wikipedia.org\/wiki\/QR_code QR code> (which encoded the\r\n% registration ID number) that each attendee was given. For the lightning\r\n% talk, I needed to quickly capture the IDs of the audience to do a drawing\r\n% at the end for prizes. For the coding contest, I needed a way to capture\r\n% the IDs of the players to keep score.\r\n%\r\n% This is what the EXPO participants had hanging around their necks.\r\n\r\nim = imread('qr_example.jpg');\r\nimshow(im)\r\n\r\n%%\r\n% Rather than try to detect and do\r\n% <https:\/\/www.mathworks.com\/help\/vision\/ref\/ocr.html OCR> on the ID\r\n% number, I figured it would be easier to try to read the QR code. So, how\r\n% do I read a QR code? Well, my colleague, Takeyoshi, took on the challenge\r\n% to do that. After searching the web to find the encoding\/decoding specs,\r\n% he put together a nice function that used Lior's entry on the File\r\n% Exchange. The entry is a wrapper around a Java library\r\n% <https:\/\/github.com\/zxing\/zxing ZXing>, and we were pleasantly surprised\r\n% at the accuracy it gave.\r\n\r\n% Add java path\r\njavaaddpath('core-3.3.0.jar')\r\njavaaddpath('javase-3.3.0.jar')\r\n\r\nstr = decode_qr(im)\r\n\r\n%%\r\n% With a little bit of modification, I was able to create an ID Scanner for\r\n% the purpose of the EXPO sessions.\r\n%\r\n% <html>\r\n% [video mp4=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_qr_encode_decode\/QRReader.mp4\"][\/video]\r\n% <\/html>\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8916#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29239-qr-code-encode-and-decode#comment comment> for Lior.\r\n\r\n##### SOURCE END ##### 95617a3f09144eb2823c32d00f01b393\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_qr_encode_decode\/potw_qr_encode_decode_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Jiro&#8216;s pick this week is QR Code encode and decode by Lior Shapira.On October 31, we held our biggest annual event of the year, the MATLAB EXPO 2017 Japan. This year, I took part in organizing&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/11\/03\/qr-code-encoderdecoder\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8916"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8916"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8916\/revisions"}],"predecessor-version":[{"id":8925,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8916\/revisions\/8925"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}