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+ }
0 commit comments