Tanoda
QRCodeDecodeController.cs
Go to the documentation of this file.
1
4
5using System;
6using UnityEngine;
7using ZXing;
8
9public class QRCodeDecodeController : MonoBehaviour
10{
11 public delegate void QRScanFinished(string str); //declare a delegate to deal with the QRcode decode complete
12
13 public event QRScanFinished onQRScanFinished; //declare a event with the delegate to trigger the complete event
14
15 bool decoding;
16 bool tempDecodeing;
17 string dataText;
19 private Color[] orginalc; //the colors of the camera data.
20 private Color32[] targetColorARR; //the colors of the camera data.
21 private byte[] targetbyte; //the pixels of the camera image.
22 private int W, H, WxH; //width/height of the camera image
23 int framerate;
24
25#if UNITY_IOS
26 int blockWidth = 450;
27 #elif UNITY_ANDROID
28 int blockWidth = 350;
29 #else
30 int blockWidth = 350;
31#endif
32 bool isInit;
33 BarcodeReader barReader;
34
35 void Start()
36 {
37 barReader = new BarcodeReader();
38 barReader.AutoRotate = true;
39 barReader.TryInverted = true;
40
42 {
43 e_DeviceController = FindObjectOfType<DeviceCameraController>();
45 Debug.LogError("the Device Controller is not exsit,Please Drag DeviceCamera from project to Hierarchy");
46 }
47 }
48
49 void Update()
50 {
51#if UNITY_EDITOR
52 if (framerate++ % 15 == 0)
53 {
54#elif UNITY_IOS || UNITY_ANDROID
55 if (framerate++ % 15== 0) {
56#else
57 if (framerate++ % 20== 0) {
58#endif
59 if (e_DeviceController.isPlaying && !decoding)
60 {
61 W = e_DeviceController.dWebCam.Width(); // get the image width
62 H = e_DeviceController.dWebCam.Height(); // get the image height
63
64 if (W < 100 || H < 100) return;
65
66 if (!isInit && W > 100 && H > 100)
67 {
68 blockWidth = Math.Min(W, H);
69 isInit = true;
70 }
71
72 if (targetColorARR == null) targetColorARR = new Color32[blockWidth * blockWidth];
73
74 var posx = (W - blockWidth) >> 1; //
75 var posy = (H - blockWidth) >> 1;
76
77 orginalc = e_DeviceController.dWebCam.GetPixels(posx, posy, blockWidth,
78 blockWidth); // get the webcam image colors
79
80 //convert the color(float) to color32 (byte)
81 for (var i = 0; i != blockWidth; i++)
82 for (var j = 0; j != blockWidth; j++)
83 {
84 targetColorARR[i + j * blockWidth].r = (byte) (orginalc[i + j * blockWidth].r * 255);
85 targetColorARR[i + j * blockWidth].g = (byte) (orginalc[i + j * blockWidth].g * 255);
86 targetColorARR[i + j * blockWidth].b = (byte) (orginalc[i + j * blockWidth].b * 255);
87 targetColorARR[i + j * blockWidth].a = 255;
88 }
89#if !UNITY_WEBGL
90 // scan the qrcode
91 Loom.RunAsync(() =>
92 {
93 try
94 {
95 Result data;
96 data = barReader.Decode(targetColorARR, blockWidth, blockWidth); //start decode
97 if (data != null) // if get the result success
98 {
99 decoding = true; // set the variable is true
100 dataText = data.Text; // use the variable to save the code result
101 }
102 }
103 catch (Exception e)
104 {
105 // Debug.LogError("Decode Error: " + e.Data.ToString());
106 decoding = false;
107 }
108 });
109#else
110 Result data;
111 data = barReader.Decode(targetColorARR, blockWidth, blockWidth);//start decode
112 if (data != null) // if get the result success
113 {
114 decoding = true; // set the variable is true
115 dataText = data.Text; // use the variable to save the code result
116 }
117#endif
118 }
119
120 if (decoding)
121 {
122 // if the status variable is change
123 if (tempDecodeing != decoding) onQRScanFinished(dataText); //triger the scan finished event;
124 tempDecodeing = decoding;
125 }
126 }
127 }
128
132 public void Reset()
133 {
134 decoding = false;
135 tempDecodeing = decoding;
136 }
137
141 public void StartWork()
142 {
144 decoding = false;
145 tempDecodeing = decoding;
146 }
147
151 public void StopWork()
152 {
153 decoding = true;
154 tempDecodeing = decoding;
156 }
157
163 public static string DecodeByStaticPic(Texture2D tex)
164 {
165 var codeReader = new BarcodeReader();
166 codeReader.AutoRotate = true;
167 codeReader.TryInverted = true;
168
169 var data = codeReader.Decode(tex.GetPixels32(), tex.width, tex.height);
170 if (data != null)
171 return data.Text;
172 return "decode failed!";
173 }
174}
Es.InkPainter.Math Math
Definition: PaintTest.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
void StartWork()
start the work.
void StopWork()
Stops the work. when you need to leave current scene ,you must call this func firstly
int Width()
get the width of the camera
int Height()
get the height of the camera
Color[] GetPixels()
get the Pixels of the camera image
Definition: Loom.cs:10
static void RunAsync(Action a)
Definition: Loom.cs:85
void Reset()
Reset this scan param
void StopWork()
Stops the work.
void StartWork()
Stops the work.
DeviceCameraController e_DeviceController
static string DecodeByStaticPic(Texture2D tex)
Decodes the by static picture.
delegate void QRScanFinished(string str)