Unity调用手机摄像机识别二维码

 更新时间:2019年07月24日 14:49:16   作者:C-h-h  
这篇文章主要为大家详细介绍了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
  {
  }
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 使用快捷键在Unity中快速锁定和解锁Inspector右上角的锁功能

    使用快捷键在Unity中快速锁定和解锁Inspector右上角的锁功能

    这篇文章主要为大家介绍了使用快捷键在Unity中快速锁定和解锁Inspector右上角的锁功能详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • C# 显示、隐藏窗口对应的任务栏

    C# 显示、隐藏窗口对应的任务栏

    WPF中全屏窗口,会自动隐藏任务栏,那非全屏窗口如何隐藏任务栏?甚至有没有一种场景,隐藏任务后自定义一套系统任务栏来显示?这篇文章主要介绍了C# 显示、隐藏窗口对应的任务栏,需要的朋友可以参考下
    2021-10-10
  • C#开发WPF程序中的弱事件模式

    C#开发WPF程序中的弱事件模式

    这篇文章介绍了C#开发WPF程序中的弱事件模式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • Log4Net 日志配置[附带源码下载]

    Log4Net 日志配置[附带源码下载]

    这篇文章主要介绍了Log4Net 日志配置[附带源码下载],需要的朋友可以参考下
    2015-05-05
  • C# Ini文件操作实例

    C# Ini文件操作实例

    这篇文章主要介绍了C# Ini文件操作实例,需要的朋友可以参考下
    2014-02-02
  • C#基础知识 全面解析可空类型

    C#基础知识 全面解析可空类型

    C# 2.0 中还引入了可空类型,可空类型也是值类型,只是可空类型是包括null的值类型的,下面就介绍下C#2.0中对可空类型的支持具体有哪些内容
    2012-11-11
  • jQuery uploadify在谷歌和火狐浏览器上传失败的解决方案

    jQuery uploadify在谷歌和火狐浏览器上传失败的解决方案

    jquery.uploadify插件是一个基于jquery来实现上传的,这个插件很好用,每一次向后台发送数据流请求时,ie会自动把本地cookie存储捆绑在一起发送给服务器。但firefox、chrome不会这样做,他们会认为这样不安全,下面介绍下jQuery uploadify上传失败的解决方案
    2015-08-08
  • C# TreeView无限目录树实现方法

    C# TreeView无限目录树实现方法

    这篇文章主要介绍了C# TreeView无限目录树实现方法,实例分析了TreeView节点操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • 使用c#在word文档中创建表格的方法详解

    使用c#在word文档中创建表格的方法详解

    本篇文章是对使用c#在word文档中创建表格的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C#使用Unity实现剪刀石头布游戏

    C#使用Unity实现剪刀石头布游戏

    这篇文章主要为大家详细介绍了C#语言使用Unity实现剪刀石头布游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01

最新评论