当前位置:   article > 正文

unity Vuforia + 二维码解析 使用同一个摄像机_vuforia扫描二维码深度如何判断

vuforia扫描二维码深度如何判断

unity中,使用Vuforia并且实现同一相机扫描二维码。网上有几个教程,但是会遇到当应用被暂停以后二维码扫描不正确,主要原因来自于Vuforia的一个问题。查了很久才解决,在此记录,希望能帮到遇到同样问题的同学。

  1. using UnityEngine;
  2. using System.Collections;
  3. using Vuforia;
  4. using ZXing;
  5. using ZXing.Client.Result;
  6. public class ScanCodeBehaviour : MonoBehaviour {
  7. VuforiaBehaviour vuforia;
  8. bool decoding;
  9. bool init;
  10. BarcodeReader barcodeReader = new BarcodeReader();
  11. bool posting;
  12. void Awake()
  13. {
  14. vuforia = this.GetComponent<VuforiaBehaviour>();
  15. vuforia.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
  16. }
  17. // Use this for initialization
  18. void Start () {
  19. }
  20. void Update()
  21. {
  22. if (vuforia == null)
  23. return;
  24. if (vuforia.enabled && !init)
  25. {
  26. //Wait 1/4 seconds for the device to initialize (otherwise it seems to crash sometimes)
  27. init = true;
  28. Loom.QueueOnMainThread(() =>
  29. {
  30. //这里必须先设置false,再设置true!!!
  31. CameraDevice.Instance.SetFrameFormat(Vuforia.Image.PIXEL_FORMAT.RGB888, false);
  32. init = CameraDevice.Instance.SetFrameFormat(Vuforia.Image.PIXEL_FORMAT.RGB888, true);
  33. posting = false;
  34. }, 0.25f);
  35. }
  36. }
  37. void OnApplicationFocus(bool value)
  38. {
  39. if (value == false)
  40. {
  41. }
  42. else
  43. {
  44. //失去焦点后需要重新init
  45. init = false;
  46. }
  47. }
  48. public void Reinit()
  49. {
  50. init = false;
  51. }
  52. // Update is called once per frame
  53. void OnTrackablesUpdated() {
  54. if (vuforia.enabled && CameraDevice.Instance != null && !decoding && !posting)
  55. {
  56. Vuforia.Image image = CameraDevice.Instance.GetCameraImage(Vuforia.Image.PIXEL_FORMAT.RGB888);
  57. if (image != null)
  58. {
  59. decoding = true;
  60. Loom.RunAsync(() =>
  61. {
  62. try
  63. {
  64. Result data = barcodeReader.Decode(image.Pixels, image.BufferWidth, image.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
  65. if (data != null)
  66. {
  67. Loom.QueueOnMainThread(() =>
  68. {
  69. Debug.LogError(data.Text);
  70. });
  71. }
  72. }
  73. finally
  74. {
  75. decoding = false;
  76. }
  77. });
  78. }
  79. }
  80. }
  81. }

重点就两点,一点是应用失去焦点返回后需要重新init,第二是,重新init的时候需要把原有的format设置成false,不然不可用。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/715902
推荐阅读
相关标签
  

闽ICP备14008679号