UNITY2021 开发安卓app 扫描一维二维条码
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?
?
全部代碼如下:
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; //獲取第一個攝像頭,用第一個攝像頭的畫面生成圖片信息 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 framevoid 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 updatevoid 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 framevoid 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=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+",");}*/}} }總結
以上是生活随笔為你收集整理的UNITY2021 开发安卓app 扫描一维二维条码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内核程序和应用程序
- 下一篇: vyos User Guide