Java:如何在CameraX预览视图上绘制?

6

有人能向我展示如何在cameraX预览上绘制矩形吗?我尝试实现自定义视图,正如以前在kotlin中撰写的stackoverflow答案建议的那样,但是当我尝试将其转换为Java时,它似乎对我不起作用。

XML文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/my_root">

    <com.example.cameraxxx.RectOverlay
        android:id="@+id/rectOverlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.camera.view.PreviewView
        android:id="@+id/previewView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="preview_area"
        android:importantForAccessibility="no"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

    </androidx.camera.view.PreviewView>


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="13dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/previewView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:visibility="visible" />


</androidx.constraintlayout.widget.ConstraintLayout>

主活动提取

imageAnalysis.setAnalyzer(executor, new ImageAnalysis.Analyzer() {
            @SuppressLint("UnsafeExperimentalUsageError")
            @Override
            public void analyze(@NonNull ImageProxy imageProxy) {
                int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();
                FaceDetectorOptions realTimeOpts =
                        new FaceDetectorOptions.Builder()
                                .setPerformanceMode(1).enableTracking()
                                .build();
                
                Image mediaImage = imageProxy.getImage();
                if(mediaImage != null) {
                    InputImage image = InputImage.fromMediaImage(mediaImage,imageProxy.getImageInfo().getRotationDegrees());
                    FaceDetector detector = FaceDetection.getClient(realTimeOpts);
                    Task<List<Face>> result =
                            detector.process(image)
                                    .addOnSuccessListener(
                                            new OnSuccessListener<List<Face>>() {
                                                @Override
                                                public void onSuccess(List<Face> faces) {
                                                    // Task completed successfully
                                                    // ...
                                                    if(faces.size() !=0){
                                                        rectOverlay.setAlpha(1f);
                                                        Toast.makeText(MainActivity.this,"Face detected",Toast.LENGTH_SHORT).show();
                                                        Canvas canvas = new Canvas();
                                                        for (Face face: faces){
                                                            rectOverlay.drawOverlay(face,canvas);
                                                        }

                                                    }
                                                }
                                            })
                                    .addOnFailureListener(
                                            new OnFailureListener() {
                                                @Override
                                                public void onFailure(@NonNull Exception e) {
                                                    // Task failed with an exception
                                                    // ...
                                                    Log.d("y123","nno face");
                                                }
                                            })
                                    .addOnCompleteListener(
                                            new OnCompleteListener<List<Face>>() {
                                                @Override
                                                public void onComplete(@NonNull Task<List<Face>> task) {
                                                    imageProxy.close();
                                                }
                                            });
                }
            }
        });
        cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, imageAnalysis, preview);
        preview.setSurfaceProvider(mPreviewView.createSurfaceProvider());
    }

RectOverlay 类

package com.example.cameraxxx;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.google.mlkit.vision.face.Face;

public class RectOverlay extends View {

    private Paint paint;

    public RectOverlay(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.FILL);
    }

    public void drawOverlay(Face face, Canvas canvas){
        canvas.drawRect(face.getBoundingBox().left,face.getBoundingBox().top, face.getBoundingBox().right, face.getBoundingBox().bottom, paint);
        invalidate();

    }
}

2
覆盖层的宽度和高度应与“PreviewView”相同,即覆盖层应填充其父级。因此,不要使用“wrap_content”,而是使用“0dp”,以便在视图上设置约束条件。此外,在xml布局文件中,在“PreviewView”之后设置覆盖层。 - Husayn Hakeem
“不起作用”是什么意思?您是否遇到了构建或运行时错误?如果有,它们显示了什么?您可以参考此MLKit示例,该示例在每个检测到的对象周围绘制边界框。 - topher217
请参考以下链接:https://dev59.com/pb3pa4cB1Zd3GeqPakBm#68208561 - Bruce
1个回答

0
  1. 在 XML 中使用 match_parent 来设置 RectOverlay 的宽度和高度
  2. 将 RectOverlay 放置在 PreviewView 下方的位置

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接