如何在 Kotlin Compose 桌面加载图像?
How to load image in Kotlin Compose desktop?
在桌面使用Kotlin compose时如何从硬盘加载图片?
你可以通过这个函数获取ImageAsset
fun imageFromFile(file: File): ImageAsset {
return org.jetbrains.skia.Image.makeFromEncoded(file.readBytes()).asImageAsset()
}
完整示例:
import androidx.compose.desktop.Window
import androidx.compose.foundation.Image
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.ImageAsset
import androidx.compose.ui.graphics.asImageAsset
import java.io.File
fun main() = Window {
val file = File("D:\images\my_image.PNG")
val image = remember { imageFromFile(file) }
Image(asset = image)
}
fun imageFromFile(file: File): ImageAsset {
return org.jetbrains.skia.Image.makeFromEncoded(file.readBytes()).asImageAsset()
}
这对我有用。
Image(bitmap = imageFromResource("image.png"),
"image",
)
contentDescription
是必需的,但可以是您想要的任何内容。您还可以添加修饰符,例如
val imageModifier = Modifier
.height(240.dp)
.fillMaxWidth()
.clip(RoundedCornerShape(12.dp))
Image(bitmap = imageFromResource("header.png"),
"image",
imageModifier,
contentScale = ContentScale.Fit
)
其他答案已过时,根据 Compose 1.0.0-beta5,您应该执行以下操作:
Image(painterResource("image.jpg"))
如果您只想加载位图
val bitmap = useResource("image.jpg") { loadImageBitmap(it) }
只需要文件名(不是完整路径),但请确保您的资源在 src/main/resources/image.jpg
试试这个:
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.res.loadImageBitmap
import java.io.File
val file = File(path)
val imageBitmap: ImageBitmap = remember(file) {
loadImageBitmap(file.inputStream())
}
Image(
painter = BitmapPainter(image = imageBitmap),
contentDescription = null
)
Image.asImageBitmap() 已弃用。使用新的 Image.toComposeImageBitmap()。目前 (01.04.2022) 尚未弃用:
@Composable
fun CanvasArea2() {
val image = remember { imageFromFile(File("C:/Users/Admin/Desktop/banana.png")) }
Canvas(modifier = Modifier.background(color = Color(0xFFFFFFFF))) {
drawImage(image)
}
}
fun imageFromFile(file: File): ImageBitmap {
return org.jetbrains.skia.Image.makeFromEncoded(file.readBytes()).toComposeImageBitmap()
}
在桌面使用Kotlin compose时如何从硬盘加载图片?
你可以通过这个函数获取ImageAsset
fun imageFromFile(file: File): ImageAsset {
return org.jetbrains.skia.Image.makeFromEncoded(file.readBytes()).asImageAsset()
}
完整示例:
import androidx.compose.desktop.Window
import androidx.compose.foundation.Image
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.ImageAsset
import androidx.compose.ui.graphics.asImageAsset
import java.io.File
fun main() = Window {
val file = File("D:\images\my_image.PNG")
val image = remember { imageFromFile(file) }
Image(asset = image)
}
fun imageFromFile(file: File): ImageAsset {
return org.jetbrains.skia.Image.makeFromEncoded(file.readBytes()).asImageAsset()
}
这对我有用。
Image(bitmap = imageFromResource("image.png"),
"image",
)
contentDescription
是必需的,但可以是您想要的任何内容。您还可以添加修饰符,例如
val imageModifier = Modifier
.height(240.dp)
.fillMaxWidth()
.clip(RoundedCornerShape(12.dp))
Image(bitmap = imageFromResource("header.png"),
"image",
imageModifier,
contentScale = ContentScale.Fit
)
其他答案已过时,根据 Compose 1.0.0-beta5,您应该执行以下操作:
Image(painterResource("image.jpg"))
如果您只想加载位图
val bitmap = useResource("image.jpg") { loadImageBitmap(it) }
只需要文件名(不是完整路径),但请确保您的资源在 src/main/resources/image.jpg
试试这个:
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.res.loadImageBitmap
import java.io.File
val file = File(path)
val imageBitmap: ImageBitmap = remember(file) {
loadImageBitmap(file.inputStream())
}
Image(
painter = BitmapPainter(image = imageBitmap),
contentDescription = null
)
Image.asImageBitmap() 已弃用。使用新的 Image.toComposeImageBitmap()。目前 (01.04.2022) 尚未弃用:
@Composable
fun CanvasArea2() {
val image = remember { imageFromFile(File("C:/Users/Admin/Desktop/banana.png")) }
Canvas(modifier = Modifier.background(color = Color(0xFFFFFFFF))) {
drawImage(image)
}
}
fun imageFromFile(file: File): ImageBitmap {
return org.jetbrains.skia.Image.makeFromEncoded(file.readBytes()).toComposeImageBitmap()
}