Exemplo n.º 1
0
        private static TextResult[] DecodeFile(string strImagePath, int mode, int iLeft, int iTop, int iRight, int iBottom)
        {
            BarcodeReader _br = new BarcodeReader();

            _br.LicenseKeys = "t0068MgAAAJbpvFwUvsodF81FjWojDo91ZYmDf3+aNdOGPOBOygS6Yte0JFqPMt/DnNMdfGS4gInUd+5RYOCX6IramuO+m4A=";
            string[] strTemplateNameArray       = { "All_DEFAULT", "All_DEFAULT_WITHREGION" };
            string   tempDefaultTemplateJson    = "{\"Version\": \"2.0\",\"ImageParameter\": {\"Name\": \"" + strTemplateNameArray[0] + "\",\"BarcodeFormatIds\": [\"All\"],\"RegionPredetectionMode\": \"Enable\"}}";
            string   tempTemplateJsonWithRegion = "{\"Version\": \"2.0\",\"ImageParameter\": {\"Name\": \"" + strTemplateNameArray[1] + "\",\"BarcodeFormatIds\": [\"All\"],\"RegionPredetectionMode\": \"Disable\",\"RegionDefinitionNameArray\": [\"Region\"]},\"RegionDefinitionArray\": [{\"Name\": \"Region\",\"MeasuredByPercentage\": true" + ",\"Left\":" + iLeft.ToString() + ",\"Top\":" + iTop.ToString() + ",\"Right\":" + iRight.ToString() + ",\"Bottom\":" + iBottom.ToString() + "}]}";

            if (mode == 0)
            {
                // load template json as a string.
                string        errorstring   = "";
                EnumErrorCode temperrorcode = _br.InitRuntimeSettingsWithString(tempDefaultTemplateJson, EnumConflictMode.ECM_Overwrite, out errorstring);
            }
            else
            {
                // load template json as a string.
                string        errorstring   = "";
                EnumErrorCode temperrorcode = _br.InitRuntimeSettingsWithString(tempTemplateJsonWithRegion, EnumConflictMode.ECM_Overwrite, out errorstring);
            }
            _br.DecodeFile(strImagePath, strTemplateNameArray[mode]);
            TextResult[] result = _br.DecodeFile(strImagePath, strTemplateNameArray[mode]);

            return(result);
        }
Exemplo n.º 2
0
        private static TextResult[] DecodeFile(string strImagePath, int mode, int iLeft, int iTop, int iRight, int iBottom)
        {
            BarcodeReader _br = new BarcodeReader();

            _br.ProductKeys = "t0068MgAAADaH8yokXmKf3axcV99lMBDDRYEZIsBZ5PPiekmW820HqSR2tQ/VOjuXPvq1FCvla7eS6KmEMUFgHZR9X7GuR2s=";
            string[] strTemplateNameArray       = { "All_DEFAULT", "All_DEFAULT_WITHREGION" };
            string   tempDefaultTemplateJson    = "{\"Version\": \"2.0\",\"ImageParameter\": {\"Name\": \"" + strTemplateNameArray[0] + "\",\"BarcodeFormatIds\": [\"BF_ALL\"],\"RegionPredetectionModes\": [{\"Mode\":\"RPM_GENERAL_RGB_CONTRAST\"}]}}";
            string   tempTemplateJsonWithRegion = "{\"Version\": \"2.0\",\"ImageParameter\": {\"Name\": \"" + strTemplateNameArray[1] + "\",\"BarcodeFormatIds\": [\"BF_ALL\"],\"RegionPredetectionModes\": [{\"Mode\":\"RPM_GENERAL\"}],\"RegionDefinitionNameArray\": [\"Region\"]},\"RegionDefinitionArray\": [{\"Name\": \"Region\",\"MeasuredByPercentage\": 1" + ",\"Left\":" + iLeft.ToString() + ",\"Top\":" + iTop.ToString() + ",\"Right\":" + iRight.ToString() + ",\"Bottom\":" + iBottom.ToString() + "}]}";

            if (mode == 0)
            {
                // load template json as a string.
                string        errorstring   = "";
                EnumErrorCode temperrorcode = _br.InitRuntimeSettingsWithString(tempDefaultTemplateJson, EnumConflictMode.CM_OVERWRITE, out errorstring);
            }
            else
            {
                // load template json as a string.
                string        errorstring   = "";
                EnumErrorCode temperrorcode = _br.InitRuntimeSettingsWithString(tempTemplateJsonWithRegion, EnumConflictMode.CM_OVERWRITE, out errorstring);
            }
            _br.DecodeFile(strImagePath, strTemplateNameArray[mode]);
            TextResult[] result = _br.DecodeFile(strImagePath, strTemplateNameArray[mode]);

            return(result);
        }
Exemplo n.º 3
0
 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;
     }
 }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        public void DecodeFile(string file)
        {
            string strErrorMSG = "";

            //Best coverage settings
            mBarcodeRader.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
            //mBarcodeRader.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
            //mBarcodeRader.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"Balance\",\"DeblurLevel\":5,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_STATISTICS\"}]}}", EnumConflictMode.CM_OVERWRITE, out strErrorMSG);


            TextResult[] tempResult = mBarcodeRader.DecodeFile(file, "");
            if (tempResult != null && tempResult.Length > 0)
            {
                if (OnBarcodeRecognized != null)
                {
                    OnBarcodeRecognized(tempResult);
                }
            }
        }
Exemplo n.º 6
0
        private static TextResult[] DecodeFile(string strImagePath, int mode, int iLeft, int iTop, int iRight, int iBottom)
        {
            BarcodeReader _br = new BarcodeReader();

            _br.ProductKeys = "t0068MgAAAEUWFzAvIFjWdsOhURov3SljTtFakKFsHemq+2NKnvb5tEihIDmWlZsFpCWpVOnWr1Uw1NzIQ2EcnLj9Hxxvjfs=";

            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);


            string[] strTemplateNameArray       = { "All_DEFAULT", "All_DEFAULT_WITHREGION" };
            string   tempDefaultTemplateJson    = "{\"Version\": \"2.0\",\"ImageParameter\": {\"Name\": \"" + strTemplateNameArray[0] + "\",\"BarcodeFormatIds\": [\"BF_ALL\"],\"RegionPredetectionModes\": [{\"Mode\":\"RPM_GENERAL_RGB_CONTRAST\"}]}}";
            string   tempTemplateJsonWithRegion = "{\"Version\": \"2.0\",\"ImageParameter\": {\"Name\": \"" + strTemplateNameArray[1] + "\",\"BarcodeFormatIds\": [\"BF_ALL\"],\"RegionPredetectionModes\": [{\"Mode\":\"RPM_GENERAL\"}],\"RegionDefinitionNameArray\": [\"Region\"]},\"RegionDefinitionArray\": [{\"Name\": \"Region\",\"MeasuredByPercentage\": 1" + ",\"Left\":" + iLeft.ToString() + ",\"Top\":" + iTop.ToString() + ",\"Right\":" + iRight.ToString() + ",\"Bottom\":" + iBottom.ToString() + "}]}";

            if (mode == 0)
            {
                // load template json as a string.
                string        errorstring   = "";
                EnumErrorCode temperrorcode = _br.AppendTplStringToRuntimeSettings(tempDefaultTemplateJson, EnumConflictMode.CM_OVERWRITE, out errorstring);
            }
            else
            {
                // load template json as a string.
                string        errorstring   = "";
                EnumErrorCode temperrorcode = _br.AppendTplStringToRuntimeSettings(tempTemplateJsonWithRegion, EnumConflictMode.CM_OVERWRITE, out errorstring);
            }

            TextResult[] result = _br.DecodeFile(strImagePath, "");

            return(result);
        }
Exemplo n.º 7
0
        /// <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));
        }