Skip to content

Commit 67d61ae

Browse files
authored
Merge pull request #81 from adit-dhananjay/feat/aspose-ocr
Example of Aspose OCR
2 parents a4db7fe + b36db5f commit 67d61ae

11 files changed

Lines changed: 247 additions & 0 deletions

aspose-ocr/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin/
2+
obj/
3+
*.sln.iml
4+
.idea/**/*
5+
**/.idea/*

aspose-ocr/Apps/ImageToTextApp.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Ivy.Aspose.OCR.Examples.Connections.OCR;
2+
using System.IO;
3+
4+
namespace Ivy.Aspose.OCR.Examples.Apps;
5+
6+
[App(icon: Icons.FileImage, path: ["Apps"])]
7+
public class ImageToTextApp : ViewBase
8+
{
9+
public override object? Build()
10+
{
11+
var outputText = this.UseState<string>("");
12+
var ocrService = UseService<IOCRService>();
13+
14+
var error = UseState<string?>(() => null);
15+
var files = UseState<FileInput?>(() => null);
16+
var fileBytes = UseState<byte[]?>(() => null); // Store the actual file bytes
17+
18+
var uploadUrl = this.UseUpload(
19+
uploadedBytes =>
20+
{
21+
if (uploadedBytes.Length > 1 * 1024 * 1024) // 1MB limit
22+
{
23+
error.Set("File size must be less than 1MB");
24+
fileBytes.Set((byte[]?)null); // Clear stored bytes on error
25+
return;
26+
}
27+
28+
error.Set((string?)null);
29+
fileBytes.Set(uploadedBytes); // Store the file bytes for later use
30+
},
31+
"image/jpeg",
32+
"uploaded-image"
33+
);
34+
35+
return Layout.Vertical(
36+
Text.H1("Convert Image to text online").Color(Colors.Green),
37+
Text.Block("Free OCR software to convert images or screenshots to text online"),
38+
error.Value != null
39+
? new Callout(error.Value, variant: CalloutVariant.Error)
40+
: null,
41+
files.ToFileInput(uploadUrl, "Upload Image").Accept("image/*"),
42+
new Button("Recognize", _ =>
43+
{
44+
if (error.Value == null && fileBytes.Value != null)
45+
{
46+
// Use the stored file bytes instead of file.Content
47+
using var ms = new MemoryStream(fileBytes.Value);
48+
outputText.Value = ocrService.ExtractText(ms);
49+
fileBytes.Set((byte[]?)null); // Clear stored bytes once completed
50+
}
51+
}),
52+
Text.Block("Output Text:"),
53+
new ObservableView<string>(outputText)
54+
).Align(Align.Center);
55+
}
56+
}
40.5 KB
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Aspose.OCR;
2+
3+
namespace Ivy.Aspose.OCR.Examples.Connections.OCR.Aspose;
4+
5+
public class AsposeOcrService: IOCRService
6+
{
7+
private readonly AsposeOcr recognitionEngine;
8+
9+
public AsposeOcrService()
10+
{
11+
recognitionEngine = new AsposeOcr();
12+
}
13+
14+
public string ExtractText(MemoryStream imageStream)
15+
{
16+
// Add image to the recognition batch
17+
var source = new OcrInput(InputType.SingleImage);
18+
source.Add(imageStream);
19+
20+
// Perform OCR
21+
List<RecognitionResult> results
22+
= recognitionEngine.Recognize(source);
23+
24+
// OCR processing
25+
string result = string.Empty;
26+
if (results.Count > 0)
27+
{
28+
result = results[0].RecognitionText;
29+
}
30+
31+
return result;
32+
}
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Ivy.Aspose.OCR.Examples.Connections.OCR;
2+
3+
public interface IOCRService
4+
{
5+
string ExtractText(MemoryStream imageStream);
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Ivy.Aspose.OCR.Examples.Connections.OCR.Aspose;
2+
using Ivy.Connections;
3+
4+
namespace Ivy.Aspose.OCR.Examples.Connections.OCR;
5+
6+
public class OCRConnection : IConnection
7+
{
8+
public string GetConnectionType()
9+
{
10+
return typeof(OCRConnection).ToString();
11+
}
12+
13+
public string GetContext(string connectionPath)
14+
{
15+
throw new NotImplementedException();
16+
}
17+
18+
public ConnectionEntity[] GetEntities()
19+
{
20+
throw new NotImplementedException();
21+
}
22+
23+
public string GetName() => nameof(OCRConnection);
24+
25+
public string GetNamespace() => typeof(OCRConnection).Namespace;
26+
27+
public void RegisterServices(IServiceCollection services)
28+
{
29+
services.AddScoped<IOCRService, AsposeOcrService>();
30+
}
31+
}

aspose-ocr/GlobalUsings.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
global using Ivy;
2+
global using Ivy.Apps;
3+
global using Ivy.Auth;
4+
global using Ivy.Chrome;
5+
global using Ivy.Client;
6+
global using Ivy.Core;
7+
global using Ivy.Core.Hooks;
8+
global using Ivy.Helpers;
9+
global using Ivy.Hooks;
10+
global using Ivy.Shared;
11+
global using Ivy.Services;
12+
global using Ivy.Views;
13+
global using Ivy.Views.Alerts;
14+
global using Ivy.Views.Blades;
15+
global using Ivy.Views.Builders;
16+
global using Ivy.Views.Charts;
17+
global using Ivy.Views.Dashboards;
18+
global using Ivy.Views.Forms;
19+
global using Ivy.Views.Tables;
20+
global using Ivy.Widgets.Inputs;
21+
global using Microsoft.Extensions.Configuration;
22+
global using Microsoft.Extensions.DependencyInjection;
23+
global using Microsoft.Extensions.Logging;
24+
global using System.Collections.Immutable;
25+
global using System.ComponentModel.DataAnnotations;
26+
global using System.Globalization;
27+
global using System.Reactive.Linq;
28+
29+
namespace Ivy.Aspose.OCR.Examples;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<NoWarn>CS8618;CS8603;CS8602;CS8604;CS9113</NoWarn>
9+
<RootNamespace>Ivy.Aspose.OCR.Examples</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<EmbeddedResource Include="Assets/**/*" />
14+
</ItemGroup>
15+
16+
17+
18+
<ItemGroup>
19+
<PackageReference Include="Aspose.OCR" Version="25.9.0" />
20+
<PackageReference Include="Ivy" Version="1.0.113.0" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36429.23 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ivy.Aspose.OCR.Examples", "Ivy.Aspose.OCR.Examples.csproj", "{BA2A13B0-78C9-203B-1462-034AD42E4D37}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {24B5F0F2-14B2-4944-9817-4606E84F19E2}
24+
EndGlobalSection
25+
EndGlobal

aspose-ocr/Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
using Ivy.Aspose.OCR.Examples.Apps;
3+
4+
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
5+
var server = new Server();
6+
#if DEBUG
7+
server.UseHotReload();
8+
#endif
9+
server.AddAppsFromAssembly();
10+
server.AddConnectionsFromAssembly();
11+
var chromeSettings = new ChromeSettings()
12+
.DefaultApp<ImageToTextApp>()
13+
.UseTabs(preventDuplicates: true);
14+
server.UseChrome(chromeSettings);
15+
await server.RunAsync();

0 commit comments

Comments
 (0)