您可以在下面看到 android 視圖影像與輸出影像不同,我希望輸出影像與 android 視圖中顯示的完全相同。我想這樣做是因為將來我會在中心添加一個框,我可以在 android 視圖中找到框的位置,我將使用這個位置從輸出影像中剪切影像。
這是代碼->
@Composable
fun CameraView(
isOpenFrontCamera: Boolean,
outputDirectory: File,
executor: Executor,
onImageCaptured: (Uri) -> Unit,
one rror: (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)
},
one rror = one rror
)
},
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)
)
}
)
}
}
應用 ->
輸出->
uj5u.com熱心網友回復:
我做了這樣的邏輯,它作業得很好->
我可以通過this->得到螢屏的大小
modifier = Modifier
.fillMaxSize()
.onGloballyPositioned { coordinates ->
containerOverlaySize = coordinates.size
}
輸出影像邏輯->
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
)})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/506986.html
標籤:安卓 安卓工作室 科特林 安卓布局 android-jetpack-compose