简体   繁体   English

调整大小并压缩从图库上传时的图像

[英]resize and compress image on upload from gallery

i want compress image on upload from gallery. 我想压缩从图库上传的图像。 i use "id.zelory:compressor" library for this purpose. 我为此目的使用“ id.zelory:compressor”库。

            case REQUEST_GALLERY_CODE: {
            if (resultCode == RESULT_OK && null != data) {
                try {
                    InputStream inputStream = getContentResolver().openInputStream(data.getData());
                    String type = getFileExtension(data.getData());
                    UploadImage(getBytes(inputStream) , type);

and getbytes function : 和getbytes函数:

    private byte[] getBytes(InputStream is) throws IOException {
    ByteArrayOutputStream byteBuff = new ByteArrayOutputStream();
    int buffSize = 1024;
    byte[] buff = new byte[buffSize];
    int len = 0;
    while ((len = is.read(buff)) != -1) {
        byteBuff.write(buff, 0, len);
    }
    return byteBuff.toByteArray();
}

i want before send image to upload (UploadImage()) compress it by using this : 我想在发送图像上传(UploadImage())之前使用以下方法对其进行压缩:

compressedImageFile = new Compressor(this).compressToFile(actualImageFile);

but input this code is actualImageFile! 但是输入的这段代码是actualImageFile! how can do this? 怎么办呢?

You can use native quality change. 您可以使用本地质量更改。 In my latest project I have used smth like this: 在我的最新项目中,我使用了以下方法:

 val image = data?.extras?.get("data") as Bitmap
 val byteArrayOutputStream = ByteArrayOutputStream()
 image.compress(Bitmap.CompressFormat.PNG,IMAGE_QUALITY, byteArrayOutputStream)
 val byteArray = byteArrayOutputStream.toByteArray()
 service.send(Item(Base64.encodeToString(byteArray, Base64.DEFAULT))

IMAGE_QUALITY is a number from 0-100. IMAGE_QUALITY是0到100之间的数字。 You can play with a number to see what quality will satisfy your needs 您可以玩数字游戏,看看哪种质量可以满足您的需求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM