当前位置:   article > 正文

UNITY2021 开发安卓app 扫描一维二维条码_unity usb扫码录入

unity usb扫码录入

2016年,我用PDA(WINCE)开发了潍柴汽车物料仓库收货程序,实现物料扫描数据直连SAP服务器。记得当时PDA扫描一维码时很快,扫描二维码时,明显就要慢一些。

潍汽ERP系统PDA终端直连解决方案

2017年,胡总的需求使用终端直连SAP,实现验证总装工位上的BOM准确性,为LES配送纠正数据,我用UNITY开发了一个安卓APP。记得当时使用手机扫描一维码时是比较慢的,我的华为P7手机扫描一下VIN码大概要2~3秒钟,还总是扫不到。

潍汽ERP系统安卓终端直连解决方案

今天突然想到,这几年微信扫描支付很便捷,可谓所见即所得,扫描的速度非常快,难道是手机硬件提高后情况改变了?打开UNITY,写了一个APP,用手机测试了一下,果然和微信的扫描速度一样的秒扫。那手机硬件如此强大的今天,选择条码获取的终端设备,还需要什么PDA啊?手机应该足够了。

做个笔记详细记录一下:

1、安装UNITY 2021

登录官网下载UNITY HUB,安装选择UNITY2021正式版本,安装时把安卓的SDK和NDK一起安装,很方便打一个勾就行,再不需要提前去安装android studio了。

2、新建一个UNITY2D项目

在海尔啊骑里面,Main Camera主摄像机中添加一个Canvas桌布:

Canvas桌布的空间属性中,选择"Camera摄像机“,2D平面即桌布就是摄像机的视野。

注意红色的箭头,需要把摄像机拖到这里,就和桌布建立了关系。

在game视图中设定分辨率,竖屏:

我们再在桌布上放上RawImage,button,Text三个控件。

RawImage用来显示手机摄像头,button按一下就识别一下条码,Text用来显示条码的文本内容。

3、程序脚本

在桌布控件上,新建一个C#脚本,每一个物体都可以挂载脚本,我们这里给桌布挂一个就好。

使用脚本之前,还需要导入一个ZXING开源的条码扫描库”zxing.unity.dll“看这名字就知道zxing专门为unity提供的:

我的网盘下载 zxing.unity.dll   版本是16.6

链接:https://pan.baidu.com/s/1wAcXdRcjloD6MWFouA0YCA 
提取码:bkjs 
 

全部代码如下:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using ZXing;
  6. using ZXing.QrCode;
  7. public class NewBehaviourScript : MonoBehaviour
  8. {
  9. public RawImage m_RawImage;
  10. public Text m_Text;
  11. private WebCamTexture m_webCameraTexture;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
  16. string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
  17. m_webCameraTexture = new WebCamTexture(tDeviceName, (int)m_RawImage.rectTransform.rect.width, (int)m_RawImage.rectTransform.rect.height);//名字,宽,高
  18. m_webCameraTexture.Play(); //开始实时显示
  19. m_RawImage.texture = m_webCameraTexture; //赋值图片信息
  20. }
  21. public void CheckQRCode()
  22. {
  23. m_Text.text = "Scan......";
  24. Color32[] m_colorData = m_webCameraTexture.GetPixels32(); //存储摄像头画面的颜色数组
  25. BarcodeReader m_barcodeRender = new BarcodeReader(); //二维码的变量
  26. var s = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);//将画面中的二维码信息检索出来
  27. m_Text.text = s.Text;
  28. }
  29. // Update is called once per frame
  30. void Update()
  31. {
  32. }
  33. }

脚本中出现了public全局变量后,在桌布的属性节目中,找到脚本界面属性,发现全局变量还需要和UI控件关联上,方法也是把控件拖上去,就和UI上的控件关联上了:

4、解决一个 UNITY zxing 竖屏扫描摄像头画面都是旋转了90度的问题:

网上的例子,大都在问这个问题,我已经解决了,把RawImage控件,旋转属性中,设置-90度即可:

5、编译UNITY APP

打开手机的USB调试模式,用USB数据线连接电脑与手机,UNITY中切换平台至android,直接编译运行即可:

------------2021.7.8-----扫码上传服务器---------------

手机上扫到条码数据后,我还可以上传到自己的web服务器上:

一、UNITY增加上传服务器的全部代码:

使用HTTP GET把条码数据传给服务器

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using ZXing;
  9. using ZXing.QrCode;
  10. public class NewBehaviourScript : MonoBehaviour
  11. {
  12. public RawImage m_RawImage;
  13. public Text m_Text;
  14. private WebCamTexture m_webCameraTexture;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
  19. string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
  20. m_webCameraTexture = new WebCamTexture(tDeviceName, (int)m_RawImage.rectTransform.rect.width, (int)m_RawImage.rectTransform.rect.height);//名字,宽,高
  21. m_webCameraTexture.Play(); //开始实时显示
  22. m_RawImage.texture = m_webCameraTexture; //赋值图片信息
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. }
  28. public void OnMyClickButton()
  29. {
  30. m_Text.text = "Scan......";
  31. Color32[] m_colorData = m_webCameraTexture.GetPixels32(); //存储摄像头画面的颜色数组
  32. BarcodeReader m_barcodeRender = new BarcodeReader(); //二维码的变量
  33. var s = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);//将画面中的二维码信息检索出来
  34. m_Text.text = s.Text;
  35. string url = "http://esb.baonengmotor.com/GetClientPost.aspx";
  36. string postDataStr = "username=liuxin&password=123456&ordno="+ s.Text;
  37. string result = HttpGet(url, postDataStr);
  38. }
  39. //用于http get请求
  40. public static string HttpGet(string Url, string postDataStr)
  41. {
  42. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
  43. request.Method = "GET";
  44. request.ContentType = "text/html;charset=UTF-8";
  45. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  46. Stream myResponseStream = response.GetResponseStream();
  47. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
  48. string retString = myStreamReader.ReadToEnd();
  49. myStreamReader.Close();
  50. myResponseStream.Close();
  51. return retString;
  52. }
  53. }

二、web服务器接收的全部.net代码:

当然,web这还是使用了EF6和FineUI框架,

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace WMS
  8. {
  9. public partial class GetClientPost : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. //http://esb.baonengmotor.com/GetClientPost.aspx?username=liuxin&password=123456&ordno=fdafgagfdagafd789078906fadgdafg6598
  14. //http://localhost:12345/GetClientPost.aspx?username=liuxin&password=123456&ordno=fdafgagfdagafd789078906fadgdafg6598
  15. string username = Request["username"];
  16. string password = Request["password"];
  17. string ordno = Request["ordno"];
  18. var someone = (from t in po.db.BA_USER
  19. where
  20. (
  21. t.ZUSER.Equals(username) &&
  22. t.PASSWORD.Equals(password)
  23. )
  24. select t).FirstOrDefault();
  25. if (someone == null) { Response.Write("user or pass error."); return; }
  26. JK_MOM_MileStoneReport one = new JK_MOM_MileStoneReport();
  27. one.OSBID = username;
  28. one.SERNO = username;
  29. one.ORDNO = ordno;
  30. one.ZPASS_S = DateTime.Now.Date;
  31. one.ZPASS_T = DateTime.Now.TimeOfDay;
  32. po.pdadb.JK_MOM_MileStoneReport.Add(one);
  33. po.pdadb.SaveChanges();
  34. Response.Write("DataSaved.");
  35. /*
  36. var some = from w in po.db.RE_USER_ROLE
  37. from t in po.db.RE_ROLE_TCODE
  38. where
  39. (
  40. w.ZUSER.Equals(username) &&
  41. w.ROLE.Equals(t.ROLE)
  42. )
  43. select new
  44. {
  45. t.TCODE
  46. };
  47. foreach (var one in some)
  48. {
  49. Response.Write(one.TCODE+",");
  50. }
  51. */
  52. }
  53. }
  54. }

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

闽ICP备14008679号