如何使用 Glide 压缩和降低图像质量 android

How to Compress and reduce Quality of image using Glide for android

我正在使用 Glide 库上传图片。在我的另一个应用程序中,我使用此代码

void imageButtonclick() {
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CropImage.activity(filePath).setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1,1).setOutputCompressQuality(50).start(UploadBook.this);

                // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    //intent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);
                    //startActivityForResult(intent, CAMERA_REQUEST_CODE);
            }
        });

通过声明来减小图像的大小 .setOutputCompressQuality(50) 但我不知道如何使用 Glide Image library 做同样的事情。

我的密码是

public static void loadLocalImage(Context ctx, RequestOptions glideRequests, Uri uri, ImageView imageView,
                                      RequestListener listener) {

        glideRequests.fitCenter()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(ctx)
                .applyDefaultRequestOptions(glideRequests.placeholder(R.drawable.ic_stub).error(R.drawable.ic_stub))
                .asBitmap()
                .load(uri)
                .listener(listener)
                .into(imageView);

    }

试试这个

Glide
    .with(ctx)
    .load(url)
    .apply(new RequestOptions().override(600, 200))
    .into(imageView);

来自 Glide official site:

By default Glide prefers RGB_565 because it requires only two bytes per pixel (16 bit) and therefore has half the memory footprint of the higher quality and system default ARGB_8888.

所以您不能再降低位图的质量,但您还有其他选择。

选项 1:使用 override(x, y) 调整图像大小。

RequestOptions myOptions = new RequestOptions()
    .override(100, 100);

Glide.with(fragment)
    .asBitmap()
    .apply(myOptions)
    .load(url)
    .into(bitmapView);

选项 2:使用 centerCropfitCenter

缩放图像

centerCrop() is a cropping technique that scales the image so that it fills the requested bounds of the ImageView and then crops the extra. The ImageView will be filled completely, but the entire image might not be displayed.

RequestOptions myOptions = new RequestOptions()
    .centerCrop();

Glide.with(ctx)
    .asBitmap()
    .apply(myOptions)
    .load(url)
    .into(bitmapView);

fitCenter() is a cropping technique that scales the image so that both dimensions are equal to or less than the requested bounds of the ImageView. The image will be displayed completely, but might not fill the entire ImageView.

RequestOptions myOptions = new RequestOptions()
        .fitCenter();

    Glide.with(ctx)
        .asBitmap()
        .apply(myOptions)
        .load(url)
        .into(bitmapView);

选项 3:混合这两种解决方案

RequestOptions myOptions = new RequestOptions()
    .fitCenter() // or centerCrop
    .override(100, 100);

 Glide.with(ctx)
     .asBitmap()
     .apply(myOptions)
     .load(url)
     .into(bitmapView);

我的解决方案是使用 Kotlin 的扩展函数。使用图像的宽度和高度可以提高效率。在某些情况下,您可能需要为宽度和高度添加乘数以获得更好的质量。

    fun ImageView.loadImage(uri: String) {

        this.post {

            val myOptions = RequestOptions()
               .override(this.width, this.height)
               .centerCrop()

            Glide
               .with(this.context)
               .load(uri)
               .apply(myOptions)
               .into(this)

        }

    }