我想用滑块旋转和缩放图像并将编辑后的图像保存到 Android 中的存储中,我该怎么做?
如果此解决方案不合适,请告诉我。
var sliderRotation by remember { mutableFloatStateOf(1f) }
var sliderZoom by remember { mutableFloatStateOf(1f) }
val imageLoader = ImageLoader.Builder(context)
.build()
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
MainToolbar(title = R.string.edit_image)
}
) { paddingValue ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValue),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Box(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
) {
AsyncImage(
modifier = Modifier
.fillMaxSize()
.scale(sliderZoom)
.rotate(sliderRotation),
contentDescription = "Zoomable image",
model = ImageRequest.Builder(context)
.data(uiState.editImageUri.toString())
.build(),
imageLoader = imageLoader,
)
}
Column(
modifier = Modifier
.fillMaxWidth()
.weight(1f),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
SliderRow(
sliderValue = sliderRotation,
onValueChange = {
sliderRotation = it },
title = stringResource(R.string.size)
) {
Icon(
painter = painterResource(id = R.drawable.repeat),
contentDescription = "repeat icon",
tint = Color.Unspecified
)
}
SliderRow(
sliderValue = sliderZoom,
onValueChange = { sliderZoom = it },
title = stringResource(R.string.zoom)
) {
Icon(
painter = painterResource(id = R.drawable.repeat),
contentDescription = "repeat icon",
tint = Color.Unspecified
)
}
Button(onClick = { }) {
}
}
}
}
使用线圈时,您可以获得图像结果为
bitmap
,并且这个bitmap
可以保存在文件存储中的任何位置,例如:-
private fun Context.saveToInternalStorage(bitmapImage: Bitmap): String {
val cw = ContextWrapper(applicationContext)
val directory: File = cw.getDir("imageDir", Context.MODE_PRIVATE)
val mypath = File(directory, "profile.jpg")
var fos: FileOutputStream? = null
try {
fos = FileOutputStream(mypath)
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos)
} catch (e: Exception) {
e.printStackTrace()
} finally {
try {
fos!!.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
return directory.absolutePath
}
编辑:- 使用可以使用
SubcomposeAsyncImage
来获取回调,一旦成功,您就可以轻松获取 bitmap
对象。
如需更多定制,请查看官方文档
SubcomposeAsyncImage(
model = "https://example.com/image.jpg",
contentDescription = "",
onSuccess = {
val bitmap = it.result.drawable.toBitmap()
},
onError = {
}
)