首页 > 解决方案 > 如何显示从 ML Kit 检测到的文本并将其显示在单词的实际位置

问题描述

我正在开发适用于 android 的 OCR 应用程序,并且正在使用 Camera2Api。我使用 android-camera2basic作为样板代码,并且我使用 ml 工具包进行文本识别。

我面临一个奇怪的问题,GraphicOverlay 没有正确缩放它只覆盖半屏。GraphicOverlay 没有在检测到的单词上正确绘制。

点击这里查看问题

正如您所看到的,图形覆盖没有在它应该绘制的位置上绘制,例如“Stack Exchange Network”图形overaly 没有显示在 Stack Exchange Network 的顶部。

这是我的文件

标签: android-camera2firebase-mlkittext-recognitionandroid-graphicscamera-api

解决方案


问题是该库检测bitmap您通过的原件上的单词(以其原始宽度和高度),然后根据该图像返回单词的位置。但屏幕上显示的图像是 SCALED 图像,具有新的宽度和高度以适合屏幕或imageView. 所以你想要做的是,bitmap根据你的重新调整imageView然后通过新的bitmap

//get the scaled bitmap image with the new width and height showed on the screen
    private Bitmap getScaledBitmap (Bitmap bitmapImage){

        //width and height of original image
        final int imageWidth = bitmapImage.getWidth();
        final int imageHeight = bitmapImage.getHeight();

        //width and height of the imageView
        final int imageViewWidth  = mImageView.getMeasuredWidth();
        final int imageViewHeight = mImageView.getMeasuredHeight();

        final int scaledWidth , scaledHeight;


        if (imageWidth*imageViewHeight <= imageHeight*imageViewWidth) {

            //rescaled width and height of image within ImageView
            scaledWidth = (imageWidth*imageViewHeight)/imageHeight;
            scaledHeight = imageViewHeight;
        }
        else {
            //rescaled width and height of image within ImageView
            scaledWidth = imageViewWidth;
            scaledHeight = (imageHeight*imageViewWidth)/imageWidth;
        }


        return Bitmap.createScaledBitmap(bitmapImage, scaledWidth, scaledHeight, true);
    }

我用这个答案来缩放图像:https ://stackoverflow.com/a/13318469/9242141


编辑:抱歉,我注意到您没有在代码中使用位图,并且您的情况可能会有所不同,但我认为您可以根据这个想法找到解决方案,因为我遇到了与您相同的问题并且这有效非常适合我。


推荐阅读