Skip to content

Commit b2cc8f2

Browse files
committed
Add Azure Cognitive Services OCR API
1 parent 85d317d commit b2cc8f2

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

src/VueJsTsOcrAspNetCoreSample/Controllers/ApiController.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
using System;
2-
using System.Collections.Generic;
2+
using System.IO;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
6+
using Microsoft.AspNetCore.Http;
57
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.ProjectOxford.Vision;
9+
using Microsoft.ProjectOxford.Vision.Contract;
10+
11+
using Newtonsoft.Json;
612

713
namespace VueJsTsOcrAspNetCoreSample.Controllers
814
{
@@ -16,5 +22,42 @@ public IActionResult Hello()
1622
var msg = new { Message = "Hello World" };
1723
return this.Ok(msg);
1824
}
25+
26+
[Route("ocr")]
27+
[HttpPost]
28+
public async Task<IActionResult> GetApiText(IFormFile file)
29+
{
30+
if (file == null)
31+
{
32+
file = this.Request.Form.Files.FirstOrDefault();
33+
}
34+
35+
OcrResults result;
36+
string imagePath;
37+
using (var stream = file.OpenReadStream())
38+
{
39+
var client = new VisionServiceClient("[SUBSCRIPTION_KEY_GOES_HERE]");
40+
try
41+
{
42+
result = await client.RecognizeTextAsync(stream, languageCode: "en", detectOrientation: true).ConfigureAwait(false);
43+
44+
using (var ms = new MemoryStream())
45+
{
46+
await file.CopyToAsync(ms).ConfigureAwait(false);
47+
var base64 = Convert.ToBase64String(ms.ToArray());
48+
var value = $"data:{file.ContentType};base64,{base64}";
49+
50+
imagePath = value;
51+
}
52+
}
53+
catch (Exception e)
54+
{
55+
Console.WriteLine(e);
56+
throw;
57+
}
58+
}
59+
60+
return Json(new { Result = result, ImagePath = imagePath }, new JsonSerializerSettings() { Formatting = Formatting.Indented });
61+
}
1962
}
2063
}

src/VueJsTsOcrAspNetCoreSample/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:26622/",
7-
"sslPort": 0
6+
"applicationUrl": "https://localhost:44340/",
7+
"sslPort": 44340
88
}
99
},
1010
"profiles": {

src/VueJsTsOcrAspNetCoreSample/project.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"dependencies": {
33
"Microsoft.NETCore.App": {
44
"version": "1.1.0",
@@ -22,7 +22,8 @@
2222
"Microsoft.Extensions.Logging.Debug": "1.1.0",
2323
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
2424
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
25-
"Microsoft.AspNetCore.SpaServices": "1.1.0"
25+
"Microsoft.AspNetCore.SpaServices": "1.1.0",
26+
"Microsoft.ProjectOxford.Vision": "1.0.372"
2627
},
2728

2829
"tools": {

src/VueJsTsOcrAspNetCoreSample/src/components/Ocr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from "vue";
22
import { Component, Inject } from "vue-property-decorator";
3-
import { AxiosStatic, AxiosResponse } from "axios";
3+
import { AxiosStatic, AxiosRequestConfig, AxiosResponse } from "axios";
44
import Symbols from "../configs/Symbols";
55

66
@Component({

src/VueJsTsOcrAspNetCoreSample/src/components/Ocr.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
</div>
77

88
<div class="image-output">
9-
<img id="ocr-image" ref="ocrOutput" />
9+
<img id="ocr-image" ref="ocrOutput" style="width:300px" />
1010
</div>
1111

1212
<div class="cognitive-result">
13-
<textarea id="ocr-result" width="50" height="200px" ref="ocrResult"></textarea>
13+
<textarea id="ocr-result" style="width: 300px; height: 200px;" ref="ocrResult"></textarea>
1414
</div>
1515
</div>
1616
</template>

0 commit comments

Comments
 (0)