Unity调用手机摄像机识别二维码

Danica ·
更新时间:2024-11-14
· 805 次阅读

本文实现Unity调用手机摄像,拍摄,然后识别二维码,显示二维码的内容。

需要导入一个zxing.unity.dll文件,现在这个脚本的识别数据是放在Updata里边扫描的 数据量特别大会卡  要是用的话就自己做一下一秒执行一次。我这里没有弄

下载地址:zxing.unity.dll

代码:

using System.Threading; using UnityEngine; using ZXing; public class WebCameraScript : MonoBehaviour { public string LastResult; public string Lastresult; public Color32[] data; private bool isQuit; public GUITexture myCameraTexture; private WebCamTexture webCameraTexture; private void Start() { // bool success = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); // Checks how many and which cameras are available on the device for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) { // We want the back camera if (!WebCamTexture.devices[cameraIndex].isFrontFacing) { //webCameraTexture = new WebCamTexture(cameraIndex, Screen.width, Screen.height); webCameraTexture = new WebCamTexture(cameraIndex, 200, 200); // Here we flip the GuiTexture by applying a localScale transformation // works only in Landscape mode myCameraTexture.transform.localScale = new Vector3(1, 1, 1); } } // Here we tell that the texture of coming from the camera should be applied // to our GUITexture. As we have flipped it before the camera preview will have the // correct orientation myCameraTexture.texture = webCameraTexture; // Starts the camera webCameraTexture.Play(); //enabled=WebCamTexture.s } public void ShowCamera() { myCameraTexture.guiTexture.enabled = true; webCameraTexture.Play(); } public void HideCamera() { myCameraTexture.guiTexture.enabled = false; webCameraTexture.Stop(); } private void OnGUI() { GUI.Label(new Rect(60, 30*1, Screen.width, 20), "LastResult:" + LastResult); if (GUI.Button(new Rect(0, 0, 100, 100), "ON/OFF")) { if (webCameraTexture.isPlaying) HideCamera(); else ShowCamera(); } } private void Update() { //data = new Color32[webCameraTexture.width * webCameraTexture.height]; data = webCameraTexture.GetPixels32(); DecodeQR(webCameraTexture.width, webCameraTexture.height); } private void DecodeQR(int W, int H) { if (isQuit) return; // create a reader with a custom luminance source var barcodeReader = new BarcodeReader {AutoRotate = true, TryHarder = true}; // while (true) { try { // decode the current frame Result result = barcodeReader.Decode(data, W, H); if (result != null) { LastResult = result.Text; // shouldEncodeNow = true; print("i read out::" + result.Text); } // Sleep a little bit and set the signal to get the next frame Thread.Sleep(200); data = null; } catch { } } } } 您可能感兴趣的文章:Unity使用摄像机实现望远镜效果Unity3D实现控制摄像机移动unity3D实现摄像机抖动特效



摄像 摄像机 二维 二维码 unity 手机

需要 登录 后方可回复, 如果你还没有账号请 注册新账号