如何使用Kotlin在Android中压缩照片 [英] How to compress photo in Android with Kotlin

查看:341
本文介绍了如何使用Kotlin在Android中压缩照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这是我从智能手机拍摄照片的代码,问题是图像很大,我想压缩它,有什么帮助或想法吗?

now this is my code to take a photo from my smartphone, the problem is that the image is very large and I would like to compress it, some help or idea?

感谢信息

方法abrirCamara()的执行

Execution of the method abrirCamara()

private fun abrirCamara() {
    val values = ContentValues()
    values.put(MediaStore.Images.Media.TITLE, "Nueva foto")
    values.put(MediaStore.Images.Media.DESCRIPTION, "Desde la camara")
    image_uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
    //intención de la cámara
    val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri)
    startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)

}

当用户从权限请求弹出窗口中按PERMIT或DENY时,调用

is called when the user press PERMIT or DENY from the permission request pop-up window

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {

    when(requestCode){
        PERMISSION_CODE -> {
            if (grantResults.size > 0 && grantResults[0] ==
                PackageManager.PERMISSION_GRANTED){
                //permiso de popup fue concedido
                abrirCamara()
            }
            else{
                //el permiso de popup fue denegado
                Toast.makeText(this, "Permiso denegado", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

从相机意图捕获图像时呼叫

Call when the image was captured from the camera's intention

     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    //Llamada cuando la imagen fue capturada desde la intención de la cámara
    if (resultCode == Activity.RESULT_OK){


        //configurar imagen capturada a vista de imagen (ImageView)
        imgEnvio.setImageURI(image_uri)        

        captura_btn.visibility =View.INVISIBLE
        siguiente.visibility=View.VISIBLE

    }
}

推荐答案

使用压缩机

Gradle

dependencies {
implementation 'id.zelory:compressor:3.0.0'
}

压缩图像文件

val compressedImageFile = Compressor.compress(context, actualImageFile)

将图像文件压缩到特定的目标位置

Compress Image File to specific destination

val compressedImageFile = Compressor.compress(context, actualImageFile) {
default()
destination(myFile)
}

使用默认约束和自定义约束

Using default constraint and custom partial of it

val compressedImageFile = Compressor.compress(context, actualImageFile) {
default(width = 640, format = Bitmap.CompressFormat.WEBP)
}

完整的自定义约束

val compressedImageFile = Compressor.compress(context, actualImageFile) {
resolution(1280, 720)
quality(80)
format(Bitmap.CompressFormat.WEBP)
size(2_097_152) // 2 MB
}

val compressedImageFile = Compressor.compress(context, actualImageFile) {
    resolution(1280, 720)
    quality(80)
    format(Bitmap.CompressFormat.WEBP)
    size(2_097_152) // 2 MB
    }

val compressedImageFile = Compressor.compress(context, actualImageFile) {
    resolution(1280, 720)
    quality(80)
    format(Bitmap.CompressFormat.WEBP)
    size(2_097_152) // 2 MB
    }

val compressedImageFile = Compressor.compress(context, actualImageFile) {
    resolution(1280, 720)
    quality(80)
    format(Bitmap.CompressFormat.WEBP)
    size(2_097_152) // 2 MB
}

这篇关于如何使用Kotlin在Android中压缩照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆