1

I'm using Universal Image Loader to:

  • Load image synchronously

  • Fix orientation

  • Crop center (this is not working)

  • Do all this with a low memory footprint

I don't want to scale a bitmap by myself if I can do it with Universal Image Loader. The reason I ask this question is because I don't know wether this is possible or not with Universal Image Loader.

I don't want (or need) to use an ImageView to load the bitmap I simply need a bitmap, but crop center it on the fly. Is this possible with UIL? I have gone through the issues on GH, through questions on SO and can't find an example for it.

This is the code:

private Bitmap getBitmap(Uri uri, int width, int height) {
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .considerExifParams(true)
            .cacheOnDisk(true)
            .build();
    ImageSize size = new ImageSize(width, height);
    return mImageLoader.loadImageSync(uri.toString(), size, options);
}

Thank you all!

albertpeiro
  • 356
  • 4
  • 14
  • possible duplicate of [Android Crop Center of Bitmap](http://stackoverflow.com/questions/6908604/android-crop-center-of-bitmap) – matiash May 25 '14 at 18:48
  • 1
    The question you mention asks for a general purpose solution. I am asking how to do it with Universal Image Loader, which can be answered with Yes or No and some code, I believe. – albertpeiro May 25 '14 at 18:56
  • Your `getBitmap()` method returns a `Bitmap` instance. That's all you need to apply the answer at the other question. – matiash May 25 '14 at 18:57
  • I don't want to do it by hand. I want to do it with UIL if possible and that's exactly what I am asking for. – albertpeiro May 25 '14 at 19:09

1 Answers1

2

I think that that's what BitmapProcessors are for (although I haven't used the lib myself).

Here's what I would do:

// preProcessor gets to work before image gets cached, so cache will contain the cropped
// version
[...Builder...].preProcessor(new BitmapProcessor() {
    public Bitmap process (Bitmap src) {
        // Roughly assuming that the bitmap is bigger than the needed dimensions
        // but you get the idea
        int xOffset = (src.getWidth() - width) / 2;
        int yOffset = (src.getHeight() - height) / 2;
        Bitmap result = Bitmap.createBitmap(src, xOffset, yOffset, width, height);
        // not sure whether this is necessary or the calling logic does it
        // my guess is it does 'cause it's a simple check [if (src != result) src.recycle();]
        // and the lib is rather smart altogether. Check the docs, they probably mention this
        src.recycle();
        return result;
    }
})
Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59