欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-09-03 18:01:57
本文实现unity调用手机摄像,拍摄,然后识别二维码,显示二维码的内容。 需要导入一个zxing.unity.dll文件,现在这个脚本的识别数据是放在updata里边扫描...

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

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

下载地址:

代码:

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

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