图库中的一些照片在某些设备上旋转 Android Kotlin

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

我正在使用 Jetpack Compose 开发 Android 应用程序。这些天,我正在使用一些设备测试该应用程序,并从设备库中找到一些照片(不是全部)在一台设备上旋转(富士通的 901FJ,Android 版本为 9)。有些照片旋转 90 度,有些倒置。我读过一些用 Java 编写的代码,但我只了解 Kotlin。我仍然不知道如何更改代码。有人可以帮助我吗?
SignupView的代码

@OptIn(ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class)
@Composable
fun Signup(navController: NavController, auth: FirebaseAuth, mainViewModel: MainViewModel) {
    val requester = remember { BringIntoViewRequester() }
    val scope = rememberCoroutineScope()
    val context = LocalContext.current
    val dataStore = StoreUserData(context)
    var imageBitmap by remember { mutableStateOf<Bitmap?>(null) }
    val galleryLauncher =
        rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
            uri?.getBitmapOrNull(context.contentResolver)?.let { bitmap: Bitmap ->
                imageBitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, true)
            }
        }
...    

        Column(
        modifier = Modifier
            .background(Beige)
            .fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
            Spacer(modifier = Modifier.height(20.dp))
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.Center
        ) {
            Text(text = "Signup", fontSize = 32.sp, color = Brown)
        }
        Column(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Row() {
                if (imageBitmap != null) {
                    Image(
                        painter = rememberImagePainter(imageBitmap),
                        contentScale = ContentScale.FillBounds,
                        contentDescription = null,
                        modifier = Modifier
                            .width(60.dp)
                            .height(60.dp)
                            .clip(CircleShape)
                    )
                } else {
                    Image(
                        painter = painterResource(id = R.drawable.coffee),
                        contentDescription = null,
                        contentScale = ContentScale.Fit,
                        modifier = Modifier
                            .size(60.dp)
                            .clip(CircleShape)
                    )
                }
            }
            Row() {
                Button(
                    onClick = { galleryLauncher.launch("image/*") },
                    modifier = Modifier
                        .wrapContentSize(),
                    colors = ButtonDefaults.textButtonColors(
                        backgroundColor = Pink,
                        contentColor = Brown,
                        disabledContentColor = Color.LightGray
                    )
                ) {
                    Text(text = "Select a photo")
                }
            }          
...
                                                
}


fun Uri.getBitmapOrNull(contentResolver: ContentResolver): Bitmap? {
    return kotlin.runCatching {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val source = ImageDecoder.createSource(contentResolver, this)
            ImageDecoder.decodeBitmap(source)
        } else {
            MediaStore.Images.Media.getBitmap(contentResolver, this)
        }
    }.getOrNull()
}
android kotlin bitmap android-jetpack-compose photo-gallery
1个回答
0
投票

多亏了有用的评论,我才得以了解“ExifInterface”。 这些是我添加的代码,效果很好。
build.gradle(:应用程序)

implementation 'androidx.exifinterface:exifinterface:1.3.6'
val galleryLauncher =
        rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
            uri?.getBitmapOrNull(context.contentResolver)?.let { bitmap: Bitmap ->
                val orientation = uri?.getOrientation(context.contentResolver)
                val rotatedBitmap = orientation?.let { rotateBitmap(bitmap, it) } ?: bitmap
                imageBitmap = Bitmap.createScaledBitmap(rotatedBitmap, 120, 120, true)
            }
        }

fun Uri.getOrientation(contentResolver: ContentResolver): Int {
    var orientation = ExifInterface.ORIENTATION_UNDEFINED
    try {
        contentResolver.openInputStream(this)?.use { inputStream ->
            val exifInterface = ExifInterface(inputStream)
            orientation = exifInterface.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED
            )
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return orientation
}


fun rotateBitmap(bitmap: Bitmap, orientation: Int): Bitmap {
    val matrix = Matrix()
    when (orientation) {
        ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1f, 1f)
        ExifInterface.ORIENTATION_ROTATE_90 -> matrix.setRotate(90f)
        ExifInterface.ORIENTATION_TRANSVERSE -> {
            matrix.setRotate(270f)
            matrix.postScale(-1f, 1f)
        }
        ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180f)
        ExifInterface.ORIENTATION_FLIP_VERTICAL -> matrix.setRotate(180f)
        ExifInterface.ORIENTATION_TRANSPOSE -> {
            matrix.setRotate(90f)
            matrix.postScale(-1f, 1f)
        }
        ExifInterface.ORIENTATION_ROTATE_270 -> matrix.setRotate(270f)
        else -> return bitmap
    }
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}
© www.soinside.com 2019 - 2024. All rights reserved.