android-camera2 - 如何显示从 ML Kit 检测到的文本并将其显示在单词的实际位置
问题描述
我正在开发适用于 android 的 OCR 应用程序,并且正在使用 Camera2Api。我使用 android-camera2basic作为样板代码,并且我使用 ml 工具包进行文本识别。
我面临一个奇怪的问题,GraphicOverlay 没有正确缩放它只覆盖半屏。GraphicOverlay 没有在检测到的单词上正确绘制。
正如您所看到的,图形覆盖没有在它应该绘制的位置上绘制,例如“Stack Exchange Network”图形overaly 没有显示在 Stack Exchange Network 的顶部。
这是我的文件
解决方案
问题是该库检测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
编辑:抱歉,我注意到您没有在代码中使用位图,并且您的情况可能会有所不同,但我认为您可以根据这个想法找到解决方案,因为我遇到了与您相同的问题并且这有效非常适合我。
推荐阅读
- python - 使用复杂的嵌套 JSON 并验证 Excel 数据
- android - 如何实现从firebase到按钮的点击监听器
- c++ - 如何将无符号整数转换为 32 位二进制表示
- javascript - 鉴于 JavaScript 中的 Schläfli 符号,如何在庞加莱圆盘上绘制双曲镶嵌?
- pandoc - 在分类书目 (CSL) 中的参考类型之前生成标题
- python - 并排排列和打印numpy数组值
- android - 有没有办法以编程方式从 com.android.filemanager 安装 apk
- linux - Ubuntu 18.04 上 Docker 的 Elasticsearch 文件描述符错误
- sql - 多值参数在 SSRS 报告中不起作用
- javascript - 来自动态输入文本JS的总和