private static TextResult[] DecodeFile(BarcodeReader _br, string strImagePath) { //modifiy the default template and decode file. PublicRuntimeSettings settings = _br.GetRuntimeSettings(); //set max barcode count. settings.mMaxBarcodesCount = 1; _br.UpdateRuntimeSettings(settings); TextResult[] result = _br.DecodeFile(strImagePath, ""); return(result); }
private void ReadBarcodesButton_Click(object sender, EventArgs e) { Console.WriteLine("reading dbr"); if (pictureBox1.Tag == null) { return; } if (DBRTemplateTextBox.Text != "") { try { String o = ""; reader.InitRuntimeSettingsWithString(DBRTemplateTextBox.Text, EnumConflictMode.CM_OVERWRITE, errorMessage: out o); PublicRuntimeSettings rs = reader.GetRuntimeSettings(); rs.ResultCoordinateType = EnumResultCoordinateType.RCT_PIXEL; reader.UpdateRuntimeSettings(rs); } catch (Exception) { throw; } } try { TextResult[] results = reader.DecodeFile(pictureBox1.Tag.ToString(), ""); string outputInfo = "Total barcodes found: " + results.Length.ToString(); Console.WriteLine(outputInfo); for (int iIndex = 0; iIndex < results.Length; ++iIndex) { int iBarcodeIndex = iIndex + 1; string builder = "Barcode " + iBarcodeIndex.ToString() + ":\r\n"; TextResult result = results[iIndex]; if (result.BarcodeFormat != 0) { builder += " Type: " + result.BarcodeFormatString + "\r\n"; } else { builder += " Type: " + result.BarcodeFormatString_2 + "\r\n"; } builder += " Value: " + result.BarcodeText + "\r\n"; draw(pictureBox1.Image, result.LocalizationResult.ResultPoints); Console.WriteLine(builder); textBox1.Text = textBox1.Text + builder; } textBox1.Tag = results; } catch (BarcodeReaderException exp) { Console.WriteLine(exp.Message); textBox1.Text = exp.Message; } }
private static TextResult[] DecodeFile(BarcodeReader _br, string strImagePath) { //modifiy the default template and decode file. PublicRuntimeSettings settings = _br.GetRuntimeSettings(); //set excepted barcode count. settings.mExpectedBarcodesCount = 0x7ffffff; settings.mRegionPredetectionMode = RegionPredetectionMode.RPM_Enable; _br.UpdateRuntimeSettings(settings); TextResult[] result = _br.DecodeFile(strImagePath, ""); return(result); }
private static TextResult[] DecodeFile(BarcodeReader _br, string strImagePath) { string strErrorMSG = ""; //Best coverage settings _br.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestCoverage\",\"DeblurLevel\":9,\"ExpectedBarcodesCount\":512,\"ScaleDownThreshold\":100000,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_SCAN_DIRECTLY\"},{\"Mode\":\"LM_STATISTICS\"},{\"Mode\":\"LM_LINES\"},{\"Mode\":\"LM_STATISTICS_MARKS\"}],\"GrayscaleTransformationModes\":[{\"Mode\":\"GTM_ORIGINAL\"},{\"Mode\":\"GTM_INVERTED\"}]}}", EnumConflictMode.CM_OVERWRITE, out strErrorMSG); //Best speed settings //_br.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestSpeed\",\"DeblurLevel\":3,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_SCAN_DIRECTLY\"}],\"TextFilterModes\":[{\"MinImageDimension\":262144,\"Mode\":\"TFM_GENERAL_CONTOUR\"}]}}", EnumConflictMode.CM_OVERWRITE, out strErrorMSG); //Balance settings //_br.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"Balance\",\"DeblurLevel\":5,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_STATISTICS\"}]}}", EnumConflictMode.CM_OVERWRITE, out strErrorMSG); //modifiy the default template and decode file. PublicRuntimeSettings settings = _br.GetRuntimeSettings(); //set excepted barcode count. settings.ExpectedBarcodesCount = 0x7ffffff; _br.UpdateRuntimeSettings(settings); TextResult[] result = _br.DecodeFile(strImagePath, ""); return(result); }
/// <summary> /// Using barcode value to rename documents. /// </summary> /// <param name="strInputFolder">the path of folder that documents in</param> /// <param name="strOutputFolder">the path of folder that renamed documents saved in</param> private void DoRename(string strInputFolder, string strOutputFolder) { int iFileCount = 0; int iSuccCount = 0; DateTime dtStart = DateTime.Now; string[] files = Directory.GetFiles(strInputFolder); if (files != null) { foreach (string strFile in files) { if (IsImageFile(strFile)) { FileStream infs = null; FileStream outfs = null; try { iFileCount++; int iDirectSeparator = strFile.LastIndexOf(Path.DirectorySeparatorChar); string strFileName = strFile.Substring(iDirectSeparator + 1); tbLog.AppendText(string.Format("\r\nProcessing file {0}\r\n", strFileName)); Bitmap bmp = (Bitmap)Bitmap.FromFile(strFile); PublicRuntimeSettings tempParameterSettings = barcodeReader.GetRuntimeSettings(); tempParameterSettings.mBarcodeFormatIds = formatid; barcodeReader.UpdateRuntimeSettings(tempParameterSettings); TextResult[] barcodes = barcodeReader.DecodeBitmap(bmp, "");//DecodeFile(strFile); bmp.Dispose(); if (barcodes == null || barcodes.Length <= 0) { tbLog.AppendText("There is no barcode on the first page\r\n"); } else { tbLog.AppendText(string.Format("Page: {0}\r\n", barcodes[0].LocalizationResult.PageNumber)); tbLog.AppendText(string.Format("Barcode Value: {0}\r\n", barcodes[0].BarcodeText)); //output file name int iDot = strFileName.LastIndexOf('.'); string strOutputFileName = barcodes[0].BarcodeText + strFileName.Substring(iDot); string strOutputFile = null; if (strOutputFolder.EndsWith(Path.DirectorySeparatorChar.ToString())) { strOutputFile = strOutputFolder + strOutputFileName; } else { strOutputFile = strOutputFolder + Path.DirectorySeparatorChar + strOutputFileName; } if (barcodes[0].BarcodeText.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { tbLog.AppendText(string.Format("{0} contains character(s) that are not allowed in file names, skip renaming\r\n", barcodes[0].BarcodeText)); } else { if (File.Exists(strOutputFile)) { tbLog.AppendText(string.Format("{0} exists, skip renaming {1}\r\n", strOutputFileName, strFileName)); } else { infs = new FileStream(strFile, FileMode.Open, FileAccess.Read); outfs = new FileStream(strOutputFile, FileMode.CreateNew); int size = 1 << 14; byte[] buffer = new byte[size]; while (infs.Read(buffer, 0, size) > 0) { outfs.Write(buffer, 0, size); } infs.Close(); outfs.Close(); iSuccCount++; tbLog.AppendText(string.Format("Renamed to {0}\r\n", strOutputFileName)); } } } } catch (Exception exp) { tbLog.AppendText(exp.Message + "\r\n"); if (infs != null) { infs.Close(); } if (outfs != null) { outfs.Close(); } } tbLog.Refresh(); } } } tbLog.AppendText("Completed\r\n"); tbLog.AppendText(string.Format("Files Total: {0} file(s), Success: {1} file(s)\r\n", iFileCount, iSuccCount)); tbLog.AppendText(string.Format("Total cost time: {0}ms", (int)(DateTime.Now - dtStart).TotalMilliseconds)); }
/// <summary> /// Using barcode value to rename documents. /// </summary> /// <param name="strInputFolder">the path of folder that documents in</param> /// <param name="strOutputFolder">the path of folder that renamed documents saved in</param> private void DoRename(string strInputFolder, string strOutputFolder) { int iFileCount = 0; int iSuccCount = 0; DateTime dtStart = DateTime.Now; string[] files = Directory.GetFiles(strInputFolder); if (files != null) { foreach (string strFile in files) { if (IsImageFile(strFile)) { FileStream infs = null; FileStream outfs = null; try { iFileCount++; int iDirectSeparator = strFile.LastIndexOf(Path.DirectorySeparatorChar); string strFileName = strFile.Substring(iDirectSeparator + 1); tbLog.AppendText(string.Format("\r\nProcessing file {0}\r\n", strFileName)); Bitmap bmp = (Bitmap)Bitmap.FromFile(strFile); string strErrorMSG = ""; //Best coverage settings barcodeReader.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestCoverage\",\"DeblurLevel\":9,\"ExpectedBarcodesCount\":512,\"ScaleDownThreshold\":100000,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_SCAN_DIRECTLY\"},{\"Mode\":\"LM_STATISTICS\"},{\"Mode\":\"LM_LINES\"},{\"Mode\":\"LM_STATISTICS_MARKS\"}],\"GrayscaleTransformationModes\":[{\"Mode\":\"GTM_ORIGINAL\"},{\"Mode\":\"GTM_INVERTED\"}]}}", EnumConflictMode.CM_OVERWRITE, out strErrorMSG); //Best speed settings //barcodeReader.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestSpeed\",\"DeblurLevel\":3,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_SCAN_DIRECTLY\"}],\"TextFilterModes\":[{\"MinImageDimension\":262144,\"Mode\":\"TFM_GENERAL_CONTOUR\"}]}}", EnumConflictMode.CM_OVERWRITE, out strErrorMSG); //Balance settings //barcodeReader.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"Balance\",\"DeblurLevel\":5,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_STATISTICS\"}]}}", EnumConflictMode.CM_OVERWRITE, out strErrorMSG); PublicRuntimeSettings tempParameterSettings = barcodeReader.GetRuntimeSettings(); tempParameterSettings.BarcodeFormatIds = formatid; barcodeReader.UpdateRuntimeSettings(tempParameterSettings); TextResult[] barcodes = barcodeReader.DecodeBitmap(bmp, "");//DecodeFile(strFile); bmp.Dispose(); if (barcodes == null || barcodes.Length <= 0) { tbLog.AppendText("There is no barcode on the first page\r\n"); } else { tbLog.AppendText(string.Format("Page: {0}\r\n", barcodes[0].LocalizationResult.PageNumber)); tbLog.AppendText(string.Format("Barcode Value: {0}\r\n", barcodes[0].BarcodeText)); //output file name int iDot = strFileName.LastIndexOf('.'); string strOutputFileName = barcodes[0].BarcodeText + strFileName.Substring(iDot); string strOutputFile = null; if (strOutputFolder.EndsWith(Path.DirectorySeparatorChar.ToString())) { strOutputFile = strOutputFolder + strOutputFileName; } else { strOutputFile = strOutputFolder + Path.DirectorySeparatorChar + strOutputFileName; } if (barcodes[0].BarcodeText.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { tbLog.AppendText(string.Format("{0} contains character(s) that are not allowed in file names, skip renaming\r\n", barcodes[0].BarcodeText)); } else { if (File.Exists(strOutputFile)) { tbLog.AppendText(string.Format("{0} exists, skip renaming {1}\r\n", strOutputFileName, strFileName)); } else { infs = new FileStream(strFile, FileMode.Open, FileAccess.Read); outfs = new FileStream(strOutputFile, FileMode.CreateNew); int size = 1 << 14; byte[] buffer = new byte[size]; while (infs.Read(buffer, 0, size) > 0) { outfs.Write(buffer, 0, size); } infs.Close(); outfs.Close(); iSuccCount++; tbLog.AppendText(string.Format("Renamed to {0}\r\n", strOutputFileName)); } } } } catch (Exception exp) { tbLog.AppendText(exp.Message + "\r\n"); if (infs != null) { infs.Close(); } if (outfs != null) { outfs.Close(); } } tbLog.Refresh(); } } } tbLog.AppendText("Completed\r\n"); tbLog.AppendText(string.Format("Files Total: {0} file(s), Success: {1} file(s)\r\n", iFileCount, iSuccCount)); tbLog.AppendText(string.Format("Total cost time: {0}ms", (int)(DateTime.Now - dtStart).TotalMilliseconds)); }