Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
剖析設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用
這篇文章主要介紹了設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用,理論上來說組合模式包含抽象構(gòu)件、樹葉構(gòu)件和樹枝構(gòu)件三個(gè)角色,需要的朋友可以參考下2016-02-02
WinForm使用DecExpress控件中的ChartControl插件繪制圖表
這篇文章介紹了WinForm使用DecExpress控件中的ChartControl插件繪制圖表的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)windows form倒計(jì)時(shí)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)windows form倒計(jì)時(shí)的方法,涉及C#桌面程序設(shè)計(jì)中時(shí)間操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#開發(fā)WinForm清空DataGridView控件綁定的數(shù)據(jù)
本文詳細(xì)講解了C#開發(fā)WinForm清空DataGridView控件綁定數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
C#操作配置文件app.config、web.config增刪改
這篇文章介紹了C#操作配置文件app.config、web.config增刪改的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)改變DataGrid某一行和單元格顏色的方法
這篇文章主要介紹了C#實(shí)現(xiàn)改變DataGrid某一行和單元格顏色的方法,主要涉及DataGrid控件的添加與使用、數(shù)據(jù)源的綁定、單元格與行的獲取等操作。需要的朋友可以參考下2014-09-09

