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
  {
  }
 }
 }
}

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

相关文章

  • 剖析设计模式编程中C#对于组合模式的运用

    剖析设计模式编程中C#对于组合模式的运用

    这篇文章主要介绍了设计模式编程中C#对于组合模式的运用,理论上来说组合模式包含抽象构件、树叶构件和树枝构件三个角色,需要的朋友可以参考下
    2016-02-02
  • C# AutoMapper 使用方法总结

    C# AutoMapper 使用方法总结

    这篇文章主要介绍了C# AutoMapper 使用方法,文中讲解非常细致,代码帮助大家更好的理解学习,感兴趣的朋友可以了解下
    2020-06-06
  • 浅谈二叉查找树的集合总结分析

    浅谈二叉查找树的集合总结分析

    本篇文章是谈二叉查找树进行了详细的总结分析,需要的朋友参考下
    2013-05-05
  • WinForm使用DecExpress控件中的ChartControl插件绘制图表

    WinForm使用DecExpress控件中的ChartControl插件绘制图表

    这篇文章介绍了WinForm使用DecExpress控件中的ChartControl插件绘制图表的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#实现windows form倒计时的方法

    C#实现windows form倒计时的方法

    这篇文章主要介绍了C#实现windows form倒计时的方法,涉及C#桌面程序设计中时间操作的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C# WINFORM自定义异常处理方法

    C# WINFORM自定义异常处理方法

    这篇文章主要介绍了一个简单的统一异常处理方法。系统底层出现异常,写入记录文件,系统顶层捕获底层异常,显示提示信息。需要的可以参考一下
    2021-12-12
  • 详解C# 托管资源和非托管资源

    详解C# 托管资源和非托管资源

    这篇文章主要介绍了详解C# 托管资源和非托管资源的相关资料,需要的朋友可以参考下
    2016-01-01
  • C#开发WinForm清空DataGridView控件绑定的数据

    C#开发WinForm清空DataGridView控件绑定的数据

    本文详细讲解了C#开发WinForm清空DataGridView控件绑定数据的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#操作配置文件app.config、web.config增删改

    C#操作配置文件app.config、web.config增删改

    这篇文章介绍了C#操作配置文件app.config、web.config增删改的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#实现改变DataGrid某一行和单元格颜色的方法

    C#实现改变DataGrid某一行和单元格颜色的方法

    这篇文章主要介绍了C#实现改变DataGrid某一行和单元格颜色的方法,主要涉及DataGrid控件的添加与使用、数据源的绑定、单元格与行的获取等操作。需要的朋友可以参考下
    2014-09-09

最新评论