-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQrCodeScanner.cs
More file actions
158 lines (113 loc) · 4.55 KB
/
QrCodeScanner.cs
File metadata and controls
158 lines (113 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using Unity.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using ZXing;
using UnityEngine.AI;
using UnityEngine.UI;
using TMPro;
public class QrCodeScanner : MonoBehaviour
{
[SerializeField]
private ARSession session;
[SerializeField]
public Button scanButton;
[SerializeField]
public SetNavigationTarget obj;
[SerializeField]
private ARSessionOrigin sessionOrigin;
[SerializeField]
private ARCameraManager cameraManager;
[SerializeField]
public GameObject scanmessagedisplay;
[SerializeField]
private List<Target> navigationTargetObjects = new List<Target>();
public Texture2D cameraImageTexture;
private IBarcodeReader reader = new BarcodeReader(); // create a barcode reader instance
void Start()
{
scanButton.onClick.AddListener(ScanQRCode);
}
private void OnEnable()
{
cameraManager.frameReceived += OnCameraFrameReceived;
}
private void OnDisable()
{
cameraManager.frameReceived -= OnCameraFrameReceived;
}
private void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
{
}
private void ScanQRCode()
{
if (!cameraManager.TryAcquireLatestCpuImage(out XRCpuImage image))
{
StartCoroutine(obj.ShowMessagePopup("Scan successful"));
obj.EnableObjects();
scanButton.gameObject.SetActive(false);
scanmessagedisplay.gameObject.SetActive(false);
return;
}
var conversionParams = new XRCpuImage.ConversionParams
{
// Get the entire image.
inputRect = new RectInt(0, 0, image.width, image.height),
// Downsample by 2.
outputDimensions = new Vector2Int(image.width / 2, image.height / 2),
// Choose RGBA format.
outputFormat = TextureFormat.RGBA32,
// Flip across the vertical axis (mirror image).
transformation = XRCpuImage.Transformation.MirrorY
};
// See how many bytes you need to store the final image.
int size = image.GetConvertedDataSize(conversionParams);
// Allocate a buffer to store the image.
var buffer = new NativeArray<byte>(size, Allocator.Temp);
// Extract the image data
image.Convert(conversionParams, buffer);
// The image was converted to RGBA32 format and written into the provided buffer
// so you can dispose of the XRCpuImage. You must do this or it will leak resources.
image.Dispose();
// At this point, you can process the image, pass it to a computer vision algorithm, etc.
// In this example, you apply it to a texture to visualize it.
// You've got the data; let's put it into a texture so you can visualize it.
cameraImageTexture = new Texture2D(
conversionParams.outputDimensions.x,
conversionParams.outputDimensions.y,
conversionParams.outputFormat,
false);
cameraImageTexture.LoadRawTextureData(buffer);
cameraImageTexture.Apply();
// Done with your temporary data, so you can dispose it.
buffer.Dispose();
// Detect and decode the barcode inside the bitmap
var result = reader.Decode(cameraImageTexture.GetPixels32(), cameraImageTexture.width, cameraImageTexture.height);
// Do something with the result
if (result != null)
{
SetQrCodeRecenterTarget(result.Text);
}
else
{
StartCoroutine(obj.ShowMessagePopup("Scan not successful"));
}
}
private void SetQrCodeRecenterTarget(string targetText)
{
Target currentTarget = navigationTargetObjects.Find(x => x.Name.Equals(targetText));
if (currentTarget != null)
{
StartCoroutine(obj.ShowMessagePopup("Scan successful"));
obj.EnableObjects();
scanButton.gameObject.SetActive(false);
scanmessagedisplay.gameObject.SetActive(false);
// Reset position and rotation of ARSession
session.Reset();
// Add offset for recentering
sessionOrigin.transform.position = currentTarget.PositionObject.transform.position;
sessionOrigin.transform.rotation = currentTarget.PositionObject.transform.rotation;
}
}
}