Unity调用手机摄像机识别二维码
作者:C-h-h 发布时间:2023-05-18 23:56:36
标签:Unity,摄像机,二维码
本文实现Unity调用手机摄像,拍摄,然后识别二维码,显示二维码的内容。
需要导入一个zxing.unity.dll文件,现在这个脚本的识别数据是放在Updata里边扫描的 数据量特别大会卡 要是用的话就自己做一下一秒执行一次。我这里没有弄
下载地址: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
{
}
}
}
}
来源:https://blog.csdn.net/chh19941125/article/details/45953725


猜你喜欢
- 本文实例为大家分享了unity实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下首先创建一个头部,编写脚本利用WASD控制头部的移动。Vec
- Spring流程Debug1.1 Spring测试环境搭建Spring模块概览,绿色是模块,Spring中八大模块,黑色表示该模块包含的ja
- 前言我们知道在Android开发中不能在非ui线程中更新ui,但是,有的时候我们需要在代码中执行一些诸如访问网络、查询数据库等耗时操作,为了
- 上一篇博文《Android中Handler使用浅析》通过实现倒计时闪屏页面的制作引出了Handler的使用方法以及实现原理,博文末尾也提到了
- Unity中的PostProcessBuild:深入解析与实用案例在Unity游戏开发中,我们经常需要在构建完成后对生成的应用程序进行一些额
- 应用开发的时候,有时我们需要将一些图片进行预览,例如:相片管理的应用。这个时候用ListView的话就显得不是太合适了,因为Li
- 需求说明实现方式嗯 这个可以视作一个经典的消费者和生产者的问题,详细见代码注释代码内容 消费者,负责取走生产者产生的信息/** * @aut
- 一、网络爬虫的基本知识网络爬虫通过遍历互联网络,把网络中的相关网页全部抓取过来,这体现了爬的概念。爬虫如何遍历网络呢,互联网可以看做是一张大
- 什么原因使我们不得不使用线程池?个人认为主要原因是:短时间内需要处理的任务数量很多使用线程池的好处:1.减少在创建和销毁线程上所花的时间以及
- Android的硬件抽象层,简
- 概念:有enum关键字修饰的类,成为枚举类1、枚举规则枚举类的对象可以有类里面定义,不支持重新new出来,枚举类有构造函数,其他的类都一样,
- 这里简单介绍了一些常用的属性,以及一些术语的解释和举例说明,不太全面,希望读者多多补充。1.重载:函数名相同,参数的个数或参数类型不同; p
- 1.实现一个ItsClient 客户端用来实例化调用验证功能public class ItsClient {private static f
- 1. 文件上传a. 看看@FIEL注解的属性/** * 上传文件时使用该注解 设置文件相关参数 */@Retention(Retention
- 目录一.数组的基本概念二.数组的声明三.数组的创建及初始化1.数组的创建2.数组的初始化四.访问数组元素五.for each 循环六.数组的
- 本文实例为大家分享了java web个人通讯录系统的具体代码,供大家参考,具体内容如下现在开始上截图:下面粘贴代码:首先是目录结构:<
- 在Android开发中,Animation是用来给控件制作效果的。大多数的控件都可以用这个类,这个类包含了4种基本动作,分别为移动,旋转,淡
- SpringMVC获取表单数据1、实体类package cn.hadron.bean;import java.io.Serializable
- 本文实例为大家分享了JavaFX实现简单日历效果的具体代码,供大家参考,具体内容如下1.先看效果:2.代码:1)ClockEdge.java
- 首先假设你的应用程序配置文件如下:<?xml version="1.0" encoding="utf-8