Unity3d 打开本地摄像头扫描二维码

首先 下载ZXing.Net.0.12.0.0.zip,下载地址为http://zxingnet.codeplex.com/。

’解压找到unity文件夹,然后将其放到unity工程。

using UnityEngine;

using System.Collections;

using ZXing;

using UnityEngine.UI;

public class QRcode : MonoBehaviour

{

public Color32[] data;

private bool isScan;

public RawImage cameraTexture;

public Text txtQRcode;

private WebCamTexture webCameraTexture;

private BarcodeReader barcodeReader;

private float timer = 0;

IEnumerator Start()

{

barcodeReader = new BarcodeReader();

yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);

if (Application.HasUserAuthorization(UserAuthorization.WebCam))

{

WebCamDevice[] devices = WebCamTexture.devices;

string devicename = devices[0].name;

webCameraTexture = new WebCamTexture(devicename, 400, 300);

cameraTexture.texture = webCameraTexture;

webCameraTexture.Play();

isScan = true;

}

}

void Update()

{

if (isScan)

{

timer += Time.deltaTime;

if (timer > 0.5f) //0.5秒扫描一次

{

StartCoroutine(ScanQRcode());

timer = 0;

}

}

}

IEnumerator ScanQRcode()

{

data = webCameraTexture.GetPixels32();

DecodeQR(webCameraTexture.width, webCameraTexture.height);

yield return new WaitForEndOfFrame();

}

private void DecodeQR(int width, int height)

{

var br = barcodeReader.Decode(data, width, height);

if (br != null)

{

txtQRcode.text = br.Text;

isScan = false;

webCameraTexture.Stop();

}

}

}


注意::::http://www.cnblogs.com/laugher/p/5757759.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 项目中有一个需求是产品要通过激活码(二维码)来激活产品才能正常使用,之前为了快速推进项目一直是使用Metiao ...
    魁犸阅读 9,103评论 0 1
  • 二维码扫描最近两年简直是风靡移动互联网时代,尤其在国内发展神速。围绕条码扫码功能,首先说说通过本文你可以知道啥。一...
    55book阅读 9,661评论 0 1
  • Zxing已经是一个很成熟的框架了,但它是用maven构建的项目,在以gradle为基础的AS中集成起来总感觉不太...
    雾中的影子阅读 46,426评论 7 28
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,351评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,463评论 19 139