halkar / Tesseract.Xamarin

Tesseract OCR wrapper for Xamarin
MIT License
121 stars 40 forks source link

Tesseract.Droid.TesseractApi.Init hangs #27

Closed pandrija closed 8 years ago

pandrija commented 8 years ago

Started from Xamarin default Android app template, just added the tessdata files for English and called Init method, but execution hangs on Init method....

public class MainActivity : Activity
    {
        int count = 1;

        Tesseract.Droid.TesseractApi ocrApi;

        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);

            this.ocrApi = new Tesseract.Droid.TesseractApi(this,
                Tesseract.Droid.AssetsDeployment.OncePerVersion);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };

            var result = this.ocrApi.Init("eng").Result;
        }
    }
}

image

Here's the debugger output (last part): [TesseractApi] Copy assets to /storage/emulated/0/Android/data/com.companyname.labelscanner/files Thread started: #2 Thread started: #3 [Mono] [0x7f58a19450] worker starting Thread started: #4 [Mono] [0x7f58814450] worker starting [Mono] DllImport attempting to load: '/system/lib64/libc.so'. [Mono] DllImport loaded library '/system/lib64/libc.so'. [Mono] DllImport searching in: '/system/lib64/libc.so' ('/system/lib64/libc.so'). [Mono] Searching for 'gettid'. [Mono] Probing 'gettid'. [Mono] Found as 'gettid'. Thread started: #5 [Mono] [0x7f585fc450] worker starting [Mono] [0x7f58a19450] hill climbing, change max number of threads 8 [Mono] [0x7f58a19450] worker finishing Thread finished: #3 Thread finished: #2 [Mono] [0x7f58814450] worker finishing Thread finished: #4 Thread finished: #5 [Mono] [0x7f585fc450] worker finishing

halkar commented 8 years ago

@pandrija thanks, will have a look. And sorry for the delay, my mail filter gone wild.

halkar commented 8 years ago

Don't use .Result. Instead use async/await

    [Activity (Label = "tesseract.test", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : Activity
    {
        int count = 1;
        Tesseract.Droid.TesseractApi ocrApi;

        protected override async void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);

            this.ocrApi = new Tesseract.Droid.TesseractApi (this,
                Tesseract.Droid.AssetsDeployment.OncePerInitialization);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };

            var result = await this.ocrApi.Init ("eng");
        }
    }
pandrija commented 8 years ago

Thx - works great!