溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼

發(fā)布時(shí)間:2020-09-01 07:54:07 來源:腳本之家 閱讀:170 作者:C-h-h 欄目:編程語言

本文實(shí)現(xiàn)Unity調(diào)用手機(jī)攝像,拍攝,然后識(shí)別二維碼,顯示二維碼的內(nèi)容。

需要導(dǎo)入一個(gè)zxing.unity.dll文件,現(xiàn)在這個(gè)腳本的識(shí)別數(shù)據(jù)是放在Updata里邊掃描的 數(shù)據(jù)量特別大會(huì)卡  要是用的話就自己做一下一秒執(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
  {
  }
 }
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI