【问题标题】:Drawing canvas within ImageAnalyzer使用图像分析器绘制画布
【发布时间】:2021-07-24 03:17:18
【问题描述】:

我有一个简单的布局:

<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>

看起来像这样:

如您所见,我有一个 ImageView foundObject,我想用它来显示我从自定义 ImageAnalyzer 收到的找到对象的边框:

public class PaperImageAnalyser implements ImageAnalysis.Analyzer {


    ObjectDetectorOptions options = new ObjectDetectorOptions.Builder()
        .setDetectorMode(ObjectDetectorOptions.STREAM_MODE)
        .enableClassification()  // Optional
        .build();

    ObjectDetector objectDetector = ObjectDetection.getClient(options);



    @Override
    public void analyze(@NonNull ImageProxy imageProxy) {
    @SuppressLint("UnsafeExperimentalUsageError") Image mediaImage = imageProxy.getImage();

    if (mediaImage != null) {
        InputImage image =InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());

        objectDetector.process(image)
            .addOnSuccessListener(
                new OnSuccessListener<List<DetectedObject>>() {
                    @Override
                    public void onSuccess(List<DetectedObject> detectedObjects) {
            
                        for (DetectedObject detectedObject : detectedObjects) {
                            Rect boundingBox = detectedObject.getBoundingBox();

                            Log.e("received",""+ boundingBox);
                            Paint paint = new Paint();
                            paint.setAntiAlias(true);
                            paint.setStyle(Paint.Style.STROKE);
                            paint.setColor(Color.RED);
                            paint.setStrokeWidth(10f);

                            Canvas canvas = new Canvas();
                            canvas.drawRect( boundingBox.left,  boundingBox.top, boundingBox.right,    boundingBox.bottom,   paint   );
                            CameraImp.foundObject.draw(canvas); //CameraImp = my fragment, foundObject is the static imageview

                        }

                    }
                })
            .addOnFailureListener(
                new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e("error",""+e.getMessage());
                    }
                })
            .addOnCompleteListener(new OnCompleteListener<List<DetectedObject>>() {
                @SuppressLint("UnsafeExperimentalUsageError")
                @Override
                public void onComplete(@NonNull Task<List<DetectedObject>> task) {
                    imageProxy.getImage().close();
                    imageProxy.close(); 

                }
            });
        }
    }
}

问题是我收到了被识别的对象,例如:

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 不显示对象的形状。

问题是原因是什么。是因为我从自定义 ImageAnalyzer 收到了错误的尺寸,还是因为我绘制了错误的形状?

【问题讨论】:

    标签: android canvas imageview android-camerax google-mlkit


    【解决方案1】:

    由于预览和分析的图像大小不同,您需要获取分析图像的宽度和高度,并将边界框转换为正确的大小以进行显示。请参考 mlkit vision_quickstart 中的这个示例作为参考。 https://github.com/googlesamples/mlkit/blob/74d5edb101d1e2fb8bd404c41a684b71a06d507a/android/vision-quickstart/app/src/main/java/com/google/mlkit/vision/demo/java/CameraXLivePreviewActivity.java#L421

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2011-08-07
      • 2012-06-06
      • 2012-10-08
      • 1970-01-01
      相关资源
      最近更新 更多