Ejemplo n.º 1
0
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            if (Request.HttpMethod == "GET")
            {
                return(View());
            }

            var model = new RecognizeTextModel();

            await RunOperationOnImage(async stream =>
            {
                model.Results = await VisionServiceClient.RecognizeTextAsync(stream, detectOrientation: false);
            });

            model.ImageDump = GetInlineImageWithLines(model.Results);



            if (file != null && file.ContentLength > 0)
            {
                if (!file.ContentType.StartsWith("image"))
                {
                    TempData["Message"] = "Only image files may be uploaded";
                }
                else
                {
                    try
                    {
                        CloudStorageAccount account   = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
                        CloudBlobClient     client    = account.CreateCloudBlobClient();
                        CloudBlobContainer  container = client.GetContainerReference("pictures");
                        CloudBlockBlob      photo     = container.GetBlockBlobReference(Path.GetFileName(file.FileName));
                        await photo.UploadFromStreamAsync(file.InputStream);


                        using (var outputStream = new MemoryStream())
                        {
                            file.InputStream.Seek(0L, SeekOrigin.Begin);
                            var settings = new ResizeSettings {
                                MaxWidth = 192
                            };
                            ImageBuilder.Current.Build(file.InputStream, outputStream, settings);
                            outputStream.Seek(0L, SeekOrigin.Begin);
                            container = client.GetContainerReference("pictures");
                            CloudBlockBlob thumbnail = container.GetBlockBlobReference(Path.GetFileName(file.FileName));
                            await thumbnail.UploadFromStreamAsync(outputStream);
                        }
                    }
                    catch (Exception ex)
                    {
                        // In case something goes wrong
                        TempData["Message"] = ex.Message;
                    }
                }
            }



            return(View(model));
        }
        public async Task <ActionResult> Index()
        {
            if (Request.HttpMethod == "GET")
            {
                return(View());
            }

            var model = new RecognizeTextModel();

            await RunOperationOnImage(async stream =>
            {
                model.Results = await VisionServiceClient.RecognizeTextAsync(stream, detectOrientation: false);
            });

            model.ImageDump = GetInlineImageWithLines(model.Results);

            return(View(model));
        }