0

I'm using this code to capture an image using the mobile camera, display it and store it on the device then after clicking another button the text in the captured image will be extracted in label named TextView



    public partial class MainPage : ContentPage
    {
        private MediaFile photo;
        private string photopath;
        public MainPage()
        {
            InitializeComponent();
        }
        
        private async void CameraButton_Clicked(object sender, EventArgs e)
        {
            

        var cameraMediaOptions = new StoreCameraMediaOptions
            {
                DefaultCamera = CameraDevice.Rear,
                SaveToAlbum = true,
                Directory = "MyAppName",
                Name = null,
                CompressionQuality = 100
            };
            photo = await CrossMedia.Current.TakePhotoAsync(cameraMediaOptions);
           
            if (photo == null) return;
            PhotoImage.Source = ImageSource.FromStream(() => photo.GetStream());
        }

        private async void TextButton_Clicked(object sender, EventArgs e)
        {
            if (photo != null)
            {
                var ocr = new IronTesseract();
                var result = await ocr.ReadAsync(photopath);
                TextView.Text = result.Text;
                if (string.IsNullOrWhiteSpace(result.Text))
                {
                    TextView.Text = "No Text Found";
                    return;
                }
            }
            else
            {
                await DisplayAlert("Please Take Photo First", "", "OK");
                return;
            }
        }

    }

what happend is after running the app i click on text extraction button befor capturing photo and the alert displays "Please Take Photo First appear", then capturing the photo and display it and everything is fine, but after capturing the image when in click the text extraction button suddenly the app break and no enough information is displayed, tried to use chatGPT to check the problem since I am begginer and it said that it might be in the way I defiend the image path and feeding it to the IronOcr library, please help me here is pic of the break mode message : enter image description here

Abode
  • 1

1 Answers1

0

photopath get declared as a variable and it was never given the value. Therefore, it is like passing null value to ReadAsync method.

enter image description here

Read and ReadAsync methods in IronOCR take OcrInput object as input. Where OcrInput object accept a varieties of input including filepath, bitmap, stream, image, and cropRectangle. Changing photopath to OcrInput object will make sure the correct input into the Read and ReadAsync methods:

var ocr = new IronTesseract();
using (var ocrInput = new OcrInput(photo.GetStream()))
{
var result = await ocr.ReadAsync(photopath);
Console.WriteLine(ocrResult.Text);
}

Please visit IronOCR for more information and guide.