Java – sliding into simpletarget does not comply with the specified width and height

I'm using glide to load the image, resize it, and save it to a file via simpletarget < bitmap > These images will be uploaded to Amazon S3, but in addition I adjust the size of the image before uploading to save as many user bandwidth as possible. For my application, 1024 pixel wide images are more than enough, so I use the following code to achieve this:

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

It works almost perfectly, but the resulting image is not 1024 pixels wide It was tested with a source image with a size of 4160 x 2340 pixels, and the size of the saved image was 2080 x 1170 pixels

I try to use the width and height parameters passed to the new simpletarget < bitmap > (350350), and use these parameters to get an image size of 1040 x 585 pixels

I really don't know how to make glide respect the size of transmission In fact, I want to scale the image so that the larger size (width or height) is limited to 1024 pixels, the smaller size is resized accordingly (I believe I must find a way to get the original image), and then pass the width and height to simpletarget, but to do this, I need glide to respect the passed width and height!)

Does anyone know what happened? I'm using glide 3.7 0

Since this problem itself may be useful to those who try to use glide to resize and save images, I believe it is in everyone's interest to provide my practical "solution", which relies on the new simpletarget implementation of automatically saving resized images:

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

}

It's easy to use:

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

Solution

After a good night's sleep, I understand! I accidentally found a problem on glide's GitHub page, but I didn't realize that I missed some content in my explanation. Now I have fully understood the content after 10 hours You should never underestimate the power of sleep! But I digress Here is the answer of glide's GitHub issue tracker:

I am confused about this answer because:

Since I have passed the size, I think I have covered this size, which should be respected I missed this important concept:

And this:

Now, it makes sense for me to piece it together Glide only down samples the image (as the first step in the size image process explained by R ó BERT), and it gives the image with approximate size Let me say glide is very clever at this By using the down sampling method before resizing, you can avoid processing unnecessary large bitmaps in memory and improve the quality of resizing, because reducing to a precise size will damage too many "important" pixels!

Since I did not apply any conversion to this load pipeline, the resizing process was stopped in the first step (down sampling), and the generated image only has a size close to my expected target size

To solve this problem, I just applied one Fitcenter() transform, as follows:

Glide.with(appCtx)
        .load(sourceImageUri)
        .asBitmap()
        .fitCenter()
        .into(new FileTarget(to,or omit this override if you want
            }
        });

The resulting image now has a size of 1024 x 576 pixels, which is exactly what I expected

Glide is a very cool library!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>