在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):zetbaitsu/Compressor开源软件地址(OpenSource Url):https://github.com/zetbaitsu/Compressor开源编程语言(OpenSource Language):Kotlin 92.7%开源软件介绍(OpenSource Introduction):Compressor Compressor is a lightweight and powerful android image compression library. Compressor will allow you to compress large photos into smaller sized photos with very less or negligible loss in quality of the image.Gradledependencies {
implementation 'id.zelory:compressor:3.0.1'
} Let's compress the image size!Compress Image Fileval compressedImageFile = Compressor.compress(context, actualImageFile) Compress Image File to specific destinationval compressedImageFile = Compressor.compress(context, actualImageFile) {
default()
destination(myFile)
} I want custom Compressor!Using default constraint and custom partial of itval compressedImageFile = Compressor.compress(context, actualImageFile) {
default(width = 640, format = Bitmap.CompressFormat.WEBP)
} Full custom constraintval compressedImageFile = Compressor.compress(context, actualImageFile) {
resolution(1280, 720)
quality(80)
format(Bitmap.CompressFormat.WEBP)
size(2_097_152) // 2 MB
} Using your own custom constraintclass MyLowerCaseNameConstraint: Constraint {
override fun isSatisfied(imageFile: File): Boolean {
return imageFile.name.all { it.isLowerCase() }
}
override fun satisfy(imageFile: File): File {
val destination = File(imageFile.parent, imageFile.name.toLowerCase())
imageFile.renameTo(destination)
return destination
}
}
val compressedImageFile = Compressor.compress(context, actualImageFile) {
constraint(MyLowerCaseNameConstraint()) // your own constraint
quality(80) // combine with compressor constraint
format(Bitmap.CompressFormat.WEBP)
} You can create your own extension toofun Compression.lowerCaseName() {
constraint(MyLowerCaseNameConstraint())
}
val compressedImageFile = Compressor.compress(context, actualImageFile) {
lowerCaseName() // your own extension
quality(80) // combine with compressor constraint
format(Bitmap.CompressFormat.WEBP)
} Compressor now is using Kotlin coroutines!Calling Compressor should be done from coroutines scope// e.g calling from activity lifecycle scope
lifecycleScope.launch {
val compressedImageFile = Compressor.compress(context, actualImageFile)
}
// calling from global scope
GlobalScope.launch {
val compressedImageFile = Compressor.compress(context, actualImageFile)
} Run Compressor in main threadval compressedImageFile = Compressor.compress(context, actualImageFile, Dispatchers.Main) Old versionPlease read this readme License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论