diff --git a/aspose-ocr/.gitignore b/aspose-ocr/.gitignore new file mode 100644 index 00000000..2cb90536 --- /dev/null +++ b/aspose-ocr/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +*.sln.iml +.idea/**/* +**/.idea/* \ No newline at end of file diff --git a/aspose-ocr/Apps/ImageToTextApp.cs b/aspose-ocr/Apps/ImageToTextApp.cs new file mode 100644 index 00000000..ec7c6255 --- /dev/null +++ b/aspose-ocr/Apps/ImageToTextApp.cs @@ -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(""); + var ocrService = UseService(); + + var error = UseState(() => null); + var files = UseState(() => null); + var fileBytes = UseState(() => 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(outputText) + ).Align(Align.Center); + } +} \ No newline at end of file diff --git a/aspose-ocr/Apps/aspose-OCR-image-to-text.png b/aspose-ocr/Apps/aspose-OCR-image-to-text.png new file mode 100644 index 00000000..858fc172 Binary files /dev/null and b/aspose-ocr/Apps/aspose-OCR-image-to-text.png differ diff --git a/aspose-ocr/Connections/OCR/Aspose/AsposeOCRService.cs b/aspose-ocr/Connections/OCR/Aspose/AsposeOCRService.cs new file mode 100644 index 00000000..1e4a220f --- /dev/null +++ b/aspose-ocr/Connections/OCR/Aspose/AsposeOCRService.cs @@ -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 results + = recognitionEngine.Recognize(source); + + // OCR processing + string result = string.Empty; + if (results.Count > 0) + { + result = results[0].RecognitionText; + } + + return result; + } +} diff --git a/aspose-ocr/Connections/OCR/IOCRService.cs b/aspose-ocr/Connections/OCR/IOCRService.cs new file mode 100644 index 00000000..0dc91164 --- /dev/null +++ b/aspose-ocr/Connections/OCR/IOCRService.cs @@ -0,0 +1,6 @@ +namespace Ivy.Aspose.OCR.Examples.Connections.OCR; + +public interface IOCRService +{ + string ExtractText(MemoryStream imageStream); +} diff --git a/aspose-ocr/Connections/OCR/OCRConnection.cs b/aspose-ocr/Connections/OCR/OCRConnection.cs new file mode 100644 index 00000000..b73224a6 --- /dev/null +++ b/aspose-ocr/Connections/OCR/OCRConnection.cs @@ -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(); + } +} diff --git a/aspose-ocr/GlobalUsings.cs b/aspose-ocr/GlobalUsings.cs new file mode 100644 index 00000000..1fecd23e --- /dev/null +++ b/aspose-ocr/GlobalUsings.cs @@ -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; diff --git a/aspose-ocr/Ivy.Aspose.OCR.Examples.csproj b/aspose-ocr/Ivy.Aspose.OCR.Examples.csproj new file mode 100644 index 00000000..fa5d1867 --- /dev/null +++ b/aspose-ocr/Ivy.Aspose.OCR.Examples.csproj @@ -0,0 +1,23 @@ + + + + Exe + net9.0 + enable + enable + CS8618;CS8603;CS8602;CS8604;CS9113 + Ivy.Aspose.OCR.Examples + + + + + + + + + + + + + + diff --git a/aspose-ocr/Ivy.Aspose.OCR.Examples.sln b/aspose-ocr/Ivy.Aspose.OCR.Examples.sln new file mode 100644 index 00000000..5963229b --- /dev/null +++ b/aspose-ocr/Ivy.Aspose.OCR.Examples.sln @@ -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 diff --git a/aspose-ocr/Program.cs b/aspose-ocr/Program.cs new file mode 100644 index 00000000..a2ad299e --- /dev/null +++ b/aspose-ocr/Program.cs @@ -0,0 +1,15 @@ + +using Ivy.Aspose.OCR.Examples.Apps; + +CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); +var server = new Server(); +#if DEBUG +server.UseHotReload(); +#endif +server.AddAppsFromAssembly(); +server.AddConnectionsFromAssembly(); +var chromeSettings = new ChromeSettings() + .DefaultApp() + .UseTabs(preventDuplicates: true); +server.UseChrome(chromeSettings); +await server.RunAsync(); diff --git a/aspose-ocr/README.md b/aspose-ocr/README.md new file mode 100644 index 00000000..bdc9f646 --- /dev/null +++ b/aspose-ocr/README.md @@ -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)