首页 / 二维码 / 扫码枪二维码一般格式解析Demo
扫码枪二维码一般格式解析Demo
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了扫码枪二维码一般格式解析Demo,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含14710字,纯文字阅读大概需要22分钟。
内容图文
1、背景,测试Demo如下图所示
1.1、单击网格,文本框获取选中的二维码;
1.2、点击【OK】按钮根据条件解析字符串,呈现于列表框中;
2、测试数据类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Threading.Tasks; namespace Test.Barcode { #region Model publicclass Sample { public Sample() { } privatestring strSN = string.Empty; privatestring strPKGID = string.Empty; privatestring strBarcode = string.Empty; publicstring SN { get { returnthis.strSN; } set { this.strSN = value; } } publicstring PKGID { get { returnthis.strPKGID; } set { this.strPKGID = value; } } publicstring Barcode { get { returnthis.strBarcode; } set { this.strBarcode = value; } } } #endregionpublicclass TestData { ///<summary>/// 获取测试数据表信息 ///</summary>///<returns></returns>publicstatic DataTable GetData() { DataTable dt = new DataTable(); dt.Columns.Add("SN"); dt.Columns.Add("PKGID"); dt.Columns.Add("Barcode"); DataRow dr = dt.NewRow(); dr[0] = "21136267"; dr[1] = "LA1011321948AB9412"; dr[2]= "[)>06F01001P52SLA1011321948AB941218VLEHWTF02010I1P211362672PDKHW20747420-141V10113210D1948(140)1T1948Q140"; dt.Rows.Add(dr); DataRow dr2 = dt.NewRow(); dr2[0] = "21138120-009"; dr2[1] = "LA1011321948AB9409"; dr2[2]= "[)>06F01001P52SLA1011321948AB940918VLEHWTF02010I1P21138120-0092PBKHW20708834-131V10113210D1948(40)1T1948Q40"; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3[0] = "21241207"; dr3[1] = "LA1011321947AE1218"; dr3[2]= "[)>06F01001P52SLA1011321947AE121818VLEHWTF02010I1P212412072PBKHW20709231-121V10113210D1947(210)1Q8.641T1947Q210"; dt.Rows.Add(dr3); return dt; } publicstatic List<Sample> GetList() { List<Sample> samples =new List<Sample> { new Sample {SN="21136267",PKGID="LA1011321948AB9412",Barcode="[)>06F01001P52SLA1011321948AB941218VLEHWTF02010I1P211362672PDKHW20747420-141V10113210D1948(140)1T1948Q140"}, new Sample{SN="21138120-009",PKGID="LA1011321948AB9409",Barcode="[)>06F01001P52SLA1011321948AB940918VLEHWTF02010I1P21138120-0092PBKHW20708834-131V10113210D1948(40)1T1948Q40"}, new Sample{SN="21241207",PKGID="LA1011321947AE1218",Barcode="[)>06F01001P52SLA1011321947AE121818VLEHWTF02010I1P212412072PBKHW20709231-121V10113210D1947(210)1Q8.641T1947Q210"} }; return samples; } } }
3、窗体实现
/* 要响应GridView的单击或者双击事件,要设置GridView的OptionsBehavior.Editable=false。 如果为true,它是不会响应这这两个事件的。 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Test.Barcode { public partial class FrmTest : Form { bool bo = false; public FrmTest() { InitializeComponent(); #region Timer timer = new Timer(); timer.Interval = 1000; timer.Tick += new EventHandler(RealTimer); timer.Start(); #endregion } void RealTimer(object sender,EventArgs e) { barHeaderItem1.Caption = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); } privatevoid btnExit_Click(object sender, EventArgs e) { this.Close(); } privatevoid FrmTest_FormClosing(object sender, FormClosingEventArgs e) { if (bo) { Application.Exit(); return; } DialogResult dialog = DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure exit the system?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dialog == DialogResult.OK) { try { bo = true; } catch (Exception) { } finally { Application.Exit(); } } else { e.Cancel = true; } } privatevoid FrmTest_Load(object sender, EventArgs e) { //gridControl1.DataSource = TestData.GetData(); gridControl1.DataSource = TestData.GetList(); gridView1.Columns[0].Width = 100; gridView1.Columns[1].Width = 150; gridView1.Columns[2].Width = 360; gridView1.Columns[0].Caption = "编号"; gridView1.Columns[1].Caption = "箱号"; gridView1.Columns[2].Caption = "二维码"; } privatevoid gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { // memoStr.Text = GetSelect("Barcode");string aa = gridView1.GetFocusedRowCellValue("Barcode").ToString(); memoStr.Text = aa; memoStr.Focus(); } ///<summary>/// 获取单元格值 ///</summary>///<param name="filedName">filedName为要获取列的列名</param>///<returns></returns>publicstring GetSelect(string filedName) { int[] pRows = this.gridView1.GetSelectedRows();//传递实体类过去 获取选中的行if (pRows.GetLength(0) > 0) return gridView1.GetRowCellValue(pRows[0], filedName).ToString(); elsereturnnull; } ///<summary>/// asciiCode转换 ///</summary>///<param name="asciiCode"></param>///<returns></returns>publicstaticchar[] Chr(int asciiCode) { if (asciiCode >= 0 && asciiCode <= 255) { System.Text.ASCIIEncoding ascii = new ASCIIEncoding(); byte[] byteArray = newbyte[] { (byte)asciiCode }; char[] aa = ascii.GetChars(byteArray); return aa; } else { thrownew Exception("ASCII Code is not valid."); } } privatevoid btnOk_Click(object sender, EventArgs e) { lstItem.Items.Clear(); MaterialBarcode mbarcode = new MaterialBarcode(memoStr.Text); #regionstring s1 = mbarcode.Barcode; string s2 = mbarcode.BoxName; string s3 = mbarcode.ArrayItemCode; string s4 = mbarcode.ArrayQty; string s5 = mbarcode.VerSion; string s6 = mbarcode.SN; string s7 = mbarcode.MAC; string s8 = mbarcode.GPON; string s9 = mbarcode.ClientItemCode; string s10 = mbarcode.CLEI; string s11 = mbarcode.Model; //string s12 = mbarcode.SupplyCode;string s13 = mbarcode.CompanyCode; string s14 = mbarcode.ClientPO; string s15 = mbarcode.BatchNumber; string s16 = mbarcode.GW; #endregionchar[] c = Chr(29); string[] myArr = s1.Split(c); if (string.IsNullOrEmpty(s5)) { MessageBox.Show("The version is not allow empty!"); return; } if (string.IsNullOrEmpty(s3)) { MessageBox.Show("The PN is not allow empty!"); return; } if (string.IsNullOrEmpty(s15)) { MessageBox.Show("The Lot No is not allow empty!"); return; } for (int i = 0; i < myArr.Length; i++) { if (!myArr[0].StartsWith("[)>")) { MessageBox.Show("The type of string is wrong!"); return; } if (i > 2 && myArr[2].Length != 21 && myArr[2].StartsWith("52S")) { if (myArr[2].Length > 21) { MessageBox.Show("The length more than 18!"); return; } else { MessageBox.Show("The length less than 18!"); return; } } if (i > 5 && (!myArr[5].StartsWith("1P"))) { MessageBox.Show("The number of matirial is not allow empty!"); return; } lstItem.Items.Add(myArr[i]); } } privatevoid memoStr_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == 13) { btnOk_Click(sender, e); } } privatevoid gridControl1_Click(object sender, EventArgs e) { //No.1 //memoStr.Text = GetSelect("Barcode"); //No.2 //string aa = gridView1.GetFocusedRowCellValue("Barcode").ToString(); //memoStr.Text = aa; //memoStr.Focus(); } } }
4、 结构体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace Test.Barcode { [StructLayout(LayoutKind.Sequential)] public struct MaterialBarcode { private string barcode; private string strBoxName; private string strArrayItemCode; private string strArrayQty; private string strVerSion; private string strSN; private string strMAC; private string strGPON; private string strClientItemCode; private string strCLEI; private string strModel; private string strSupplyCode; private string strCompanyCode; private string strClientPO; private string strBatchNumber; private string strGW; public MaterialBarcode(string barcode) { this.barcode = barcode; this.strBoxName = string.Empty; this.strArrayItemCode = string.Empty; this.strArrayQty = string.Empty; this.strVerSion = string.Empty; this.strSN = string.Empty; this.strMAC = string.Empty; this.strGPON = string.Empty; this.strClientItemCode = string.Empty; this.strCLEI = string.Empty; this.strModel = string.Empty; this.strSupplyCode = string.Empty; this.strCompanyCode = string.Empty; this.strClientPO = string.Empty; this.strBatchNumber = string.Empty; this.strGW = string.Empty; } publicstring GW { get { returnthis.strGW; } set { this.strGW = value; } } publicstring BoxName { get { returnthis.GetBoxName(this.barcode); } set { } } publicstring ArrayItemCode { get { returnthis.strArrayItemCode; } set { this.strArrayItemCode = value; } } publicstring ArrayQty { get { returnthis.strArrayQty; } set { this.strArrayQty = value; } } publicstring VerSion { get { returnthis.strVerSion; } set { this.strVerSion = value; } } publicstring SN { get { returnthis.strSN; } set { this.strSN = value; } } publicstring MAC { get { returnthis.strMAC; } set { this.strMAC = value; } } publicstring GPON { get { returnthis.strGPON; } set { this.strGPON = value; } } publicstring ClientItemCode { get { returnthis.strClientItemCode; } set { this.strClientItemCode = value; } } publicstring CLEI { get { returnthis.strCLEI; } set { this.strCLEI = value; } } publicstring Model { get { returnthis.strModel; } set { this.strModel = value; } } publicstring SupplyCode { get { returnthis.SupplyCode; } set { this.SupplyCode = value; } } publicstring CompanyCode { get { returnthis.strCompanyCode; } set { this.strCompanyCode = value; } } publicstring ClientPO { get { returnthis.strClientPO; } set { this.strClientPO = value; } } publicstring BatchNumber { get { returnthis.strBatchNumber; } set { this.strBatchNumber = value; } } privatestring GetBoxName(string barcode) { this.GetItemInfoByBarcode(barcode); returnthis.strBoxName; } privatevoid GetItemInfoByBarcode(string barcode) { barcode = barcode.Replace(Convert.ToString(‘\x001d‘), "&"); barcode = barcode.Replace(Convert.ToString(‘\x001e‘), "&"); string[] strArray = barcode.Split(newchar[] { ‘&‘ }); for (int i = 1; i < (strArray.Length - 1); i++) { if ((strArray[i].StartsWith("3S") || strArray[i].StartsWith("4S")) || (strArray[i].StartsWith("5S") || strArray[i].StartsWith("52S"))) { if (strArray[i].StartsWith("52S")) { this.strBoxName = strArray[i].Substring(3); } else { this.strBoxName = strArray[i].Substring(2); } } elseif (strArray[i].StartsWith("23S")) { this.strMAC = strArray[i].Substring(3); } elseif (strArray[i].StartsWith("24S")) { this.strGPON = strArray[i].Substring(3); } elseif (strArray[i].StartsWith("P")) { this.strClientItemCode = strArray[i].Substring(1); } elseif (strArray[i].StartsWith("1P")) { this.strArrayItemCode = strArray[i].Substring(2); } elseif (strArray[i].StartsWith("2P")) { this.strVerSion = strArray[i].Substring(2); } elseif (strArray[i].StartsWith("Q")) { this.strArrayQty = strArray[i].Substring(1); } elseif (strArray[i].StartsWith("S")) { this.strSN = strArray[i].Substring(1); } elseif (strArray[i].StartsWith("K")) { this.strClientPO = strArray[i].Substring(1); } elseif (strArray[i].StartsWith("1T")) { this.strBatchNumber = strArray[i].Substring(2); } elseif (strArray[i].StartsWith("1Q")) { this.strGW = strArray[i].Substring(2); } } } publicstring Barcode { get { if (this.barcode == null) { returnstring.Empty; } returnthis.barcode; } set { this.barcode = value; } } publicstring Itemcode { get { return GetCode(this.Barcode); } } publicstring Qty { get { return GetQty(this.Barcode); } } privatestaticstring GetQty(string barcode) { if (!barcode.StartsWith("Q")) { if (barcode.StartsWith("TQ")) { barcode = barcode.Substring(2, barcode.Length - 2).Trim(); } } else { StringBuilder builder = new StringBuilder(); barcode = barcode.Substring(1, barcode.Length - 1).Trim(); foreach (char ch in barcode) { if (char.IsLetter(ch)) { break; } builder.Append(ch); } barcode = builder.ToString(); } if ((barcode.IndexOf(‘E‘) <= 0) && (barcode.IndexOf(‘P‘) <= 0)) { if (barcode.StartsWith("(") || barcode.StartsWith("(")) { barcode = barcode.Substring(3, barcode.Length - 3); if (barcode.IndexOf(‘,‘) > 0) { barcode = barcode.Remove(barcode.IndexOf(‘,‘), 1); } return barcode.Trim(); } foreach (char ch3 in barcode) { if (!char.IsLetterOrDigit(ch3)) { returnstring.Empty; } } return barcode.Trim(); } string str = string.Empty; foreach (char ch2 in barcode) { if (ch2.Equals(‘E‘) || ch2.Equals(‘P‘)) { break; } if (char.IsNumber(ch2)) { str = str + ch2; } else { str = string.Empty; } } return str.Trim(); } publicstaticbool IsValid(string barcode) { return (GetCode(barcode) != null); } publicstaticstring GetCode(string barcode) { if ((barcode.Length == 8) || ((barcode.Length == 12) && (barcode.IndexOf(‘-‘) > -1))) { return barcode; } if (barcode.Length < 8) { returnstring.Empty; } string str = string.Empty; switch (barcode.Substring(0, 2)) { case"09": if (barcode.Length != 20) { if ((barcode.Length > 20) && (barcode.IndexOf(‘/‘) != -1)) { str = barcode.Substring(2, barcode.IndexOf(‘/‘) - 2); } return str; } return barcode.Substring(2, 8); case"(P": if (barcode.Length >= 3) { return barcode.Substring(3, barcode.Length - 3); } returnstring.Empty; case"10": if (barcode.Length == 0x12) { return barcode.Substring(2, 8); } returnstring.Empty; case"21": if (barcode.Length == 20) { return barcode.Substring(2, 8); } returnstring.Empty; case"02": if (barcode.Length == 0x10) { return ("0302" + barcode.Substring(2, 4)); } returnstring.Empty; case"03": if (barcode.Length == 0x10) { return ("0303" + barcode.Substring(2, 4)); } returnstring.Empty; case"19": if (barcode.Length != 20) { if ((barcode.Length > 20) && (barcode.IndexOf(‘/‘) != -1)) { str = barcode.Substring(2, barcode.IndexOf(‘/‘) - 2); } return str; } return barcode.Substring(2, 8); case"29": if (barcode.Length >= 9) { return barcode.Substring(2, 8); } returnstring.Empty; case"39": if (barcode.Length >= 9) { return barcode.Substring(2, 8); } returnstring.Empty; } if (barcode.ToUpper().StartsWith("L")) { returnstring.Copy(barcode); } if (barcode.ToUpper().StartsWith("P")) { if (barcode.Length == 0x13) { return barcode.Substring(1, 8); } if ((barcode.Length > 9) && (barcode.IndexOf("-") != -1)) { return barcode.Substring(1, 12); } if ((barcode.Length > 0x13) && (barcode.IndexOf(‘/‘) != -1)) { str = barcode.Substring(1, barcode.IndexOf(‘/‘) - 1); } return str; } if (char.IsNumber(barcode, 0)) { str = barcode.Substring(0, 8); } return str; } publicoverridestring ToString() { returnthis.Barcode; } } }
原文:https://www.cnblogs.com/lxh168/p/13734166.html
内容总结
以上是互联网集市为您收集整理的扫码枪二维码一般格式解析Demo全部内容,希望文章能够帮你解决扫码枪二维码一般格式解析Demo所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。