繁体   English   中英

我制作了一个相机视图应用程序,我希望图像的 output 与视图中显示的完全相同

[英]I have made a camera view app, I want the output of the image to be exactly as it is shown in the view

You can see below the android view image differs from the output image, I want the output image exactly same as it is shown in the android view. I want to do this because in future I will add a box in the center and I can find the box position in the android view and I will use this position to cut out the image from the output image.

这是代码->

 @Composable
fun CameraView(
    isOpenFrontCamera: Boolean,
    outputDirectory: File,
    executor: Executor,
    onImageCaptured: (Uri) -> Unit,
    onError: (ImageCaptureException) -> Unit,
    onCloseCameraClick: () -> Unit
) {
    val lensFacing: Int =
        if (isOpenFrontCamera) CameraSelector.LENS_FACING_FRONT else CameraSelector.LENS_FACING_BACK
    val context = LocalContext.current
    val lifecycleOwner = LocalLifecycleOwner.current

    val preview = Preview.Builder().build()
    val previewView = remember { PreviewView(context) }

    val imageCapture: ImageCapture = remember {
        ImageCapture.Builder().setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY).build()
    }

    val cameraSelector = CameraSelector.Builder()
        .requireLensFacing(lensFacing)
        .build()

    LaunchedEffect(lensFacing) {
        val cameraProvider = context.getCameraProvider()
        cameraProvider.unbindAll()
        cameraProvider.bindToLifecycle(
            lifecycleOwner,
            cameraSelector,
            preview,
            imageCapture
        )

        preview.setSurfaceProvider(previewView.surfaceProvider)
    }

    //Screen
    Box(
        contentAlignment = Alignment.BottomCenter,
        modifier = Modifier
            .fillMaxSize()) {
        AndroidView({ previewView }, modifier = Modifier.fillMaxSize())

        IconButton(
            modifier = Modifier.padding(bottom = 20.dp),
            onClick = {
                Log.d("takePhoto", "ON CLICK")
                takePhoto(
                    imageCapture = imageCapture,
                    outputDirectory = outputDirectory,
                    executor = executor,
                    onImageCaptured = { imageUri ->
                            onImageCaptured(imageUri)
                    },
                    onError = onError
                )
            },
            content = {
                Icon(
                    painter = painterResource(id = R.drawable.ic_baseline_camera_24),
                    contentDescription = stringResource(R.string.take_picture),
                    tint = Color.White,
                    modifier = Modifier
                        .fillMaxSize(0.2f)
                )
            }
        )
    }
}

  

应用 ->

在此处输入图像描述

输出->

在此处输入图像描述

我做了这样的逻辑,它工作得很好->

我可以通过this->得到屏幕的大小

  modifier = Modifier
            .fillMaxSize()
            .onGloballyPositioned { coordinates ->
                containerOverlaySize = coordinates.size
            }

output 图像逻辑->

      takePhoto(
                imageCapture = imageCapture,
                outputDirectory = outputDirectory,
                executor = executor,
                onImageCaptured = { imageUri ->

                    val imageOriginal =
                        MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)

                    //The imageOriginal was rotated anti clockwise, so had to rotate it
                    val rotationMatrix = Matrix()
                    rotationMatrix.postRotate(90f)

                    val rotatedBitmap = Bitmap.createBitmap(
                        imageOriginal,
                        0,
                        0,
                        imageOriginal.width,
                        imageOriginal.height,
                        rotationMatrix,
                        false
                    )

                    val androidViewRatio:Float = containerOverlaySize.width.toFloat()/containerOverlaySize.height.toFloat()

                    val outputImageWidth = rotatedBitmap.height*androidViewRatio

                    val startingPositionToCut = rotatedBitmap.width/2 - outputImageWidth/2
                    
                    val outputBitmap = Bitmap.createBitmap(
                        rotatedBitmap,
                        startingPositionToCut.toInt(),
                        0,
                        (outputImageWidth).toInt(),
                        rotatedBitmap.height
                    )})

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM