滑入 SimpleTarget<Bitmap> 不遵守指定的宽度和高度

Glide load into SimpleTarget<Bitmap> not honoring the specified width and height

我正在使用 Glide 加载图像、调整图像大小并通过 SimpleTarget<Bitmap> 将其保存到文件中。这些图像将上传到 Amazon S3,但这不是重点。我在上传之前调整图像大小,以尽可能多地节省用户的带宽。对于我的应用程序,需要一张 1024 像素宽的图像就足够了,所以我使用以下代码来实现:

final String to = getMyImageUrl();
final Context appCtx = context.getApplicationContext();

Glide.with(appCtx)
        .load(sourceImageUri)
        .asBitmap()
        .into(new SimpleTarget<Bitmap>(1024, 768) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                try {
                    FileOutputStream out = new FileOutputStream(to);
                    resource.compress(Bitmap.CompressFormat.JPEG, 70, out);
                    out.flush();
                    out.close();
                    MediaScannerConnection.scanFile(appCtx, new String[]{to}, null, null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

它几乎完美地工作,但生成的图像的大小不是 1024 像素宽。使用尺寸为 4160 x 2340 像素的源图像对其进行测试,生成的保存图像的尺寸为 2080 x 1170 像素。

我试过使用传递给 new SimpleTarget<Bitmap>(350, 350)widthheight 参数,使用这些参数生成的图像尺寸为 1040 x 585 像素。

我真的不知道该怎么做才能让 Glide 尊重传递的维度。事实上,我想按比例调整图像的大小,以便将较大的尺寸(宽度或高度)限制为 1024 像素,而较小的尺寸相应地调整大小(我相信我必须找到一种方法来获取原始图像尺寸,然后将宽度和高度传递给 SimpleTarget,但为此我需要 Glide 尊重传递的宽度和高度!)。

有人知道发生了什么事吗?我正在使用 Glide 3.7.0.


由于这个问题本身可能对尝试使用 Glide 调整大小和保存图像的人有用我相信提供我的实际 "solution" 依赖于新的 SimpleTarget 实现符合每个人的利益自动保存调整大小的图像:

import android.graphics.Bitmap;

import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileTarget extends SimpleTarget<Bitmap> {
    public FileTarget(String fileName, int width, int height) {
        this(fileName, width, height, Bitmap.CompressFormat.JPEG, 70);
    }
    public FileTarget(String fileName, int width, int height, Bitmap.CompressFormat format, int quality) {
        super(width, height);
        this.fileName = fileName;
        this.format = format;
        this.quality = quality;
    }
    String fileName;
    Bitmap.CompressFormat format;
    int quality;
    public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
            try {
                FileOutputStream out = new FileOutputStream(fileName);
                bitmap.compress(format, quality, out);
                out.flush();
                out.close();
                onFileSaved();
            } catch (IOException e) {
                e.printStackTrace();
                onSaveException(e);
            }
    }
    public void onFileSaved() {
        // do nothing, should be overriden (optional)
    }
    public void onSaveException(Exception e) {
        // do nothing, should be overriden (optional)
    }

}

使用起来很简单:

Glide.with(appCtx)
        .load(sourceImageUri)
        .asBitmap()
        .into(new FileTarget(to, 1024, 768) {
            @Override
            public void onFileSaved() {
                // do anything, or omit this override if you want
            }
        });

经过一夜安眠,我终于明白了!我在 Glide 的 github 页面中偶然发现了一个有答案的问题,但我没有意识到:我在解释中遗漏了一些东西,在休息 10 小时后我现在完全理解了。永远不要低估睡眠的力量!但我离题了。这是在 Glide's Github issue tracker:

上找到的答案

Sizing the image usually has two phases:

  • Decoding/Downsampler read image from stream with inSampleSize
  • Transforming/BitmapTransformation take the Bitmap and match the exact target size

The decoding is always needed and is included in the flow, the default case is to match the target size with the "at least" downsampler, so when it comes to the transformation the image can be downsized more without quality loss (each pixel in the source will match at least 1.0 pixels and at most ~1.999 pixels) this can be controlled by asBitmap().at least|atMost|asIs|decoder(with downsampler)

The transformation and target size is automatic by default, but only when using a ViewTarget. When you load into an ImageView the size of that will be detected even when it has match_parent. Also if there's no explicit transformation there'll be one applied from scaleType. Thus results in a pixel perfect Bitmap for that image, meaning 1 pixel in Bitmap = 1 pixel on screen resulting in the best possible quality with the best memory usage and fast rendering (because there's no pixel mapping needed when drawing the image).

With a SimpleTarget you take on these responsibilities by providing a size on the constructor or via override() or implementing getSize if the sizing info is async-ly available only.

To fix your load add a transformation: .fitCenter|centerCrop(), your current applied transformation is .dontTransform() (Answer by Róbert Papp)

因为这个我对这个答案感到困惑:

With a SimpleTarget you take on these responsibilities by providing a size on the constructor or via override() or implementing getSize if the sizing info is async-ly available only.

自从我超过尺码后,我想我已经得到了这个尺寸,应该尊重这样的尺码。我错过了这个重要的概念:

  • Decoding/Downsampler read image from stream with inSampleSize
  • Transforming/BitmapTransformation take the Bitmap and match the exact target size

还有这个:

To fix your load add a transformation: .fitCenter|centerCrop(), your current applied transformation is .dontTransform()

现在我把它拼凑起来,它很有意义。 Glide 只是对图像进行下采样(如 Róbert 所解释的调整图像大小流程的第一步),这给出了具有近似尺寸的图像。不得不说Glide在这方面是非常聪明的。通过在调整大小之前使用下采样方法,可以避免在内存中使用不必要的大位图并提高调整大小的质量,因为下采样到精确大小会损害太多 "important" 像素!

因为我没有对这个加载管道应用任何转换,所以在第一步(下采样)中停止了尺寸调整流程,并且生成的图像只有我预期目标尺寸的近似尺寸。

为了解决这个问题,我只是应用了一个 .fitCenter() 转换,如下所示:

Glide.with(appCtx)
        .load(sourceImageUri)
        .asBitmap()
        .fitCenter()
        .into(new FileTarget(to, 1024, 768) {
            @Override
            public void onFileSaved() {
                // do anything, or omit this override if you want
            }
        });

生成的图像现在具有 1024 x 576 像素的尺寸,这正是我所期望的。

Glide 是一个非常酷的库!