I’ve been trying to get the ZXing.Net unity library to work with Unity…with no success so far. No matter how many bar codes I’ve put up in front of my webcam it doesn’t return any results. Has anyone had any success getting these two to play along? this is my filthy work in progress below:
public class CodeScanner : MonoBehaviour {
public WebCamTexture CamTexture;
private Thread CodeScannerThread;
public RawImage Image;
private Color32[] c;
private sbyte[] d;
private int W, H, WxH;
private int x, y, z;
private string DecryptedData;
public Quaternion baseRotation;
private Rect screenRect;
private bool isQuit;
public string LastResult;
private bool shouldEncodeNow;
public Text DebugT;
void OnEnable () {
if(CamTexture != null) {
CamTexture.Play();
W = CamTexture.width;
H = CamTexture.height;
WxH = W * H;
}
}
void OnDisable () {
if(CamTexture != null) {
CamTexture.Pause();
}
}
void OnDestroy () {
CodeScannerThread.Abort();
CamTexture.Stop();
}
void Awake () {
CamTexture = new WebCamTexture();
c = null;
OnEnable();
Image.texture = CamTexture;
//flip preview on iOS
#if UNITY_IOS
//Image.GetComponent<RectTransform>().localScale = new Vector3(1920f/1080f, -1080f/1920f, 0.0f);
#endif
baseRotation = transform.rotation;
CodeScannerThread = new Thread(DecodeQR);
CodeScannerThread.Start();
}
void Update () {
//float scaleY = CamTexture.videoVerticallyMirrored ? -1 : 1;
//Image.transform.localScale = new Vector3(W, scaleY * H, 0);
//transform.rotation = baseRotation * Quaternion.AngleAxis(CamTexture.videoRotationAngle, Vector3.back);
if(LastResult != null && LastResult.Trim().Length > 0)
{
DebugT.text = LastResult;
}
if (c == null)
{
Debug.Log ("Reading...");
CamTexture.GetPixels32(c);
}
}
void DecodeQR () {
var barcodeReader = new ZXing.BarcodeReader();
barcodeReader.AutoRotate = true;
barcodeReader.TryInverted = true;
barcodeReader.Options.TryHarder = true;
while(true) {
Debug.Log("Scanning...");
try {
if(c != null)
{
Debug.Log("Decoding");
Result decode = barcodeReader.Decode(c,W,H);
if (decode != null)
{
Debug.Log ("NOT NULL!");
LastResult = decode.Text;
shouldEncodeNow = true;
print(decode.Text);
}
}
// Sleep a little bit and set the signal to get the next frame
Thread.Sleep(200);
//c = null;
}
catch(Exception e) {
Debug.Log (e);
continue;
}
}
}
}
Found this call stack in xcode that might hint at why their are issues:
System.NullReferenceException: A null value was found where an object instance was required.
at UnityStandardAssets.CrossPlatformInput.TiltInput.OnDisable () [0x00000] in :0
at ZXing.PDF417.Internal.Detector.detect (Boolean multiple, ZXing.Common.BitMatrix bitMatrix) [0x00000] in :0
at ZXing.PDF417.Internal.Detector.detect (ZXing.BinaryBitmap image, IDictionary`2 hints, Boolean multiple) [0x00000] in :0
at ZXing.PDF417.PDF417Reader.decode (ZXing.BinaryBitmap image, IDictionary`2 hints, Boolean multiple) [0x00000] in :0
at ZXing.PDF417.PDF417Reader.decode (ZXing.BinaryBitmap image, IDictionary`2 hints) [0x00000] in :0
at DG.Tweening.Plugins.Core.PathCore.Path.Draw (DG.Tweening.Plugins.Core.PathCore.Path p) [0x00000] in :0
at ZXing.MultiFormatReader.decodeInternal (ZXing.BinaryBitmap image) [0x00000] in :0