2016年,我用PDA(WINCE)开发了潍柴汽车物料仓库收货程序,实现物料扫描数据直连SAP服务器。记得当时PDA扫描一维码时很快,扫描二维码时,明显就要慢一些。
2017年,胡总的需求使用终端直连SAP,实现验证总装工位上的BOM准确性,为LES配送纠正数据,我用UNITY开发了一个安卓APP。记得当时使用手机扫描一维码时是比较慢的,我的华为P7手机扫描一下VIN码大概要2~3秒钟,还总是扫不到。
今天突然想到,这几年微信扫描支付很便捷,可谓所见即所得,扫描的速度非常快,难道是手机硬件提高后情况改变了?打开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
全部代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.QrCode;
public class NewBehaviourScript : MonoBehaviour
{
public RawImage m_RawImage;
public Text m_Text;
private WebCamTexture m_webCameraTexture;
// Start is called before the first frame update
void Start()
{
WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
m_webCameraTexture = new WebCamTexture(tDeviceName, (int)m_RawImage.rectTransform.rect.width, (int)m_RawImage.rectTransform.rect.height);//名字,宽,高
m_webCameraTexture.Play(); //开始实时显示
m_RawImage.texture = m_webCameraTexture; //赋值图片信息
}
public void CheckQRCode()
{
m_Text.text = "Scan......";
Color32[] m_colorData = m_webCameraTexture.GetPixels32(); //存储摄像头画面的颜色数组
BarcodeReader m_barcodeRender = new BarcodeReader(); //二维码的变量
var s = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);//将画面中的二维码信息检索出来
m_Text.text = s.Text;
}
// Update is called once per frame
void Update()
{
}
}
脚本中出现了public全局变量后,在桌布的属性节目中,找到脚本界面属性,发现全局变量还需要和UI控件关联上,方法也是把控件拖上去,就和UI上的控件关联上了:
4、解决一个 UNITY zxing 竖屏扫描摄像头画面都是旋转了90度的问题:
网上的例子,大都在问这个问题,我已经解决了,把RawImage控件,旋转属性中,设置-90度即可:
5、编译UNITY APP
打开手机的USB调试模式,用USB数据线连接电脑与手机,UNITY中切换平台至android,直接编译运行即可:
------------2021.7.8-----扫码上传服务器---------------
手机上扫到条码数据后,我还可以上传到自己的web服务器上:
一、UNITY增加上传服务器的全部代码:
使用HTTP GET把条码数据传给服务器
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.QrCode;
public class NewBehaviourScript : MonoBehaviour
{
public RawImage m_RawImage;
public Text m_Text;
private WebCamTexture m_webCameraTexture;
// Start is called before the first frame update
void Start()
{
WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
m_webCameraTexture = new WebCamTexture(tDeviceName, (int)m_RawImage.rectTransform.rect.width, (int)m_RawImage.rectTransform.rect.height);//名字,宽,高
m_webCameraTexture.Play(); //开始实时显示
m_RawImage.texture = m_webCameraTexture; //赋值图片信息
}
// Update is called once per frame
void Update()
{
}
public void OnMyClickButton()
{
m_Text.text = "Scan......";
Color32[] m_colorData = m_webCameraTexture.GetPixels32(); //存储摄像头画面的颜色数组
BarcodeReader m_barcodeRender = new BarcodeReader(); //二维码的变量
var s = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);//将画面中的二维码信息检索出来
m_Text.text = s.Text;
string url = "http://esb.baonengmotor.com/GetClientPost.aspx";
string postDataStr = "username=liuxin&password=123456&ordno="+ s.Text;
string result = HttpGet(url, postDataStr);
}
//用于http get请求
public static string HttpGet(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}
二、web服务器接收的全部.net代码:
当然,web这还是使用了EF6和FineUI框架,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WMS
{
public partial class GetClientPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//http://esb.baonengmotor.com/GetClientPost.aspx?username=liuxin&password=123456&ordno=fdafgagfdagafd789078906fadgdafg6598
//http://localhost:12345/GetClientPost.aspx?username=liuxin&password=123456&ordno=fdafgagfdagafd789078906fadgdafg6598
string username = Request["username"];
string password = Request["password"];
string ordno = Request["ordno"];
var someone = (from t in po.db.BA_USER
where
(
t.ZUSER.Equals(username) &&
t.PASSWORD.Equals(password)
)
select t).FirstOrDefault();
if (someone == null) { Response.Write("user or pass error."); return; }
JK_MOM_MileStoneReport one = new JK_MOM_MileStoneReport();
one.OSBID = username;
one.SERNO = username;
one.ORDNO = ordno;
one.ZPASS_S = DateTime.Now.Date;
one.ZPASS_T = DateTime.Now.TimeOfDay;
po.pdadb.JK_MOM_MileStoneReport.Add(one);
po.pdadb.SaveChanges();
Response.Write("DataSaved.");
/*
var some = from w in po.db.RE_USER_ROLE
from t in po.db.RE_ROLE_TCODE
where
(
w.ZUSER.Equals(username) &&
w.ROLE.Equals(t.ROLE)
)
select new
{
t.TCODE
};
foreach (var one in some)
{
Response.Write(one.TCODE+",");
}
*/
}
}
}