File tree Expand file tree Collapse file tree 5 files changed +52
-8
lines changed
src/VueJsTsOcrAspNetCoreSample Expand file tree Collapse file tree 5 files changed +52
-8
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
- using System . Collections . Generic ;
2
+ using System . IO ;
3
3
using System . Linq ;
4
4
using System . Threading . Tasks ;
5
+
6
+ using Microsoft . AspNetCore . Http ;
5
7
using Microsoft . AspNetCore . Mvc ;
8
+ using Microsoft . ProjectOxford . Vision ;
9
+ using Microsoft . ProjectOxford . Vision . Contract ;
10
+
11
+ using Newtonsoft . Json ;
6
12
7
13
namespace VueJsTsOcrAspNetCoreSample . Controllers
8
14
{
@@ -16,5 +22,42 @@ public IActionResult Hello()
16
22
var msg = new { Message = "Hello World" } ;
17
23
return this . Ok ( msg ) ;
18
24
}
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
+ }
19
62
}
20
63
}
Original file line number Diff line number Diff line change 3
3
"windowsAuthentication" : false ,
4
4
"anonymousAuthentication" : true ,
5
5
"iisExpress" : {
6
- "applicationUrl" : " http ://localhost:26622 /" ,
7
- "sslPort" : 0
6
+ "applicationUrl" : " https ://localhost:44340 /" ,
7
+ "sslPort" : 44340
8
8
}
9
9
},
10
10
"profiles" : {
Original file line number Diff line number Diff line change 1
- {
1
+ {
2
2
"dependencies" : {
3
3
"Microsoft.NETCore.App" : {
4
4
"version" : " 1.1.0" ,
22
22
"Microsoft.Extensions.Logging.Debug" : " 1.1.0" ,
23
23
"Microsoft.Extensions.Options.ConfigurationExtensions" : " 1.1.0" ,
24
24
"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"
26
27
},
27
28
28
29
"tools" : {
Original file line number Diff line number Diff line change 1
1
import Vue from "vue" ;
2
2
import { Component , Inject } from "vue-property-decorator" ;
3
- import { AxiosStatic , AxiosResponse } from "axios" ;
3
+ import { AxiosStatic , AxiosRequestConfig , AxiosResponse } from "axios" ;
4
4
import Symbols from "../configs/Symbols" ;
5
5
6
6
@Component ( {
Original file line number Diff line number Diff line change 6
6
</div >
7
7
8
8
<div class =" image-output" >
9
- <img id =" ocr-image" ref =" ocrOutput" />
9
+ <img id =" ocr-image" ref =" ocrOutput" style = " width : 300 px " />
10
10
</div >
11
11
12
12
<div class =" cognitive-result" >
13
- <textarea id =" ocr-result" width = " 50 " height = " 200px" ref =" ocrResult" ></textarea >
13
+ <textarea id =" ocr-result" style = " width : 300 px ; height : 200px ; " ref =" ocrResult" ></textarea >
14
14
</div >
15
15
</div >
16
16
</template >
You can’t perform that action at this time.
0 commit comments