gpt4 book ai didi

Java:如何在 CameraX previewView 上绘图?

转载 作者:行者123 更新时间:2023-12-04 11:44:30 26 4
gpt4 key购买 nike

谁能告诉我如何在cameraX预览上绘制一个矩形?我尝试按照过去用 kotlin 编写的堆栈溢出答案的建议实现自定义 View ,但是当我尝试将其转换为 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>
MainActivity 提取
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();

}
}

最佳答案

  • XML 中 RectOverlay 的宽高使用 match_parent
  • 将 RectOverlay 放在 XML 中 PreviewView 的下方
  • 关于Java:如何在 CameraX previewView 上绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64013921/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com