unity 調用手機攝像機 識別二維碼

來源:互聯網
上載者:User

標籤:unity   二維碼   

簡單的調用手機攝像 拍攝 然後識別二維碼 顯示二維碼的內容

需要匯入一個zxing.unity.dll檔案,現在這個指令碼的識別資料是放在Updata裡邊掃描的 資料量特別大會卡  要是用的話就自己做一下一秒執行一次。我這裡沒有弄

http://download.csdn.net/detail/chh19941125/8734149

代碼:

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 調用手機攝像機 識別二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.