Exemplo n.º 1
0
        /// <summary>
        /// Reason : To get Text from image
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>

        public string ExtractTextFromImage(string fileName)
        {
            try
            {
                GC.WaitForPendingFinalizers();

                string   ExtractedTest = "";
                string   filePath      = fileName.Trim() != "" ? @fileName : @"C:\Users\Cuelogic\Desktop\sample images\Untitled.png";
                Document doc           = new Document();
                doc.Create(filePath);
                Thread.Sleep(500);
                try
                {
                    Thread.BeginCriticalRegion();
                    doc.OCR(MiLANGUAGES.miLANG_ENGLISH, true, true);
                }
                catch (Exception) { return(""); }

                MODI.Image modiImage = (doc.Images[0] as MODI.Image);
                ExtractedTest = modiImage.Layout.Text;
                doc.Close();
                Thread.EndCriticalRegion();
                return(ExtractedTest);
            }
            catch (Exception)
            {
                return("");
            }
            finally
            {
                GC.Collect();
            }
        }
        /// <summary>
        /// Reason : To get Text from image
        /// </summary>
        /// <param name="fileName">Input file name</param>
        /// <returns>Return Text from image</returns>
        public string ExtractTextFromImage(string fileName)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    return(null); //"File name can not be null";
                }
                string   extractedText = "";
                Document doc           = new Document();
                GC.WaitForPendingFinalizers();
                doc.Create(fileName);
                Thread.Sleep(500);
                try
                {
                    Thread.BeginCriticalRegion();
                    doc.OCR(MiLANGUAGES.miLANG_ENGLISH, true, true);
                }
                catch (Exception) { return(""); }

                MODI.Image modiImage = (doc.Images[0] as MODI.Image);
                extractedText = modiImage.Layout.Text;
                doc.Close();
                Thread.EndCriticalRegion();
                return(extractedText);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                GC.Collect();
            }
        }
Exemplo n.º 3
0
        // Document is Extract the text from Image.
        private string ExtractTextFromImage(string filePath, string language)
        {
            Document modiDocument = new Document();

            modiDocument.Create(filePath);
            modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
            MODI.Image modiImage     = (modiDocument.Images[0] as MODI.Image);
            string     extractedText = modiImage.Layout.Text;

            modiDocument.Close();
            return(extractedText);
        }
Exemplo n.º 4
0
        string GetText(string Filepath)
        {
            string imageText = "INVALID";
            int    numTries  = 0;

            modiDoc.Create(Filepath);

            while (numTries <= 5 && imageText == "INVALID")
            {
                try
                {
                    modiDoc.OCR(MiLANGUAGES.miLANG_ENGLISH);
                    modiImage = (MODI.Image)modiDoc.Images[0];
                    imageText = modiImage.Layout.Text;
                }
                catch
                {
                    switch (numTries)
                    {
                    case 0:
                        //increase contrast
                        break;

                    case 1:
                        //invert colors
                        break;

                    case 2:
                        break;

                    case 3:
                        break;

                    case 4:
                        break;

                    default:
                        break;
                    }
                    numTries++;
                }
            }

            return(imageText);
        }
Exemplo n.º 5
0
    private string ExtractTextFromImageBasic(string filePath)
    {
        Document modiDocument = new Document();

        modiDocument.Create(filePath);
        modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
        MODI.Image modiImage = (modiDocument.Images[0] as MODI.Image);

        string extractedText = modiImage.Layout.Text;

        modiDocument.Close();

        int index = (int)extractedText.IndexOf("No") + 3;

        extractedText = extractedText.Substring(index, 10);

        return(extractedText);
    }
Exemplo n.º 6
0
    private string ExtractTextFromImage(string filePath)
    {
        Bitmap bmp = new Bitmap(filePath);
        string croppedImageFile = "", resizeImageFile = "";

        try
        {
            // 1: (319 x 200), (190, 107, 50, 15)
            //bmp = bmp.Clone(new Rectangle(190, 107, 50, 15), System.Drawing.Imaging.PixelFormat.DontCare);
            //filePath = "C:\\Users\\Kullanici\\Desktop\\Image\\Kart\\1.bmp";

            // Sample 2: (455x 313), (261, 197, 80, 17)
            bmp = bmp.Clone(new Rectangle(261, 197, 80, 17), System.Drawing.Imaging.PixelFormat.DontCare);
            //bmp = Resize(bmp);

            croppedImageFile = "C:\\Users\\Kullanici\\Desktop\\Image\\Kart\\2.bmp";
            resizeImageFile  = "C:\\Users\\Kullanici\\Desktop\\Image\\Kart\\22.bmp";
            bmp.Save(croppedImageFile);
            Resize(croppedImageFile, resizeImageFile, 20);

            //bmp = (Bitmap)Bitmap.FromFile(resizeImageFile);

            //bmp = SetGrayscale(bmp);
            //bmp = RemoveNoise(bmp);
            //filePath = "C:\\Users\\Kullanici\\Desktop\\Image\\Kart\\2.bmp";
            //bmp.Save(resizeImageFile);
        }
        catch (Exception ex) { string se = ex.ToString(); }

        //filePath = VaryQualityLevel(filePath);

        Document modiDocument = new Document();

        modiDocument.Create(resizeImageFile);
        modiDocument.OCR(MiLANGUAGES.miLANG_SYSDEFAULT); // hata veriyor.
        MODI.Image modiImage = (modiDocument.Images[0] as MODI.Image);

        string extractedText = modiImage.Layout.Text;

        modiDocument.Close();
        return(extractedText);
    } // ExtractTextFromImage
Exemplo n.º 7
0
    private string OCR(string fileToOCR)
    {
        MODI.Document md = new MODI.Document();

        md.Create(fileToOCR);

        md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);

        MODI.Image img = (MODI.Image)md.Images[0];

        MODI.Layout layout = img.Layout;

        layout = img.Layout;

        string result = layout.Text;

        md.Close(false);

        return(result);
    }
Exemplo n.º 8
0
        private string ExtractTextFromImage()
        {
            string         filePath = "";
            OpenFileDialog ofd      = new OpenFileDialog();

            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filePath = ofd.FileName;
            }
            else
            {
                return("");
            }
            Document modiDocument = new Document();

            modiDocument.Create(filePath);
            modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
            MODI.Image modiImage     = (modiDocument.Images[0] as MODI.Image);
            string     extractedText = modiImage.Layout.Text;

            modiDocument.Close();
            return(extractedText);
        }
Exemplo n.º 9
0
        public ScanVisitorVM ScanDetails(string item)
        {
            var modiDocument  = new Document();
            var rootImagePath = System.Web.HttpContext.Current.Server.MapPath(@"~/images");
            var filePath      = string.Format("{0}\\VisitorIdentityImages\\{1}", rootImagePath, item);

            modiDocument.Create(filePath);
            modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
            MODI.Image modiImage   = (modiDocument.Images[0] as MODI.Image);
            var        actualtText = modiImage.Layout.Text;

            var extractedText = modiImage.Layout.Text.Replace(Environment.NewLine, "<br />").Trim(new char[] { '\\' });

            string[] resultSplitted = extractedText.Split('>');
            if (objScannedData.Nationality != null)
            {
                var data = _genericSer.LookUpValues.GetAll().Where(x => x.LookUpValue == objScannedData.Nationality).FirstOrDefault();
                if (data != null)
                {
                    objScannedData.NationalityId = data.Id;
                }
                else
                {
                    objScannedData.NationalityId = 38;
                }
            }
            if (extractedText.Contains("United Arab Emêrates") || extractedText.Contains("United Arab Emirates") ||
                extractedText.Contains("Idenmy Card"))
            //Front page of emirates id card
            {
                objScannedData.TypeOfCard = "Emirates Id";
                if (resultSplitted.Length >= 4 && !string.IsNullOrEmpty(resultSplitted[3]))
                {
                    objScannedData.IDNumber = resultSplitted[3].Replace("<br /", "").Replace(" ", "");
                }
                if (resultSplitted.Length >= 6 && !string.IsNullOrEmpty(resultSplitted[5]))
                {
                    objScannedData.VisitorName = resultSplitted[5].Replace("<br /", "").Replace("Name:", "").Replace("Name", "");
                    //.Replace(" ", "")
                }
                if (resultSplitted.Length >= 8 && !string.IsNullOrEmpty(resultSplitted[7]))
                {
                    objScannedData.Nationality = resultSplitted[7].Replace("<br /", "").Replace("NaonahIy:", "").Replace("Nationality:", "").Replace(" ", "");
                }
            }
            else if (extractedText.Contains("Date of Birth") || extractedText.Contains("Dale B.rlh"))
            {
                objScannedData.TypeOfCard = "Emirates Id";
                //Back page of emirates id card
                if (resultSplitted.Length >= 1 && !string.IsNullOrEmpty(resultSplitted[0]))
                {
                    //		resultSplitted[0].Split(' ')[5][0]	48 '0'	char
                    if (resultSplitted[0].Contains(":") && !resultSplitted[0].Contains("Dale B.rlh"))
                    {
                        objScannedData.Gender      = resultSplitted[0].Split(':')[1];
                        objScannedData.DateOfBirth = resultSplitted[0].Split(':')[2].Replace("<br /", " ").Replace("Date of Birth", " ").Replace("j1", "").Replace(" ", "");
                    }
                    else
                    {
                        objScannedData.Gender      = resultSplitted[0].Split(' ')[1];
                        objScannedData.DateOfBirth = (resultSplitted[0].Split(' ')[5][0].ToString() + resultSplitted[0].Split(' ')[5][1].ToString()
                                                      + "/" + resultSplitted[0].Split(' ')[5][3].ToString() + resultSplitted[0].Split(' ')[5][4].ToString()
                                                      + "/" + resultSplitted[0].Split(' ')[5][6].ToString() + resultSplitted[0].Split(' ')[5][7].ToString()
                                                      + resultSplitted[0].Split(' ')[5][8].ToString()
                                                      + resultSplitted[0].Split(' ')[5][9].ToString());
                    }
                }
            }
            else if (extractedText.Contains("License No"))
            //Driving licence
            {
                objScannedData.TypeOfCard = "License";
                if (resultSplitted.Length >= 4 && !string.IsNullOrEmpty(resultSplitted[3]))
                {
                    objScannedData.LienceNo = resultSplitted[16].Replace("License No.", "").Replace("<br /", "");
                }
            }
            else //visiting card
            {
                objScannedData.CompanyName = "";
                //logic to retrieve email address
                foreach (var curreIitem in extractedText.Split('<'))
                {
                    if (curreIitem.Contains("@") || curreIitem.ToLower().Contains("email"))
                    {
                        objScannedData.EmailAddress = curreIitem
                                                      .Replace("br />", "")
                                                      .Replace(":", "")
                                                      .Replace("—", "")
                                                      .Replace("Email", "").Replace("email", "")
                                                      .Replace(" ", "");
                        break;
                    }
                }
                //logic to retrieve contact number
                foreach (var curreIitem in extractedText.Split('<'))
                {
                    if (curreIitem.Contains("+") || curreIitem.ToLower().Contains("tel") ||
                        curreIitem.ToLower().Contains("cell") || curreIitem.ToLower().Contains("mob"))
                    {
                        objScannedData.ContactNumber = curreIitem.Replace("br />", "")
                                                       .Replace(":", "")
                                                       .Replace("Tel", "").Replace("tel", "")
                                                       .Replace("Mob", "").Replace("mob", "")
                                                       .Replace("Cell", "").Replace("cell", "")
                                                       .Replace(" ", "");
                        break;
                    }
                }
            }
            objScannedData.CompanyName = "For Testing";

            modiDocument.Close();
            return(objScannedData);
        }
Exemplo n.º 10
0
        private void scan_button_Click(object sender, EventArgs e)
        {
            // Create a DeviceManager instance
            var deviceManager = new DeviceManager();

            // Create an empty variable to store the scanner instance
            DeviceInfo firstScannerAvailable = null;

            // Loop through the list of devices to choose the first available
            for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
            {
                // Skip the device if it's not a scanner
                if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
                {
                    continue;
                }

                firstScannerAvailable = deviceManager.DeviceInfos[i];

                break;
            }

            // Connect to the first available scanner
            var device = firstScannerAvailable.Connect();

            // Select the scanner
            var scannerItem = device.Items[1];

            // Retrieve a image in JPEG format and store it into a variable
            var imageFile = (ImageFile)scannerItem.Transfer(FormatID.wiaFormatPNG);

            // Save the image in some path with filename
            var path = @"./img/scan.png";

            if (File.Exists(path))
            {
                System.GC.Collect();
                System.GC.WaitForPendingFinalizers();
                File.Delete(path);
            }

            // Save image !
            imageFile.SaveFile(path);

            Bitmap imagen = new Bitmap(path);

            //  pictureBox1.Image = (System.Drawing.Image) imagen;
            //I did this because there was a process using the file , so the image could not be deleted

            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                pictureBox1.Image = System.Drawing.Image.FromStream(stream);
                stream.Dispose();
            }

            Document MyVariable = new Document();

            MyVariable.Create(path);
            MyVariable.OCR(MODI.MiLANGUAGES.miLANG_SPANISH, true, true);
            MODI.Image image2 = (MODI.Image)MyVariable.Images[0];
            Layout     layout = image2.Layout;
            string     str    = "";

            foreach (Word word in layout.Words)
            {
                str += word.Text + " ";
            }

            richTextBox1.Text = str;
            fillRegistroForm(str);
        }
Exemplo n.º 11
0
        internal List <Entity> extractERDDiagram(string path, out string message)
        {
            message = string.Empty;


            if (string.IsNullOrEmpty(path))
            {
                message = "Must select a file";
                return(null);
            }

            List <Entity> entities = new List <Entity>();

            string extension = Path.GetExtension(path);

            //Parsing From Image
            if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".bmp")
            {
                Document modiDocument = new Document();
                modiDocument.Create(path);
                modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
                MODI.Image modiImage     = (modiDocument.Images[0] as MODI.Image);
                string     extractedText = modiImage.Layout.Text;
                modiDocument.Close();
                entities.Add(parseStringToEntity(extractedText));
            }
            else if (extension == ".vdx")
            {
                //MS VISIO XML Parsing
                XDocument xdoc      = XDocument.Load(path);
                var       elements  = xdoc.Elements().Elements();
                XName     pageXName = XName.Get("Page", "http://schemas.microsoft.com/visio/2003/core");
                var       pages     = elements.Elements(pageXName);

                foreach (XElement page in pages)
                {
                    XName shapeXName = XName.Get("Shape", "http://schemas.microsoft.com/visio/2003/core");
                    var   shapes     = from shape in page.Elements().Elements(shapeXName)
                                       where shape.Attribute("Type").Value == "Group"
                                       select shape;

                    foreach (XElement shape in shapes)
                    {
                        try
                        {
                            Entity entity = new Entity();


                            var             shapeShapes = shape.Elements();
                            List <XElement> textShapes  = shapeShapes.Elements(shapeXName).ToList();

                            XName textXName = XName.Get("Text", "http://schemas.microsoft.com/visio/2003/core");
                            XName cpXName   = XName.Get("Text", "http://schemas.microsoft.com/visio/2003/core");

                            string tableName = textShapes[0].Elements(textXName).SingleOrDefault().Value;
                            string columns   = textShapes[1].Elements(textXName).SingleOrDefault().Value;

                            entity.Name       = tableName.Replace("\n", "");
                            entity.Attributes = parseStringToAttributes(columns);
                            entities.Add(entity);
                        }
                        catch { }
                    }
                }
            }
            else
            {
                message = "File format invalid!";
                return(null);
            }

            return(entities);
        }