Android 在ImageAnalyzer中绘制画布
我有一个简单的布局:Android 在ImageAnalyzer中绘制画布,android,canvas,imageview,android-camerax,google-mlkit,Android,Canvas,Imageview,Android Camerax,Google Mlkit,我有一个简单的布局: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#FF000000"
app:layout_constraintDimensionRatio="3:4"
tools:context=".pkgActivity.CameraImp">
<RelativeLayout
android:id="@+id/myP"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintDimensionRatio="3:4"
>
<androidx.camera.view.PreviewView
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/preview_area"
android:importantForAccessibility="no">
</androidx.camera.view.PreviewView>
<ImageView
android:id="@+id/foundObject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/pic_desc"
android:visibility="visible" />
<ImageView
android:id="@+id/captureImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:layout_constraintDimensionRatio="3:4"
android:scaleX="-1"
android:contentDescription="@string/capture"
android:src="@mipmap/ic_launcher" />
<Button
android:id="@+id/buttonGalleryImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Gallery" />
<Button
android:id="@+id/buttonChangeCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="camera" />
<Button
android:id="@+id/buttonGoBackTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="goback" />
<Button
android:id="@+id/buttonPaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="goback" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
问题是我接收到的对象已被识别,例如:
E/received: Rect(233, 0 - 720, 1309)
E/received: Rect(237, 0 - 720, 1307)
E/received: Rect(235, 0 - 720, 1304)
E/received: Rect(233, 0 - 720, 1303)
E/received: Rect(229, 0 - 720, 1299)
E/received: Rect(222, 0 - 720, 1293)
E/received: Rect(214, 0 - 720, 1289)
E/received: Rect(207, 0 - 720, 1285)
E/received: Rect(203, 0 - 720, 1285)
E/received: Rect(201, 0 - 720, 1287)
E/received: Rect(201, 0 - 720, 1290)
E/received: Rect(204, 0 - 720, 1294)
E/received: Rect(208, 0 - 720, 1297)
E/received: Rect(210, 0 - 720, 1294)
E/received: Rect(199, 0 - 720, 1271)
E/received: Rect(212, 0 - 720, 1219)
但是ImageView不显示对象的形状
问题是原因是什么。这是因为我从自定义图像分析器接收到错误的尺寸,还是因为我绘制的形状错误?由于预览和分析的图像的大小不同,您需要获得分析图像的宽度和高度,并将边界框转换为正确的大小进行显示。请参考mlkit vision_quickstart中的此示例
E/received: Rect(233, 0 - 720, 1309)
E/received: Rect(237, 0 - 720, 1307)
E/received: Rect(235, 0 - 720, 1304)
E/received: Rect(233, 0 - 720, 1303)
E/received: Rect(229, 0 - 720, 1299)
E/received: Rect(222, 0 - 720, 1293)
E/received: Rect(214, 0 - 720, 1289)
E/received: Rect(207, 0 - 720, 1285)
E/received: Rect(203, 0 - 720, 1285)
E/received: Rect(201, 0 - 720, 1287)
E/received: Rect(201, 0 - 720, 1290)
E/received: Rect(204, 0 - 720, 1294)
E/received: Rect(208, 0 - 720, 1297)
E/received: Rect(210, 0 - 720, 1294)
E/received: Rect(199, 0 - 720, 1271)
E/received: Rect(212, 0 - 720, 1219)