android - 如何使用 Mobile Vision API 获取图像中文本的位置?

标签 android xamarin imageview text-recognition vision-api

如何使用 Mobile Vision API 获取图像中文本在屏幕上的位置,以及如何在它们周围绘制一个矩形?

例子:

enter image description here

最佳答案

怎么做

在布局中放置一个ImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="250.0dp"
    android:minWidth="25px"
    android:minHeight="25px"
    android:id="@+id/imageView1" />
</LinearLayout>

onCreate方法中实例化ImageView

ImageView imgView;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        imgView = FindViewById<ImageView>(Resource.Id.imageView1);
        OCR();
    }

这是获取文本并在其上绘制矩形的重要代码

请阅读代码之上的注释

public void OCR()
    {
        //Convert The Image To Bitmap
        Bitmap bitmap = BitmapFactory.DecodeResource(ApplicationContext.Resources, Resource.Mipmap.lineindent);

        TextRecognizer textRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();

        if (!textRecognizer.IsOperational)
        {

            Log.Error("Main Activity", "Dependencies not available");

            // Check android for low storage so dependencies can be loaded, DEPRICATED CHANGE LATER
            IntentFilter intentLowStorage = new IntentFilter(Intent.ActionDeviceStorageLow);

            bool hasLowStorage = RegisterReceiver(null, intentLowStorage) != null;

            if (hasLowStorage)
            {

                Toast.MakeText(this, "Low Memory On Disk", ToastLength.Long);
                Log.Error("Main Activity", "Low Memory On Disk");
            }

        }
        else
        {
            Frame frame = new Frame.Builder().SetBitmap(bitmap).Build();


            SparseArray items = textRecognizer.Detect(frame);
            List<TextBlock> blocks = new List<TextBlock>();

            TextBlock myItem = null;
            for (int i = 0; i < items.Size(); ++i)
            {
                myItem = (TextBlock)items.ValueAt(i);

                //Add All TextBlocks to the `blocks` List
                blocks.Add(myItem);

            }
            //END OF DETECTING TEXT

            //The Color of the Rectangle to Draw on top of Text
            Paint rectPaint = new Paint();
            rectPaint.Color = Color.White;
            rectPaint.SetStyle(Paint.Style.Stroke);
            rectPaint.StrokeWidth = (4.0f);

            //Create the Canvas object,
            //Which ever way you do image that is ScreenShot for example, you 
            //need the views Height and Width to draw recatngles 
            //because the API detects the position of Text on the View
            //So Dimesnions are important for Draw method to draw at that Text 
            //Location
            Bitmap tempBitmap = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Rgb565);
            Canvas canvas = new Canvas(tempBitmap);
            canvas.DrawBitmap(bitmap, 0, 0, null);

            //Loop through each `Block`
            foreach (TextBlock textBlock in blocks)
            {
                IList<IText> textLines = textBlock.Components; 

                //loop Through each `Line`
                foreach (IText currentLine in textLines)
                {
                    IList<IText>  words = currentLine.Components;

                    //Loop through each `Word`
                    foreach (IText currentword in words)
                    {
                        //Get the Rectangle/boundingBox of the word
                        RectF rect = new RectF(currentword.BoundingBox);
                        rectPaint.Color = Color.Black;

                        //Finally Draw Rectangle/boundingBox around word
                        canvas.DrawRect(rect, rectPaint);

                        //Set image to the `View`
                        imgView.SetImageDrawable(new BitmapDrawable(Resources, tempBitmap));


                    }

                }
            }

        }
    }

结果

enter image description here

如果你想要 Lines 上的 Rectangle 然后从 words 循环中删除代码并将其放入 Lines 循环中,同样适用于 block

enter image description here

关于android - 如何使用 Mobile Vision API 获取图像中文本的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53141349/

相关文章:

android - 点燃火 : MediaPlayer: strange noises when playing multiple sounds

java - native 代码中的段错误

xamarin - 使用 FreshMVVM 实现 MasterDetailPage

Xamarin Android 将 Java.Lang.Enum 映射到 C# Enum

android - 我可以通过 cordova 插件覆盖 onCreate() 函数吗

ios - 我如何将内容置于 View Controller 的中心,以便无论 xamarin Storyboard 中使用什么设备尺寸,它始终居中?

android - 将 .setOnTouchListener() 分配给 xml 中定义的布局

android - 多线程缓存imageView加载

android - 对话框中的图像

android - BoringLayout 的可用性