Unity調用手機攝像機識別二維碼
本文實現(xiàn)Unity調用手機攝像,拍攝,然后識別二維碼,顯示二維碼的內容。
需要導入一個zxing.unity.dll文件,現(xiàn)在這個腳本的識別數(shù)據(jù)是放在Updata里邊掃描的 數(shù)據(jù)量特別大會卡 要是用的話就自己做一下一秒執(zhí)行一次。我這里沒有弄
下載地址: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 { } } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼
這篇文章主要介紹了操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼,有需要的朋友可以參考一下2013-11-11詳解WPF如何使用WriteableBitmap提升Image性能
這篇文章主要為大家詳細介紹了WPF如何使用WriteableBitmap提升Image性能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-01-01C#基礎知識之GetType與typeof的區(qū)別小結
在比較對象時,需要了解他們的類型,才能決定他們的值是否能比較。所有的類都從System.Object中繼承了GetType()方法,常常與typeo()運算符一起使用。這篇文章主要給大家介紹了關于C#基礎知識之GetType與typeof區(qū)別的相關資料,需要的朋友可以參考下2021-06-06關于Unity中RectTransform與transform的區(qū)別
這篇文章主要介紹了Unity中RectTransform與transform的區(qū)別,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01C#使用ICSharpCode.SharpZipLib.dll進行文件的壓縮與解壓功能
這篇文章主要介紹了C#使用ICSharpCode.SharpZipLib.dll進行文件的壓縮與解壓功能,需要的朋友可以參考下2017-12-12UpdateLayeredWindow實現(xiàn)任意異形窗口使用詳解
這篇文章主要為大家介紹了UpdateLayeredWindow實現(xiàn)任意異形窗口使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09