经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
Unity调用手机摄像机识别二维码
来源:jb51  时间:2019/7/25 8:19:45  对本文有异议

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

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

下载地址:zxing.unity.dll

代码:

  1. using System.Threading;
  2. using UnityEngine;
  3. using ZXing;
  4. public class WebCameraScript : MonoBehaviour
  5. {
  6. public string LastResult;
  7. public string Lastresult;
  8. public Color32[] data;
  9. private bool isQuit;
  10. public GUITexture myCameraTexture;
  11. private WebCamTexture webCameraTexture;
  12. private void Start()
  13. {
  14. // bool success = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
  15. // Checks how many and which cameras are available on the device
  16. for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++)
  17. {
  18. // We want the back camera
  19. if (!WebCamTexture.devices[cameraIndex].isFrontFacing)
  20. {
  21. //webCameraTexture = new WebCamTexture(cameraIndex, Screen.width, Screen.height);
  22. webCameraTexture = new WebCamTexture(cameraIndex, 200, 200);
  23. // Here we flip the GuiTexture by applying a localScale transformation
  24. // works only in Landscape mode
  25. myCameraTexture.transform.localScale = new Vector3(1, 1, 1);
  26. }
  27. }
  28. // Here we tell that the texture of coming from the camera should be applied
  29. // to our GUITexture. As we have flipped it before the camera preview will have the
  30. // correct orientation
  31. myCameraTexture.texture = webCameraTexture;
  32. // Starts the camera
  33. webCameraTexture.Play();
  34. //enabled=WebCamTexture.s
  35. }
  36. public void ShowCamera()
  37. {
  38. myCameraTexture.guiTexture.enabled = true;
  39. webCameraTexture.Play();
  40. }
  41. public void HideCamera()
  42. {
  43. myCameraTexture.guiTexture.enabled = false;
  44. webCameraTexture.Stop();
  45. }
  46. private void OnGUI()
  47. {
  48. GUI.Label(new Rect(60, 30*1, Screen.width, 20), "LastResult:" + LastResult);
  49. if (GUI.Button(new Rect(0, 0, 100, 100), "ON/OFF"))
  50. {
  51. if (webCameraTexture.isPlaying)
  52. HideCamera();
  53. else
  54. ShowCamera();
  55. }
  56. }
  57. private void Update()
  58. {
  59. //data = new Color32[webCameraTexture.width * webCameraTexture.height];
  60. data = webCameraTexture.GetPixels32();
  61. DecodeQR(webCameraTexture.width, webCameraTexture.height);
  62. }
  63. private void DecodeQR(int W, int H)
  64. {
  65. if (isQuit)
  66. return;
  67. // create a reader with a custom luminance source
  68. var barcodeReader = new BarcodeReader {AutoRotate = true, TryHarder = true};
  69. // while (true)
  70. {
  71. try
  72. {
  73. // decode the current frame
  74. Result result = barcodeReader.Decode(data, W, H);
  75. if (result != null)
  76. {
  77. LastResult = result.Text;
  78. // shouldEncodeNow = true;
  79. print("i read out::" + result.Text);
  80. }
  81. // Sleep a little bit and set the signal to get the next frame
  82. Thread.Sleep(200);
  83. data = null;
  84. }
  85. catch
  86. {
  87. }
  88. }
  89. }
  90. }

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号