如何使用 Glide for android 压缩和降低图像质量 [英] How to Compress and reduce Quality of image using Glide for android

查看:86
本文介绍了如何使用 Glide for android 压缩和降低图像质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

void imageButtonclick() {iv1.setOnClickListener(new View.OnClickListener() {@覆盖公共无效onClick(查看视图){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 监听器) {glideRequests.fitCenter().skipMemoryCache(真).diskCacheStrategy(DiskCacheStrategy.NONE);Glide.with(ctx).applyDefaultRequestOptions(glideRequests.placeholder(R.drawable.ic_stub).error(R.drawable.ic_stub)).asBitmap().load(uri).listener(听众).into(imageView);}

解决方案

来自 Glide官方网站:

<块引用>

默认情况下 Glide 更喜欢 RGB_565 因为它只需要两个字节每个像素(16 位),因此只有一半的内存占用更高的质量和系统默认的 ARGB_8888.

因此您不能再降低位图的质量,但您还有其他选择.

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

RequestOptions myOptions = new RequestOptions().override(100, 100);Glide.with(片段).asBitmap().apply(myOptions).load(网址).into(bitmapView);

选项 2:使用 centerCropfitCenter

缩放图像<块引用>

centerCrop() 是一种裁剪技术,可以缩放图像,使其填充 ImageView 的请求边界,然后裁剪额外的边界.ImageView 将被完全填充,但整个图像可能不显示.

RequestOptions myOptions = new RequestOptions().centerCrop();Glide.with(ctx).asBitmap().apply(myOptions).load(网址).into(bitmapView);

<块引用>

fitCenter() 是一种裁剪技术,可以缩放图像,以便维度等于或小于请求的边界图像视图.图像将完全显示,但可能不会填满整个 ImageView.

RequestOptions myOptions = new RequestOptions().fitCenter();Glide.with(ctx).asBitmap().apply(myOptions).load(网址).into(bitmapView);

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

RequestOptions myOptions = new RequestOptions().fitCenter()//或 centerCrop.override(100, 100);Glide.with(ctx).asBitmap().apply(myOptions).load(网址).into(bitmapView);

I'm using Glide library to upload image. In my another app I use this code

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);
            }
        });

to decrease size of image by declaring .setOutputCompressQuality(50) But I don't know how to do same with Glide Image library .

My code is

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);

    }

解决方案

From 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.

So you cannot reduce the quality of a bitmap anymore, but you have some another options.

Option 1: Resizing image size with override(x, y).

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

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

Option 2: Scaling image with centerCrop or fitCenter

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);

Option 3: Mix both of these solutions

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

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

这篇关于如何使用 Glide for android 压缩和降低图像质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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