/// <summary> /// zbar识别 /// </summary> /// <param name="fn_share"></param> /// <param name="filename"></param> private void barcode(fn_share fn_share, string filename) { string json = string.Empty; string sql = string.Empty; string guid = string.Empty; string decoded = string.Empty; string barcode = string.Empty; if (db.KeyExists("recognizetask")) { JObject jo = null; try { json = db.ListLeftPop("recognizetask"); fn_share.systemLog(filename, "-------------------------------------------------------------\r\n"); if (!string.IsNullOrEmpty(json)) { jo = (JObject)JsonConvert.DeserializeObject(json); //只有PDF文件才会进行条形码识别 sql = @"select t.* from list_attachment t where t.ordercode='" + jo.Value <string>("ordercode") + "' and upper(t.filesuffix)='PDF'"; DataTable dt = DBMgr.GetDataTable(sql); if (dt.Rows.Count > 0) { DateTime d1 = DateTime.Now; guid = Guid.NewGuid().ToString(); //ConvertPDF2Image(direc_pdf + dt.Rows[0]["FILENAME"], direc_img, guid, 1, 1, ImageFormat.Jpeg, Definition.Ten); ConvertPDF.pdfToPic(direc_pdf + dt.Rows[0]["FILENAME"], direc_img, guid, 1, 1, ImageFormat.Jpeg); string fileName = direc_img + guid + ".Jpeg"; fn_share.systemLog(filename, "=== ConvertToImage——" + (DateTime.Now - d1) + "\r\n"); if (File.Exists(fileName)) { Image primaryImage = Image.FromFile(fileName); Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage); using (ZBar.ImageScanner scanner = new ZBar.ImageScanner()) { scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0); scanner.SetConfiguration(ZBar.SymbolType.QRCODE, ZBar.Config.Enable, 1); scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1); List <ZBar.Symbol> symbols = new List <ZBar.Symbol>(); symbols = scanner.Scan((Image)pImg); if (symbols != null && symbols.Count > 0) { decoded = symbols[0].Data; } if (!string.IsNullOrEmpty(decoded))//有些PDF文件并无条形码 { //barcode = barconvert(decoded);//编码转换 (这是28系统的转换) barcode = decoded; } else { barcode = "001"; } sql = "update list_order set cusno='" + barcode + "' where code='" + jo.Value <string>("ordercode") + "'"; DBMgr.ExecuteNonQuery(sql); } } fn_share.systemLog(filename, "=== " + jo.Value <string>("ordercode") + "——" + (DateTime.Now - d1) + "——" + barcode + "\r\n"); } } } catch (Exception ex) { //db.ListRightPush("recognizetask", json); sql = "update list_order set cusno='002' where code='" + jo.Value <string>("ordercode") + "'"; DBMgr.ExecuteNonQuery(sql); fn_share.systemLog(filename, jo.Value <string>("ordercode") + " 异常,识别条码失败:" + ex.Message + "\r\n"); } } }
private void barcode_toolKit(fn_share fn_share, string filename) { string json = string.Empty; string sql = string.Empty; string guid = string.Empty; string decoded = string.Empty; string barcode = string.Empty; if (db.KeyExists("recognizetask")) { JObject jo = null; try { json = db.ListLeftPop("recognizetask"); fn_share.systemLog(filename, "-------------------------------------------------------------\r\n"); if (!string.IsNullOrEmpty(json)) { jo = (JObject)JsonConvert.DeserializeObject(json); //只有PDF文件才会进行条形码识别 sql = @"select t.* from list_attachment t where t.ordercode='" + jo.Value <string>("ordercode") + "' and upper(t.filesuffix)='PDF'"; DataTable dt = DBMgr.GetDataTable(sql); if (dt.Rows.Count > 0) { DateTime d1 = DateTime.Now; guid = Guid.NewGuid().ToString(); ConvertPDF.pdfToPic(direc_pdf + dt.Rows[0]["FILENAME"], direc_img, guid, 1, 1, ImageFormat.Jpeg);//pdf转图片 string fileName = direc_img + guid + ".Jpeg"; fn_share.systemLog(filename, "=== ConvertToImage——" + (DateTime.Now - d1) + "\r\n"); if (File.Exists(fileName)) { BarcodeDecoder barcodeDecoder = new BarcodeDecoder(); Image primaryImage = Image.FromFile(fileName); Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage); Dictionary <DecodeOptions, object> decodingOptions = new Dictionary <DecodeOptions, object>(); List <BarcodeFormat> possibleFormats = new List <BarcodeFormat>(10); possibleFormats.Add(BarcodeFormat.Code128); possibleFormats.Add(BarcodeFormat.EAN13); decodingOptions.Add(DecodeOptions.TryHarder, true); decodingOptions.Add(DecodeOptions.PossibleFormats, possibleFormats); DateTime d2 = DateTime.Now; Result decodedResult = barcodeDecoder.Decode(pImg, decodingOptions); Console.WriteLine("解析时长:" + (DateTime.Now - d2)); //while (decodedResult == null) //{ // System.Threading.Thread.Sleep(500); //} if (decodedResult != null)//有些PDF文件并无条形码 { barcode = decodedResult.Text; } else { barcode = "001"; } sql = "update list_order set cusno='" + barcode + "' where code='" + jo.Value <string>("ordercode") + "'"; DBMgr.ExecuteNonQuery(sql); } fn_share.systemLog(filename, "=== " + jo.Value <string>("ordercode") + "——" + (DateTime.Now - d1) + "——" + barcode + "\r\n"); } } } catch (Exception ex) { //db.ListRightPush("recognizetask", json); sql = "update list_order set cusno='002' where code='" + jo.Value <string>("ordercode") + "'"; DBMgr.ExecuteNonQuery(sql); fn_share.systemLog(filename, jo.Value <string>("ordercode") + " 异常,识别条码失败:" + ex.Message + "\r\n"); } } }