unity Zxing二维码扫码器集成
Unity Zxing QR code scanner integration
我需要将Zxing 与vuforia 集成,在Unity 中制作二维码扫描应用程序吗?
我不知道如何在 unity.Can 中将 Zxing 与 Vuforia 集成,有人指导我如何做到这一点吗?我提前有 Zxing .dll 文件和 Vuforia unity package.Thanks。
我今天正在寻找在 Unity 中将 Zxing 与 vuforia 集成。
首先要做的是从以下网址下载 dll:https://zxingnet.codeplex.com/ 并将 unity dll 复制到您的 Plugins 文件夹(应该在 Assets 文件夹中)
然后,我设法找到了一些例子(其中一些已经过时了):
http://ydaira.blogspot.fr/2012/09/how-to-decode-qr-codes-using-unity3d.html
https://github.com/Redth/ZXing.Net/blob/master/Clients/VuforiaDemo/Assets/VuforiaScanner.cs
合并这些示例并简化它们后,我得到了这样的东西(放置在 ARCamera 预制件中):
using UnityEngine;
using System;
using System.Collections;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
[AddComponentMenu("System/VuforiaScanner")]
public class VuforiaScanner : MonoBehaviour
{
private bool cameraInitialized;
private BarcodeReader barCodeReader;
void Start()
{
barCodeReader = new BarcodeReader();
StartCoroutine(InitializeCamera());
}
private IEnumerator InitializeCamera()
{
// Waiting a little seem to avoid the Vuforia's crashes.
yield return new WaitForSeconds(1.25f);
var isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
Debug.Log(String.Format("FormatSet : {0}", isFrameFormatSet));
// Force autofocus.
var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
if (!isAutoFocus)
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
cameraInitialized = true;
}
private void Update()
{
if (cameraInitialized)
{
try
{
var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);
if (cameraFeed == null)
{
return;
}
var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
if (data != null)
{
// QRCode detected.
Debug.Log(data.Text);
}
else
{
Debug.Log("No QR code detected !");
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}
}
我设法让它在 AVD(Android 虚拟设备)中运行,因此它在真实设备上也能运行。
如果您使用 Unity 5.x 和 64 位 Windows,您可能会遇到错误
Failed to load Assets/Plugins/QCARWrapper.dll
解决方法很简单,如问题
中所述
要将 Vuforia 与 Unity 64 位一起使用,只需将 QCARWrapper DLL 从 /Plugins
移动到 /Plugins/x86.
这些是 DLL:
Select Unity Project 视图中的 QCARWrapper.bundle
(位于 Assets > Plugins 下),以便其设置显示在 Unity Inspector 中更改 QCARWrapper.bundle
在 Unity 检查器中从 Any Platform 到 Standalone + Editor。
比它更有魅力。
如果您在扫描过程中遇到延迟,此代码应该对您有所帮助。我用了KDelli的答案,用了另一个线程来解码qr的代码。
using UnityEngine;
using System;
using System.Collections;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
[AddComponentMenu("System/VuforiaCamera")]
public class VuforiaCamera : MonoBehaviour
{
private bool cameraInitialized;
private BarcodeReader barCodeReader;
private bool isDecoding = false;
void Start()
{
barCodeReader = new BarcodeReader();
StartCoroutine(InitializeCamera());
}
private IEnumerator InitializeCamera()
{
// Waiting a little seem to avoid the Vuforia's crashes.
yield return new WaitForSeconds(1.25f);
// var isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
// Debug.Log(String.Format("FormatSet : {0}", isFrameFormatSet));
// Force autofocus.
var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
if (!isAutoFocus)
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
cameraInitialized = true;
}
private void Update()
{
if (cameraInitialized && !isDecoding)
{
try
{
var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);
if (cameraFeed == null)
{
return;
}
ThreadPool.QueueUserWorkItem(new WaitCallback(DecodeQr), cameraFeed);
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}
private void DecodeQr(object state){
isDecoding = true;
var cameraFeed = (Image)state;
var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
if (data != null)
{
// QRCode detected.
isDecoding = false;
}
else
{
isDecoding = false;
Debug.Log("No QR code detected !");
}
}
}
我需要将Zxing 与vuforia 集成,在Unity 中制作二维码扫描应用程序吗? 我不知道如何在 unity.Can 中将 Zxing 与 Vuforia 集成,有人指导我如何做到这一点吗?我提前有 Zxing .dll 文件和 Vuforia unity package.Thanks。
我今天正在寻找在 Unity 中将 Zxing 与 vuforia 集成。
首先要做的是从以下网址下载 dll:https://zxingnet.codeplex.com/ 并将 unity dll 复制到您的 Plugins 文件夹(应该在 Assets 文件夹中)
然后,我设法找到了一些例子(其中一些已经过时了):
http://ydaira.blogspot.fr/2012/09/how-to-decode-qr-codes-using-unity3d.html
https://github.com/Redth/ZXing.Net/blob/master/Clients/VuforiaDemo/Assets/VuforiaScanner.cs
合并这些示例并简化它们后,我得到了这样的东西(放置在 ARCamera 预制件中):
using UnityEngine;
using System;
using System.Collections;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
[AddComponentMenu("System/VuforiaScanner")]
public class VuforiaScanner : MonoBehaviour
{
private bool cameraInitialized;
private BarcodeReader barCodeReader;
void Start()
{
barCodeReader = new BarcodeReader();
StartCoroutine(InitializeCamera());
}
private IEnumerator InitializeCamera()
{
// Waiting a little seem to avoid the Vuforia's crashes.
yield return new WaitForSeconds(1.25f);
var isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
Debug.Log(String.Format("FormatSet : {0}", isFrameFormatSet));
// Force autofocus.
var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
if (!isAutoFocus)
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
cameraInitialized = true;
}
private void Update()
{
if (cameraInitialized)
{
try
{
var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);
if (cameraFeed == null)
{
return;
}
var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
if (data != null)
{
// QRCode detected.
Debug.Log(data.Text);
}
else
{
Debug.Log("No QR code detected !");
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}
}
我设法让它在 AVD(Android 虚拟设备)中运行,因此它在真实设备上也能运行。
如果您使用 Unity 5.x 和 64 位 Windows,您可能会遇到错误
Failed to load Assets/Plugins/QCARWrapper.dll
解决方法很简单,如问题
要将 Vuforia 与 Unity 64 位一起使用,只需将 QCARWrapper DLL 从
/Plugins
移动到/Plugins/x86.
这些是 DLL:Select Unity Project 视图中的
QCARWrapper.bundle
(位于 Assets > Plugins 下),以便其设置显示在 Unity Inspector 中更改QCARWrapper.bundle
在 Unity 检查器中从 Any Platform 到 Standalone + Editor。
比它更有魅力。
如果您在扫描过程中遇到延迟,此代码应该对您有所帮助。我用了KDelli的答案,用了另一个线程来解码qr的代码。
using UnityEngine;
using System;
using System.Collections;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
[AddComponentMenu("System/VuforiaCamera")]
public class VuforiaCamera : MonoBehaviour
{
private bool cameraInitialized;
private BarcodeReader barCodeReader;
private bool isDecoding = false;
void Start()
{
barCodeReader = new BarcodeReader();
StartCoroutine(InitializeCamera());
}
private IEnumerator InitializeCamera()
{
// Waiting a little seem to avoid the Vuforia's crashes.
yield return new WaitForSeconds(1.25f);
// var isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
// Debug.Log(String.Format("FormatSet : {0}", isFrameFormatSet));
// Force autofocus.
var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
if (!isAutoFocus)
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
cameraInitialized = true;
}
private void Update()
{
if (cameraInitialized && !isDecoding)
{
try
{
var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);
if (cameraFeed == null)
{
return;
}
ThreadPool.QueueUserWorkItem(new WaitCallback(DecodeQr), cameraFeed);
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}
private void DecodeQr(object state){
isDecoding = true;
var cameraFeed = (Image)state;
var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
if (data != null)
{
// QRCode detected.
isDecoding = false;
}
else
{
isDecoding = false;
Debug.Log("No QR code detected !");
}
}
}