小编How*_*den的帖子

Java:如何在 CameraX previewView 上绘图?

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

主要活动提取

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 …
Run Code Online (Sandbox Code Playgroud)

java android android-camerax

5
推荐指数
1
解决办法
1011
查看次数

标签 统计

android ×1

android-camerax ×1

java ×1