我将CameraX的分析器用例与 MLKit 的BarcodeScanner. 我想先裁剪从相机接收到的图像的一部分,然后再将其传递到扫描仪。
我现在正在做的是将ImageProxy(我在分析器中收到的)转换为Bitmap,裁剪它,然后将其传递给BarcodeScanner. 缺点是这不是一个非常快速和有效的过程。
我还注意到运行此代码时 Logcat 中收到的警告:
ML Kit 检测到您似乎将相机帧作为位图对象传递给检测器。这是低效的。请对camera2 API使用YUV_420_888格式,对(旧)相机API使用NV21格式,并将字节数组直接传递给ML Kit。
不进行转换固然很好ImageProxy,但是如何裁剪我想要分析的矩形呢?
我已经尝试过设置(imageProxy.image.cropRect) 类cropRect的字段Image,但它似乎并不影响最终结果。
android android-image android-camera2 android-camerax google-mlkit
I want to be able to get the exact image for analysis step as I get in preview. I have preview use case:
val metrics = DisplayMetrics().also { binding.codeScannerView.display.getRealMetrics(it) }
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(screenAspectRatio)
}.build()
Run Code Online (Sandbox Code Playgroud)
And next to it I have analysis use case config:
val analyzerConfig = ImageAnalysisConfig.Builder().apply {
setTargetResolution(Size(metrics.heightPixels, metrics.widthPixels))
setTargetAspectRatio(screenAspectRatio)
val analyzerThread = HandlerThread(
"QrCodeReader").apply { start() }
setCallbackHandler(Handler(analyzerThread.looper))
setImageReaderMode(
ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
}.build()
Run Code Online (Sandbox Code Playgroud)
My preview is fullscreen so it's size is …
谁能告诉我如何在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) 我有一个PreviewView占据整个屏幕但工具栏除外。相机的预览效果很好,但是当我拍摄图像时,纵横比完全不同。
我想在成功捕获后向用户显示图像,因此它的大小与 相同,PreviewView因此我不必裁剪或拉伸它。
是否可以在每个设备上更改纵横比,它是它的大小PreviewView还是我必须将其设置为固定值?