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í)行一次。我這里沒(méi)有弄
下載地址: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)文章
操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼
這篇文章主要介紹了操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼,有需要的朋友可以參考一下2013-11-11
C#中DataGridView導(dǎo)出Excel的兩種方法
這篇文章主要介紹了C#中DataGridView導(dǎo)出Excel的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
c#利用Session對(duì)象實(shí)現(xiàn)購(gòu)物車的方法示例
這篇文章主要介紹了c#利用Session對(duì)象實(shí)現(xiàn)購(gòu)物車的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
詳解WPF如何使用WriteableBitmap提升Image性能
這篇文章主要為大家詳細(xì)介紹了WPF如何使用WriteableBitmap提升Image性能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解
這篇文章主要介紹了C#導(dǎo)出網(wǎng)站功能實(shí)例代碼,需要的朋友可以參考下2015-10-10
C#基礎(chǔ)知識(shí)之GetType與typeof的區(qū)別小結(jié)
在比較對(duì)象時(shí),需要了解他們的類型,才能決定他們的值是否能比較。所有的類都從System.Object中繼承了GetType()方法,常常與typeo()運(yùn)算符一起使用。這篇文章主要給大家介紹了關(guān)于C#基礎(chǔ)知識(shí)之GetType與typeof區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-06-06
關(guān)于Unity中RectTransform與transform的區(qū)別
這篇文章主要介紹了Unity中RectTransform與transform的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
這篇文章主要介紹了C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,需要的朋友可以參考下2017-12-12
UpdateLayeredWindow實(shí)現(xiàn)任意異形窗口使用詳解
這篇文章主要為大家介紹了UpdateLayeredWindow實(shí)現(xiàn)任意異形窗口使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

