Ejemplo n.º 1
0
        public void ExtractTextTest()
        {
            ProcessImage.Init();
            var imagepath = "OCR/testimage.jpg";

            Assert.IsTrue(
                File.Exists(imagepath),
                "Deployment failed: {0} did not get deployed.",
                imagepath
                );
            ProcessImage.GrayScale(imagepath);
            Assert.IsTrue(true);
            var text = ProcessImage.ExtractText(imagepath);

            Assert.IsNotNull(text);
            Debug.WriteLine(text);
        }
Ejemplo n.º 2
0
        public ActionResult Index()
        {
            string fullPath = "";

            try
            {
                var file       = Request.Form.Files[0];
                var folderName = Path.Combine("Resources");
                var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

                if (file.Length > 0)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    fullPath = Path.Combine(pathToSave, fileName);
                    var dbPath = Path.Combine(folderName, fileName);

                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }

                    var dictresponse = new Dictionary <string, string>();

                    var alttext = ProcessImage.ExtractText(fullPath);

                    if (alttext.Length > 0)
                    {
                        alttext = "Image may contain the following text: " + alttext;
                    }
                    dictresponse.Add("text", alttext);
                    //clean file
                    ProcessImage.DeleteFile(fullPath);

                    return(Content(JsonConvert.SerializeObject(dictresponse), "application/json"));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                ProcessImage.DeleteFile(fullPath);
                return(StatusCode(500, $"Internal server error: {ex}"));
            }
        }