Drawing canvas within ImageAnalyzer

Viewed 131

I've got a simple layout:

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

Which looks like this:

layout

As you can see I have an ImageView foundObject which I want to use to display the border of the found object which I receive from my custom 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(); 

                }
            });
        }
    }
}

The problem is that I receive the objects which get recognized for e.g:

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)

but the ImageView dont display the shape of the object.

The question is what the cause is. Is it cause I receive the wrong dimensions from my custom ImageAnalyzer or is it cause I draw the shape wrong?

1 Answers
Related