服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity调用手机摄像机识别二维码

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

2022-07-29 10:24C-h-h C#

这篇文章主要为大家详细介绍了Unity调用手机摄像机识别二维码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实现Unity调用手机摄像,拍摄,然后识别二维码,显示二维码的内容。

需要导入一个zxing.unity.dll文件,现在这个脚本的识别数据是放在Updata里边扫描的 数据量特别大会卡  要是用的话就自己做一下一秒执行一次。我这里没有弄

下载地址:zxing.unity.dll

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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

延伸 · 阅读

精彩推荐
  • C#C#中Dictionary泛型集合7种常见的用法

    C#中Dictionary泛型集合7种常见的用法

    本文主要介绍了Dictionary集合的7种最基础的用法,包括创建、添加、查找、遍历、删除等方法,程序都是由简入繁,希望能通过阅读简单的示例,给大家一...

    jae3892021-11-17
  • C#Unity3D实现虚拟按钮控制人物移动效果

    Unity3D实现虚拟按钮控制人物移动效果

    这篇文章主要为大家详细介绍了Unity3D实现虚拟按钮控制人物移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    shenqingyu06052023246992022-03-11
  • C#C#动态编译并执行字符串样例

    C#动态编译并执行字符串样例

    这篇文章主要为大家详细介绍了C#动态编译并执行字符串样例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    王宝会8382022-01-12
  • C#C#强制类型转换小结

    C#强制类型转换小结

    任何一门编程语言均有相关数据类型。C#也不例外,不过转换过程要注意小类型能转换成大类型,但大类型一般不能转换成小类型,下面小编给大家详解C...

    Amedeo6352022-01-12
  • C#Winform控件Picture实现图片拖拽显示效果

    Winform控件Picture实现图片拖拽显示效果

    这篇文章主要为大家详细介绍了Winform控件Picture实现图片拖拽显示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    喜欢吃鱼的青年4062022-03-01
  • C#c# 两个数组比较,将重复部分去掉,返回不重复部分的实现

    c# 两个数组比较,将重复部分去掉,返回不重复部分的实现

    下面小编就为大家带来一篇c# 两个数组比较,将重复部分去掉,返回不重复部分的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一...

    C#教程网10332021-12-14
  • C#C# RichTextBox制作文本编辑器

    C# RichTextBox制作文本编辑器

    这篇文章主要为大家详细介绍了C# RichTextBox制作文本编辑器的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    飞翔的月亮4052021-12-31
  • C#C#编程总结(一)序列化总结

    C#编程总结(一)序列化总结

    本篇主要介绍了C#序列化总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    停留的风7022021-12-11