- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能告诉我如何在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();
}
}
最佳答案
关于Java:如何在 CameraX previewView 上绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64013921/
我正在制作一个应用程序,其中有两个文件 CameraVC.swift import UIKit class CameraVC: CameraViewController { @IBOutlet wea
我正在尝试使用 androidx.camera 包创建一个按需功能模块。当 Activity 使包含 PreviewView 的布局膨胀时,我遇到了崩溃。请注意,如果我只使用像 TextView 这样
我有一个 PreviewView 实例,我想根据使用 Firebase 和 Image Analyzer 用例的对象标识在该实例上绘制一个矩形。 但是,与 Text/SurfaceView 不同,Pr
我遵循了这些教程: https://developer.android.com/training/camerax/preview , https://proandroiddev.com/update-
camerax cameraView 和 previewView 有什么区别?因为我找不到关于比较 2 个 View 以预览 camerax 的资源或文章 最佳答案 PreviewView 是一个自定
免责声明:我知道 this question 的存在,但它目前仍未解决,我正在尝试提供额外的信息,而不是用无论如何都无法解决问题的无用答案污染那个信息。 我有一个带有默认镜像的前置摄像头的自定义设备,
谁能告诉我如何在cameraX预览上绘制一个矩形?我尝试按照过去用 kotlin 编写的堆栈溢出答案的建议实现自定义 View ,但是当我尝试将其转换为 Java 时,它似乎对我不起作用。 XML 文
我是 Andriod 的初学者,现在遇到问题。 如何使用 CameraX 和 ZXing 创建 QR 码扫描仪在 here 中进行了描述。和 here 。我正在尝试制作一个示例项目。 但是我的代码有错
我试图使用 CameraX PreviewView Composable 的内部通过 AndroidView ,但预览被拉伸(stretch),右半部分被剪裁,正如您在 screenshot 中看到的
我有一个简单的设置: preview = new Preview.Builder().build(); preview.setSurfaceProvider(mPreviewView.createSu
我正在尝试使用图像捕获中包含的叠加层来捕获图片。我能够将覆盖设置为 previewView使用 cameraView.overlay.add(binding.textView) .但是,当尝试使用 i
我正在尝试通过动画实现效果,但只有在预览 View 处于 Activity 状态后才能实现。原因之一也是为了避免相机绑定(bind)后出现闪烁。 我已经设法使用反射 (androidx.camera.
我有一个 PreviewView除了工具栏外,它占据了整个屏幕。 相机的预览效果很好,但是当我捕捉图像时,纵横比完全不同。 我想在成功捕获图像后将图像显示给用户,因此它与 PreviewView 的大
这是我的代码,我遇到了错误。请检查并告诉我如何解决这个问题。 func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMar
我想在没有任何 xml 和 PreviewView 或其他显示图片的情况下使用 CameraX 拍照,但我有问题。 最佳答案 你应该使用Camera2,这个问题是错误的 关于android - 如何在
我正在使用 Google 的 API2 相机,但我的代码遇到了一些问题。我有两个不同的 CameraSession,一个用于视频,另一个用于图像。为了更高效地执行此操作,我更改了代码以使用唯一的 Se
我是一名优秀的程序员,十分优秀!