UNITY2021 开发安卓app 扫描一维二维条码
2016年,我用PDA(WINCE)開(kāi)發(fā)了濰柴汽車物料倉(cāng)庫(kù)收貨程序,實(shí)現(xiàn)物料掃描數(shù)據(jù)直連SAP服務(wù)器。記得當(dāng)時(shí)PDA掃描一維碼時(shí)很快,掃描二維碼時(shí),明顯就要慢一些。
濰汽ERP系統(tǒng)PDA終端直連解決方案
2017年,胡總的需求使用終端直連SAP,實(shí)現(xiàn)驗(yàn)證總裝工位上的BOM準(zhǔn)確性,為L(zhǎng)ES配送糾正數(shù)據(jù),我用UNITY開(kāi)發(fā)了一個(gè)安卓APP。記得當(dāng)時(shí)使用手機(jī)掃描一維碼時(shí)是比較慢的,我的華為P7手機(jī)掃描一下VIN碼大概要2~3秒鐘,還總是掃不到。
濰汽ERP系統(tǒng)安卓終端直連解決方案
今天突然想到,這幾年微信掃描支付很便捷,可謂所見(jiàn)即所得,掃描的速度非???#xff0c;難道是手機(jī)硬件提高后情況改變了?打開(kāi)UNITY,寫了一個(gè)APP,用手機(jī)測(cè)試了一下,果然和微信的掃描速度一樣的秒掃。那手機(jī)硬件如此強(qiáng)大的今天,選擇條碼獲取的終端設(shè)備,還需要什么PDA啊?手機(jī)應(yīng)該足夠了。
做個(gè)筆記詳細(xì)記錄一下:
1、安裝UNITY 2021
登錄官網(wǎng)下載UNITY HUB,安裝選擇UNITY2021正式版本,安裝時(shí)把安卓的SDK和NDK一起安裝,很方便打一個(gè)勾就行,再不需要提前去安裝android studio了。
2、新建一個(gè)UNITY2D項(xiàng)目
在海爾啊騎里面,Main Camera主攝像機(jī)中添加一個(gè)Canvas桌布:
Canvas桌布的空間屬性中,選擇"Camera攝像機(jī)“,2D平面即桌布就是攝像機(jī)的視野。
注意紅色的箭頭,需要把攝像機(jī)拖到這里,就和桌布建立了關(guān)系。
在game視圖中設(shè)定分辨率,豎屏:
我們?cè)僭谧啦忌戏派蟁awImage,button,Text三個(gè)控件。
RawImage用來(lái)顯示手機(jī)攝像頭,button按一下就識(shí)別一下條碼,Text用來(lái)顯示條碼的文本內(nèi)容。
3、程序腳本
在桌布控件上,新建一個(gè)C#腳本,每一個(gè)物體都可以掛載腳本,我們這里給桌布掛一個(gè)就好。
使用腳本之前,還需要導(dǎo)入一個(gè)ZXING開(kāi)源的條碼掃描庫(kù)”zxing.unity.dll“看這名字就知道zxing專門為unity提供的:
我的網(wǎng)盤下載 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 updatevoid Start(){WebCamDevice[] tDevices = WebCamTexture.devices; //獲取所有攝像頭string tDeviceName = tDevices[0].name; //獲取第一個(gè)攝像頭,用第一個(gè)攝像頭的畫面生成圖片信息 m_webCameraTexture = new WebCamTexture(tDeviceName, (int)m_RawImage.rectTransform.rect.width, (int)m_RawImage.rectTransform.rect.height);//名字,寬,高m_webCameraTexture.Play(); //開(kāi)始實(shí)時(shí)顯示m_RawImage.texture = m_webCameraTexture; //賦值圖片信息 }public void CheckQRCode(){m_Text.text = "Scan......";Color32[] m_colorData = m_webCameraTexture.GetPixels32(); //存儲(chǔ)攝像頭畫面的顏色數(shù)組 BarcodeReader m_barcodeRender = new BarcodeReader(); //二維碼的變量 var s = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);//將畫面中的二維碼信息檢索出來(lái)m_Text.text = s.Text;}// Update is called once per framevoid Update(){ } }腳本中出現(xiàn)了public全局變量后,在桌布的屬性節(jié)目中,找到腳本界面屬性,發(fā)現(xiàn)全局變量還需要和UI控件關(guān)聯(lián)上,方法也是把控件拖上去,就和UI上的控件關(guān)聯(lián)上了:
4、解決一個(gè) UNITY zxing 豎屏掃描攝像頭畫面都是旋轉(zhuǎn)了90度的問(wèn)題:
網(wǎng)上的例子,大都在問(wèn)這個(gè)問(wèn)題,我已經(jīng)解決了,把RawImage控件,旋轉(zhuǎn)屬性中,設(shè)置-90度即可:
5、編譯UNITY APP
打開(kāi)手機(jī)的USB調(diào)試模式,用USB數(shù)據(jù)線連接電腦與手機(jī),UNITY中切換平臺(tái)至android,直接編譯運(yùn)行即可:
------------2021.7.8-----掃碼上傳服務(wù)器---------------
手機(jī)上掃到條碼數(shù)據(jù)后,我還可以上傳到自己的web服務(wù)器上:
一、UNITY增加上傳服務(wù)器的全部代碼:
使用HTTP GET把條碼數(shù)據(jù)傳給服務(wù)器
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 updatevoid Start(){WebCamDevice[] tDevices = WebCamTexture.devices; //獲取所有攝像頭string tDeviceName = tDevices[0].name; //獲取第一個(gè)攝像頭,用第一個(gè)攝像頭的畫面生成圖片信息 m_webCameraTexture = new WebCamTexture(tDeviceName, (int)m_RawImage.rectTransform.rect.width, (int)m_RawImage.rectTransform.rect.height);//名字,寬,高m_webCameraTexture.Play(); //開(kāi)始實(shí)時(shí)顯示m_RawImage.texture = m_webCameraTexture; //賦值圖片信息}// Update is called once per framevoid Update(){ }public void OnMyClickButton(){m_Text.text = "Scan......";Color32[] m_colorData = m_webCameraTexture.GetPixels32(); //存儲(chǔ)攝像頭畫面的顏色數(shù)組 BarcodeReader m_barcodeRender = new BarcodeReader(); //二維碼的變量 var s = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);//將畫面中的二維碼信息檢索出來(lái)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請(qǐng)求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服務(wù)器接收的全部.net代碼:
當(dāng)然,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=fdafgagfdagafd789078906fadgdafg6598string username = Request["username"];string password = Request["password"];string ordno = Request["ordno"];var someone = (from t in po.db.BA_USERwhere(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_ROLEfrom t in po.db.RE_ROLE_TCODEwhere(w.ZUSER.Equals(username) && w.ROLE.Equals(t.ROLE) )select new{ t.TCODE};foreach (var one in some){ Response.Write(one.TCODE+",");}*/}} }總結(jié)
以上是生活随笔為你收集整理的UNITY2021 开发安卓app 扫描一维二维条码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 内核程序和应用程序
- 下一篇: vyos User Guide