protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (requestCode == PickImageID && resultCode == Result.Ok && data != null)
            {
                Android.Net.Uri uri = data.Data;
                cameraSource.Stop();
                surfaceView.Visibility   = ViewStates.Gone;
                selectedimage.Visibility = ViewStates.Visible;
                mainLayout.Visibility    = ViewStates.Gone;
                captureLayout.Visibility = ViewStates.Visible;
                selectedimage.SetImageURI(uri);

                Bitmap currentImage = MediaStore.Images.Media.GetBitmap(this.ContentResolver, uri);
                selectedimage.SetImageBitmap(currentImage);
                Frame         frame         = new Frame.Builder().SetBitmap(currentImage).Build();
                SparseArray   items         = textRecognizer.Detect(frame);
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < items.Size(); i++)
                {
                    stringBuilder.Append(((TextBlock)items.ValueAt(i)).Value);
                    stringBuilder.Append(" ");
                }
                textView.Text = stringBuilder.ToString();
            }
        }
Beispiel #2
0
        /// <summary>
        /// <see cref="LabelReaderBase.GetFullTextFromImage(object)"/>
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public override Task <string> GetFullTextFromImageAsync(object image)
        {
            byte[] imageBytes = (byte[])image;
            TaskCompletionSource <string> taskCompletionSource = new TaskCompletionSource <string>();
            StringBuilder stringBuilder = new StringBuilder();

            using (Bitmap imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length))
                using (global::Android.Gms.Vision.Frame frame = new global::Android.Gms.Vision.Frame.Builder().SetBitmap(imageBitmap).Build()) {
                    SparseArray   textArray     = TextRecognizer.Detect(frame);
                    List <string> valuesInOrder = OrderTextBlocks(textArray);
                    foreach (string value in valuesInOrder)
                    {
                        stringBuilder.Append(value);
                    }
                    taskCompletionSource.SetResult(stringBuilder.ToString());
                }
            return(taskCompletionSource.Task);
        }
Beispiel #3
0
        private async Task <string> GetTextFromImageAsync(Bitmap image)
        {
            var task = Task.Run(() =>
            {
                var frame            = new Frame.Builder().SetBitmap(image).Build();
                var items            = textRecognizer.Detect(frame);
                var ocrResultBuilder = new StringBuilder();
                for (var i = 0; i < items.Size(); i++)
                {
                    var item = (TextBlock)items.ValueAt(i);
                    ocrResultBuilder.Append(item.Value);
                    ocrResultBuilder.Append(" ");
                }
                var ocrResult = ocrResultBuilder.ToString();
                return(ocrResult);
            });

            return(await task);
        }
Beispiel #4
0
        private void BtnGetTextOnClick(object sender, EventArgs e)
        {
            txtRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();
            if (!txtRecognizer.IsOperational)
            {
                txt_text.Text = "Error";
            }
            else
            {
                Frame         fr        = new Frame.Builder().SetBitmap(bitmap1).Build();
                SparseArray   items     = txtRecognizer.Detect(fr);
                StringBuilder stBuilder = new StringBuilder();
                for (int i = 0; i < items.Size(); i++)
                {
                    TextBlock item = (TextBlock)items.ValueAt(i);
                    stBuilder.Append(item.Value);
                    stBuilder.Append("\n");
                }

                txt_text.Text = stBuilder.ToString();
            }
        }
Beispiel #5
0
 private void Ler(object sender, EventArgs e)
 {
     if (bitmap != null)
     {
         if (!textRecognizer.IsOperational)
         {
         }
         else
         {
             Frame         frame = new Frame.Builder().SetBitmap(bitmap).Build();
             var           items = textRecognizer.Detect(frame);
             StringBuilder sb    = new StringBuilder();
             for (int i = 0; i < items.Size(); i++)
             {
                 var textBlock = items.ValueAt(i) as TextBlock;
                 sb.Append(textBlock.Value);
                 sb.Append("\n");
             }
             txt.Text = sb.ToString();
         }
     }
 }
Beispiel #6
0
        public Task <ScanResult> ProcessImage(Stream bitmapStream)
        {
            if (!_recognizer.IsOperational)
            {
                throw new Exception("Recognizer not operational.");
            }

            using var bitmap  = BitmapFactory.DecodeStream(bitmapStream);
            using var frame   = new Frame.Builder().SetBitmap(bitmap).Build();
            using var results = _recognizer.Detect(frame);
            var result = new ScanResult();

            for (var i = 0; i < results.Size(); i++)
            {
                var androidBlock = (TextBlock)results.ValueAt(i);
                if (androidBlock != null)
                {
                    result.Add(GetBlock(androidBlock));
                }
            }

            return(Task.FromResult(result));
        }
Beispiel #7
0
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            if (requestCode == PHOTO_REQUEST && resultCode == Result.Ok)
            {
                LaunchMediaScanIntent();
                try
                {
                    Bitmap bitmap = DecodeBitmapUri(ApplicationContext, imageUri);
                    if (detector.IsOperational && bitmap != null)
                    {
                        Frame         frame      = new Frame.Builder().SetBitmap(bitmap).Build();
                        SparseArray   textBlocks = detector.Detect(frame);
                        System.String blocks     = "";
                        System.String lines      = "";
                        System.String words      = "";
                        var           regexMoney = new Regex(@"\$ ?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$*");
                        for (int index = 0; index < textBlocks.Size(); index++)
                        {
                            //extract scanned text blocks here
                            TextBlock tBlock = (TextBlock)textBlocks.ValueAt(index);
                            blocks = blocks + tBlock.Value + " " + "\n";
                            var   text  = Regex.Replace(blocks.ToString(), "[A-Za-z ]", " ");
                            Match match = regexMoney.Match(text);
                            if (match.Success)
                            {
                                scanResults.Text = match.Value;
                                break;
                            }
                            else
                            {
                                scanResults.Text = "No Price.";
                            }

                            //    foreach (IText line in tBlock.Components)
                            //    {
                            //        //extract scanned text lines here
                            //        lines = lines + line.Value + "\n";
                            //        foreach (IText element in line.Components)
                            //        {
                            //            //extract scanned text words here
                            //            words = words + element.Value + ", ";
                            //        }
                            //    }
                            //}
                            //if (textBlocks.Size() == 0)
                            //{
                            //    scanResults.Text = "Scan Failed: Found nothing to scan";
                            //}
                            //else
                            //{



                            //    scanResults.Text = "Assim funcaaaaaaaaaaa";
                            //    //scanResults.Text = scanResults.Text + "Blocks: " + "\n";
                            //    //scanResults.Text = scanResults.Text + blocks + "\n";
                            //    //scanResults.Text = scanResults.Text + "---------" + "\n";
                            //    //scanResults.Text = scanResults.Text + "Lines: " + "\n";
                            //    //scanResults.Text = scanResults.Text + lines + "\n";
                            //    //scanResults.Text = scanResults.Text + "---------" + "\n";
                            //    //scanResults.Text = scanResults.Text + "Words: " + "\n";
                            //    //scanResults.Text = scanResults.Text + words + "\n";
                            //    //scanResults.Text = scanResults.Text + "---------" + "\n";
                            //}
                        }
                        //else
                        //{
                        //    scanResults.Text = "Could not set up the detector!";
                        //}
                    }
                }
                catch (System.Exception e)
                {
                    Log.Error("OnActivityResult", e.StackTrace);
                }
            }
        }
        private void CropAndParse()
        {
            MainActivity.DetectedMotoModel = "";
            MainActivity.DetectedPlate     = "";

            if (!txtRecognizer.IsOperational)
            {
                // Log.Error("Error", "Detector dependencies are not yet available");
            }
            else
            {
                var maxscore = MainActivity.PlateAndMotoStats.Scores.Max();

                if (maxscore > MainActivity.SCORE_THRESHOLD)
                {
                    var i0     = MainActivity.PlateAndMotoStats.Scores.ToList().IndexOf(maxscore);
                    var bboxes = MainActivity.PlateAndMotoStats.BoundingBoxes.ToList();

                    var ymin = bboxes[i0 * 4 + 0] * 300;
                    var xmin = bboxes[i0 * 4 + 1] * 300;
                    var ymax = bboxes[i0 * 4 + 2] * 300;
                    var xmax = bboxes[i0 * 4 + 3] * 300;

                    if (xmin < 0)
                    {
                        xmin = 0;
                    }
                    if (ymin < 0)
                    {
                        ymin = 0;
                    }
                    if (xmax > 300)
                    {
                        xmax = 300;
                    }
                    if (ymax > 300)
                    {
                        ymax = 300;
                    }

                    var w = xmax - xmin;
                    var h = ymax - ymin;

                    using (var builder = new Frame.Builder())
                    {
                        var cfg = Android.Graphics.Bitmap.Config.Argb8888;
                        using (var bmp = Android.Graphics.Bitmap.CreateBitmap(CopyColorsToArray(imgptr), 300, 300, cfg))
                        {
                            using (var bmpc = Android.Graphics.Bitmap.CreateBitmap(bmp, (int)xmin, (int)ymin, (int)(w), (int)(h)))
                            {
                                if (previewbmp != null)
                                {
                                    previewbmp.Dispose();
                                    previewbmp = null;
                                }
                                previewbmp = bmpc.ToSKBitmap().ToBitmap();
                                var frame      = builder.SetBitmap(bmpc).Build();
                                var items      = txtRecognizer.Detect(frame);
                                var strBuilder = new StringBuilder();
                                var plate      = "";
                                for (int i = 0; i < items.Size(); i++)
                                {
                                    var item = (TextBlock)items.ValueAt(i);
                                    var str  = item.Value.Replace('\n', ' ');
                                    plate = str;
                                    strBuilder.Append("i = " + i + ": " + str + "|");
                                    if (i > 0)
                                    {
                                        break;
                                    }
                                }
                                plate = ParsePlate(plate);
                                Console.WriteLine(plate);
                                if (CheckIsValidPlate(plate))
                                {
                                    MainActivity.DetectedPlate = plate;
                                    MainActivity.context.RunOnUiThread(() =>
                                    {
                                        var imgView = MainActivity.context.FindViewById <ImageView>(Resource.Id.imageViewPlatePreview);
                                        imgView.SetImageBitmap(previewbmp);
                                        var txtView  = MainActivity.context.FindViewById <TextView>(Resource.Id.txtObjID);
                                        txtView.Text = "Placa Detectada: " + plate + " (" + (maxscore * 100).ToString("N2") + "%)";
                                    });
                                    if (MainActivity.SavedDataStore.PlateNumbers.Contains(plate))
                                    {
                                        Xamarin.Essentials.Vibration.Vibrate(2000);
                                        StartMedia("warning.mp3");
                                        MainActivity.context.RunOnUiThread(() =>
                                        {
                                            var layout = MainActivity.context.FindViewById <LinearLayout>(Resource.Id.linearLayoutLabelPanel);
                                            layout.SetBackgroundColor(Android.Graphics.Color.ParseColor("#89ff0000"));
                                            var txtView = MainActivity.context.FindViewById <TextView>(Resource.Id.txtObjID);
                                            txtView.SetTextColor(Android.Graphics.Color.Black);
                                            var imgwarning        = MainActivity.context.FindViewById <ImageButton>(Resource.Id.imageButtonWarning);
                                            imgwarning.Visibility = Android.Views.ViewStates.Visible;
                                        });
                                    }
                                    else
                                    {
                                        MainActivity.context.RunOnUiThread(() =>
                                        {
                                            var layout = MainActivity.context.FindViewById <LinearLayout>(Resource.Id.linearLayoutLabelPanel);
                                            layout.SetBackgroundColor(Android.Graphics.Color.ParseColor("#bc000000"));
                                            var txtView = MainActivity.context.FindViewById <TextView>(Resource.Id.txtObjID);
                                            if (maxscore < 0.7)
                                            {
                                                txtView.SetTextColor(Android.Graphics.Color.Red);
                                            }
                                            else if (maxscore < 0.85)
                                            {
                                                txtView.SetTextColor(Android.Graphics.Color.Yellow);
                                            }
                                            else
                                            {
                                                txtView.SetTextColor(Android.Graphics.Color.LightGreen);
                                            }
                                        });
                                        if (MainActivity.SavedDataStore.Vibrate)
                                        {
                                            Xamarin.Essentials.Vibration.Vibrate(200.0);
                                        }
                                        if (MainActivity.SavedDataStore.PlaySound)
                                        {
                                            StartMedia("pop.m4a");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }