Exemple #1
0
        private string TextFromImage(Image image)
        {
            var ORC    = new IronOcr.AutoOcr();
            var result = ORC.Read(image);

            return(result.Text);
        }
Exemple #2
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //Dosya yükleme kodu internetten alınmıştır.
            // If the user has selected a file
            if (FileUpload1.HasFile)
            {
                // Get the file extension
                string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);

                if (fileExtension.ToLower() != ".png" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".jpeg")
                {
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text      = "Only files with .jpeg, .png and .jpg extension are allowed";
                }
                else
                {
                    // Get the file size
                    int fileSize = FileUpload1.PostedFile.ContentLength;
                    // If file size is greater than 2 MB
                    if (fileSize > 2097152)
                    {
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                        lblMessage.Text      = "File size cannot be greater than 2 MB";
                    }
                    else
                    {
                        // Upload the file
                        FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));
                        lblMessage.ForeColor = System.Drawing.Color.Green;
                        lblMessage.Text      = "File uploaded successfully";
                        adress = Server.MapPath("~/Uploads/" + FileUpload1.FileName);

                        System.IO.Stream     stream = FileUpload1.PostedFile.InputStream;
                        System.Drawing.Image image  = System.Drawing.Image.FromStream(stream);

                        int height = image.Height;
                        int width  = image.Width;

                        var ocr = new IronOcr.AutoOcr()
                        {
                            ReadBarCodes = false
                        };
                        var Area = new System.Drawing.Rectangle()
                        {
                            X = 0, Y = height * 3 / 4, Width = width, Height = height
                        };

                        String words = ocr.Read(adress, Area).ToString();

                        String[] words_isbn = words.Split(' ');
                        String   isbn = "";
                        int      i = 0, i_keep = 0;
                        for (i = 0; i < words_isbn.Length; i++)
                        {
                            if (words_isbn[i].Contains("SBN") || words_isbn[i].Equals("ISBN") || words_isbn[i].Equals("İSBN") || words_isbn[i].Equals("ısbn") || words_isbn[i].Equals("isbn") || words_isbn[i].Equals("|SBN") || words_isbn[i].Equals("lSBN") || words_isbn[i].Equals("LSBN"))
                            {
                                i_keep = i;
                            }
                        }

                        if (i_keep == 0 && (words_isbn[i_keep].Contains("ISBN") == false || words_isbn[i_keep].Contains("SBN") == false))
                        {
                            for (i = i_keep; i < words_isbn.Length; i++)
                            {
                                isbn += words_isbn[i];
                            }
                        }
                        else if (words_isbn[i_keep].Contains("ISBN") && words_isbn[i_keep].All(char.IsDigit))
                        {
                            int index = words_isbn[i_keep].IndexOf('I');
                            for (i = index; i < words_isbn[i_keep].Length; i++)
                            {
                                isbn += words_isbn[i_keep][i];
                            }

                            for (i = i_keep + 1; i < words_isbn.Length; i++)
                            {
                                isbn += words_isbn[i];
                            }
                        }
                        else if (words_isbn[i_keep].Contains("ISBN") && words_isbn[i_keep].All(char.IsDigit) == false)
                        {
                            if (words_isbn.Length < i_keep + 8)
                            {
                                for (i = i_keep + 1; i < words_isbn.Length; i++)
                                {
                                    isbn += words_isbn[i];
                                }
                            }
                            else
                            {
                                for (i = i_keep + 1; i < i_keep + 8; i++)
                                {
                                    isbn += words_isbn[i];
                                }
                            }
                        }



                        Regex regex = new Regex("[^0-9]");
                        isbn = regex.Replace(isbn, string.Empty);


                        if (isbn.Length >= 13)
                        {
                            isbn = isbn.Substring(0, 13);
                        }
                        else
                        {
                            isbn = isbn.Substring(0, 10);
                        }

                        ısbn_number.Text = isbn;
                    }
                }
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text      = "Please select a file";
            }
        }