{"id":4308,"date":"2016-07-19T21:19:02","date_gmt":"2016-07-19T13:19:02","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=4308"},"modified":"2017-09-29T09:38:18","modified_gmt":"2017-09-29T01:38:18","slug":"decode-qrcode-with-zxing-net-and-unity","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2016\/07\/19\/decode-qrcode-with-zxing-net-and-unity\/","title":{"rendered":"Decode QRCode with ZXing.net + Unity"},"content":{"rendered":"<p>Simple example to decode QRCode from texture image.<\/p>\n<p>&#8211; Download XZing.net from <a href=\"http:\/\/zxingnet.codeplex.com\/\" target=\"_blank\" rel=\"noopener\">http:\/\/zxingnet.codeplex.com\/<\/a><br \/>\n&#8211; Unzip, Copy zxing.unity.dll from Unity\/ folder into your Unity project (create Plugins\/ folder and place the dll there)<br \/>\n&#8211; Download some QRCode image, like: <a href=\"https:\/\/i0.wp.com\/www.hypergridbusiness.com\/wp-content\/uploads\/2017\/03\/VR-Box-2-QR-Code-from-Sites-in-VR.jpg\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.hypergridbusiness.com\/wp-content\/uploads\/2017\/03\/VR-Box-2-QR-Code-from-Sites-in-VR.jpg\" \/><\/a><br \/>\n&#8211; Create new script (see source below)<br \/>\n&#8211; Attach script to empty gameobject<br \/>\n&#8211; Assign your QRCode texture into inputTexture field<br \/>\n&#8211; Hit Play!<br \/>\n&#8211; See console output:<\/p>\n<pre>\u00a0 QR_CODE\r\n\u00a0 goo.gl\/BF4kdV<\/pre>\n<p>&nbsp;<\/p>\n<p>&#8212;<\/p>\n<p>Your Project window structure<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4310\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2016\/07\/19\/decode-qrcode-with-zxing-net-and-unity\/xzing_unity\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2016\/07\/xzing_unity.jpg?fit=365%2C195&amp;ssl=1\" data-orig-size=\"365,195\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"xzing_unity\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2016\/07\/xzing_unity.jpg?fit=300%2C160&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2016\/07\/xzing_unity.jpg?fit=365%2C195&amp;ssl=1\" class=\"alignnone size-medium wp-image-4310\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2016\/07\/xzing_unity-300x160.jpg?resize=300%2C160\" alt=\"xzing_unity\" width=\"300\" height=\"160\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2016\/07\/xzing_unity.jpg?resize=300%2C160&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2016\/07\/xzing_unity.jpg?w=365&amp;ssl=1 365w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>&#8212;<\/p>\n<p><strong>Source:<\/strong> <span class=\"pl-en\">ReadBarcodeFromFile.cs<\/span><\/p>\n<style>.gist table { margin-bottom: 0; }<\/style>\n<div style=\"tab-size: 8\" id=\"gist37963340\" class=\"gist\">\n<div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n<div class=\"gist-data\">\n<div class=\"js-gist-file-update-container js-task-list-container\">\n<div id=\"file-readbarcodefromfile-cs\" class=\"file my-2\">\n<div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-c  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"ReadBarcodeFromFile.cs content, created by unitycoder on 01:09PM on July 19, 2016.\"\n    ><\/p>\n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n<p>  <template class=\"js-file-alert-template\"><\/p>\n<div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n    <span><br \/>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br \/>\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a><br \/>\n    <\/span><\/p>\n<div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters<br \/>\n<\/a>\n<\/div>\n<\/div>\n<p><\/template><br \/>\n<template class=\"js-line-alert-template\"><br \/>\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\"><br \/>\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n<\/span><\/template><\/p>\n<table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"ReadBarcodeFromFile.cs\">\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC1\" class=\"blob-code blob-code-inner js-file-line\">using UnityEngine;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC2\" class=\"blob-code blob-code-inner js-file-line\">using System.Collections;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC3\" class=\"blob-code blob-code-inner js-file-line\">using ZXing;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC4\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC5\" class=\"blob-code blob-code-inner js-file-line\">public class ReadBarcodeFromFile : MonoBehaviour<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC6\" class=\"blob-code blob-code-inner js-file-line\">{<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC7\" class=\"blob-code blob-code-inner js-file-line\">    public Texture2D inputTexture; \/\/ Note: [x] Read\/Write must be enabled from texture import settings<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC8\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC9\" class=\"blob-code blob-code-inner js-file-line\">    void Start()<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC10\" class=\"blob-code blob-code-inner js-file-line\">    {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC11\" class=\"blob-code blob-code-inner js-file-line\">        \/\/ create a barcode reader instance<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC12\" class=\"blob-code blob-code-inner js-file-line\">        IBarcodeReader reader = new BarcodeReader();<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC13\" class=\"blob-code blob-code-inner js-file-line\">        \/\/ get texture Color32 array<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC14\" class=\"blob-code blob-code-inner js-file-line\">        var barcodeBitmap = inputTexture.GetPixels32();<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC15\" class=\"blob-code blob-code-inner js-file-line\">        \/\/ detect and decode the barcode inside the Color32 array<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC16\" class=\"blob-code blob-code-inner js-file-line\">        var result = reader.Decode(barcodeBitmap, inputTexture.width, inputTexture.height);<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC17\" class=\"blob-code blob-code-inner js-file-line\">        \/\/ do something with the result<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC18\" class=\"blob-code blob-code-inner js-file-line\">        if (result != null)<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC19\" class=\"blob-code blob-code-inner js-file-line\">        {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC20\" class=\"blob-code blob-code-inner js-file-line\">            Debug.Log(result.BarcodeFormat.ToString());<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC21\" class=\"blob-code blob-code-inner js-file-line\">            Debug.Log(result.Text);<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC22\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC23\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-readbarcodefromfile-cs-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n<td id=\"file-readbarcodefromfile-cs-LC24\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<\/table>\n<\/div><\/div>\n<\/p><\/div>\n<\/div><\/div>\n<div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/unitycoder\/6e7866eec8cd864be0f3c036ff1ab57b\/raw\/3a1163279701446c584fdf9c5825d3e591fd42ca\/ReadBarcodeFromFile.cs\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a><br \/>\n        <a href=\"https:\/\/gist.github.com\/unitycoder\/6e7866eec8cd864be0f3c036ff1ab57b#file-readbarcodefromfile-cs\" class=\"Link--inTextBlock\"><br \/>\n          ReadBarcodeFromFile.cs<br \/>\n        <\/a><br \/>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Simple example to decode QRCode from texture image. &#8211; Download XZing.net from http:\/\/zxingnet.codeplex.com\/ &#8211; Unzip, Copy zxing.unity.dll from Unity\/ folder into your Unity project (create Plugins\/ folder and place the dll there) &#8211; Download some QRCode image, like: &#8211; Create new script (see source below) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4309,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[420,3],"tags":[887,890,888,886,113,889],"class_list":["post-4308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-unity3d","tag-barcode","tag-decode","tag-detect","tag-qrcode","tag-scan","tag-zxing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2016\/07\/QR-Code1.jpg?fit=135%2C135&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-17u","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=4308"}],"version-history":[{"count":3,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4308\/revisions"}],"predecessor-version":[{"id":4615,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4308\/revisions\/4615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/4309"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=4308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=4308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=4308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}