如何拍摄与Android相机预览中所示尺寸相同的尺寸

问题描述 投票:0回答:1

捕获的图像的尺寸和比例与您在预览屏幕中看到的不同。

我想捕捉与预览中所示相同的尺寸。

相机预览屏幕位于非全屏视图之间。 The blue area is the camera preview area

布局代码如下:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <androidx.camera.view.PreviewView
        android:id="@+id/preview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="120dp"
        app:layout_constraintBottom_toTopOf="@id/bottom"
        app:layout_constraintEnd_toStartOf="@id/right"
        app:layout_constraintStart_toEndOf="@id/left"
        app:layout_constraintTop_toBottomOf="@id/top" />

    <ImageView
        android:id="@+id/photo_preview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="120dp"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@id/bottom"
        app:layout_constraintEnd_toStartOf="@id/right"
        app:layout_constraintStart_toEndOf="@id/left"
        app:layout_constraintTop_toBottomOf="@id/top" />

    <ImageView
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/camera"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tint="@color/white" />

    <ImageView
        android:id="@+id/left"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/camera"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tint="@color/white" />

    <ImageView
        android:id="@+id/right"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/camera"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tint="@color/white" />

    <ImageView
        android:id="@+id/bottom"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/camera"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:tint="@color/white" />

</androidx.constraintlayout.widget.ConstraintLayout>

拍照代码

private fun takePhoto() {
    imageCapture.takePicture(ContextCompat.getMainExecutor(this), object :
        ImageCapture.OnImageCapturedCallback() {
        override fun onCaptureSuccess(image: ImageProxy) {
            val buffer = image.planes[0].buffer
            val bytes = ByteArray(buffer.remaining())
            buffer.get(bytes)

            val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
            
            
            // how to resize??
            

            binding.photoPreview.isVisible = true
            binding.photoPreview.post {
                binding.photoPreview.setImageBitmap(bitmap)
            }

            image.close()
        }

        override fun onError(exception: ImageCaptureException) {
            Log.e(TAG, "Photo capture failed: ${exception.message}", exception)
        }
    })
}

拍摄的图像为全屏尺寸

但是,我期待预览中显示的尺寸的结果

android kotlin bitmap preview android-camerax
1个回答
0
投票

使用 CameraController#takePicture API。它会自动裁剪保存的图像以匹配预览的视场。或者,您可以使用 UseCaseGroup API。同一组中的

UseCase
的输出被裁剪以匹配所见即所得效果。

CameraController
在内部使用
UseCaseGroup

© www.soinside.com 2019 - 2024. All rights reserved.