Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions aspose-ocr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin/
obj/
*.sln.iml
.idea/**/*
**/.idea/*
56 changes: 56 additions & 0 deletions aspose-ocr/Apps/ImageToTextApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Ivy.Aspose.OCR.Examples.Connections.OCR;
using System.IO;

namespace Ivy.Aspose.OCR.Examples.Apps;

[App(icon: Icons.FileImage, path: ["Apps"])]
public class ImageToTextApp : ViewBase
{
public override object? Build()
{
var outputText = this.UseState<string>("");
var ocrService = UseService<IOCRService>();

var error = UseState<string?>(() => null);
var files = UseState<FileInput?>(() => null);
var fileBytes = UseState<byte[]?>(() => null); // Store the actual file bytes

var uploadUrl = this.UseUpload(
uploadedBytes =>
{
if (uploadedBytes.Length > 1 * 1024 * 1024) // 1MB limit
{
error.Set("File size must be less than 1MB");
fileBytes.Set((byte[]?)null); // Clear stored bytes on error
return;
}

error.Set((string?)null);
fileBytes.Set(uploadedBytes); // Store the file bytes for later use
},
"image/jpeg",
"uploaded-image"
);

return Layout.Vertical(
Text.H1("Convert Image to text online").Color(Colors.Green),
Text.Block("Free OCR software to convert images or screenshots to text online"),
error.Value != null
? new Callout(error.Value, variant: CalloutVariant.Error)
: null,
files.ToFileInput(uploadUrl, "Upload Image").Accept("image/*"),
new Button("Recognize", _ =>
{
if (error.Value == null && fileBytes.Value != null)
{
// Use the stored file bytes instead of file.Content
using var ms = new MemoryStream(fileBytes.Value);
outputText.Value = ocrService.ExtractText(ms);
fileBytes.Set((byte[]?)null); // Clear stored bytes once completed
}
}),
Text.Block("Output Text:"),
new ObservableView<string>(outputText)
).Align(Align.Center);
}
}
Binary file added aspose-ocr/Apps/aspose-OCR-image-to-text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions aspose-ocr/Connections/OCR/Aspose/AsposeOCRService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Aspose.OCR;

namespace Ivy.Aspose.OCR.Examples.Connections.OCR.Aspose;

public class AsposeOcrService: IOCRService
{
private readonly AsposeOcr recognitionEngine;

public AsposeOcrService()
{
recognitionEngine = new AsposeOcr();
}

public string ExtractText(MemoryStream imageStream)
{
// Add image to the recognition batch
var source = new OcrInput(InputType.SingleImage);
source.Add(imageStream);

// Perform OCR
List<RecognitionResult> results
= recognitionEngine.Recognize(source);

// OCR processing
string result = string.Empty;
if (results.Count > 0)
{
result = results[0].RecognitionText;
}

return result;
}
}
6 changes: 6 additions & 0 deletions aspose-ocr/Connections/OCR/IOCRService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Ivy.Aspose.OCR.Examples.Connections.OCR;

public interface IOCRService
{
string ExtractText(MemoryStream imageStream);
}
31 changes: 31 additions & 0 deletions aspose-ocr/Connections/OCR/OCRConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Ivy.Aspose.OCR.Examples.Connections.OCR.Aspose;
using Ivy.Connections;

namespace Ivy.Aspose.OCR.Examples.Connections.OCR;

public class OCRConnection : IConnection
{
public string GetConnectionType()
{
return typeof(OCRConnection).ToString();
}

public string GetContext(string connectionPath)
{
throw new NotImplementedException();
}

public ConnectionEntity[] GetEntities()
{
throw new NotImplementedException();
}

public string GetName() => nameof(OCRConnection);

public string GetNamespace() => typeof(OCRConnection).Namespace;

public void RegisterServices(IServiceCollection services)
{
services.AddScoped<IOCRService, AsposeOcrService>();
}
}
29 changes: 29 additions & 0 deletions aspose-ocr/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
global using Ivy;
global using Ivy.Apps;
global using Ivy.Auth;
global using Ivy.Chrome;
global using Ivy.Client;
global using Ivy.Core;
global using Ivy.Core.Hooks;
global using Ivy.Helpers;
global using Ivy.Hooks;
global using Ivy.Shared;
global using Ivy.Services;
global using Ivy.Views;
global using Ivy.Views.Alerts;
global using Ivy.Views.Blades;
global using Ivy.Views.Builders;
global using Ivy.Views.Charts;
global using Ivy.Views.Dashboards;
global using Ivy.Views.Forms;
global using Ivy.Views.Tables;
global using Ivy.Widgets.Inputs;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using System.Collections.Immutable;
global using System.ComponentModel.DataAnnotations;
global using System.Globalization;
global using System.Reactive.Linq;

namespace Ivy.Aspose.OCR.Examples;
23 changes: 23 additions & 0 deletions aspose-ocr/Ivy.Aspose.OCR.Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>CS8618;CS8603;CS8602;CS8604;CS9113</NoWarn>
<RootNamespace>Ivy.Aspose.OCR.Examples</RootNamespace>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Assets/**/*" />
</ItemGroup>



<ItemGroup>
<PackageReference Include="Aspose.OCR" Version="25.9.0" />
<PackageReference Include="Ivy" Version="1.0.113.0" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions aspose-ocr/Ivy.Aspose.OCR.Examples.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36429.23 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ivy.Aspose.OCR.Examples", "Ivy.Aspose.OCR.Examples.csproj", "{BA2A13B0-78C9-203B-1462-034AD42E4D37}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA2A13B0-78C9-203B-1462-034AD42E4D37}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {24B5F0F2-14B2-4944-9817-4606E84F19E2}
EndGlobalSection
EndGlobal
15 changes: 15 additions & 0 deletions aspose-ocr/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

using Ivy.Aspose.OCR.Examples.Apps;

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
Comment thread
adit-dhananjay marked this conversation as resolved.
var server = new Server();
#if DEBUG
server.UseHotReload();
#endif
server.AddAppsFromAssembly();
server.AddConnectionsFromAssembly();
var chromeSettings = new ChromeSettings()
.DefaultApp<ImageToTextApp>()
.UseTabs(preventDuplicates: true);
server.UseChrome(chromeSettings);
await server.RunAsync();
24 changes: 24 additions & 0 deletions aspose-ocr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Ivy.Aspose.OCR.Examples

Web application created using [Ivy](https://github.com/Ivy-Interactive/Ivy-Framework).

Ivy is a web framework for building interactive web applications using C# and .NET.

## How it works

This web application demonstrates OCR (Optical Character Recognition) using Ivy and Aspose.OCR. Users can upload an image (JPEG, up to 1MB), and the app extracts text from the image using an OCR service. The workflow is:

1. Upload an image using the provided form.
2. The app validates the file size and type.
3. On clicking "Recognize", the image is processed and text is extracted.
4. The recognized text is displayed on the page.

## Run

```
dotnet watch
```

## Output Screenshot

![OCR Example](Apps/aspose-OCR-image-to-text.png)