简体   繁体   中英

Resize bitmap using Renderscript

I'm currently working on a project where I have to use RenderScript, so i started learning about it, and it's a great technology, because, just like openGL, it lets you use computational code that goes to a native level, and doesn't have to use the dalvik vm. This part of the code, being processed much faster than if you would use normal android code. I started working with image processing and what i was wondering is:

Is it possible to resize a bitmap using RenderScript? this should be much faster then resizing an bitmap using android code. Plus, renderscript can process information that is bigger than 48mB (limit on some phones for each process).

While you could use Rendscript to do the bitmap resize, I'm not sure if that's the best choice. A quick look at the Android code base shows that Java API does go into native code to do a bitmap resize, although if the resize algorithm isn't to your needs, you'll have to implement your own.

There are a number of answers on SO for getting the bitmap scaled efficiently. My recommendation is to try those, and if they still aren't doing what your want, either as quickly or how the results appear visually to then investigate into writing your own. If you still want to write your own, do use the performance tools available to see if you really are faster or just reinventing the wheel.

You can use the below function to resize the image.

private Bitmap resize(Bitmap inBmp) {
    RenderScript mRs = RenderScript.create(getApplication());
    Bitmap outBmp = Bitmap.createBitmap(OUTPUT_IMAGE_WIDTH, inBmp.getHeight() * OUTPUT_IMAGE_WIDTH /inBmp.getWidth(), inBmp.getConfig());
    ScriptIntrinsicResize siResize = ScriptIntrinsicResize.create(mRs);
    Allocation inAlloc = Allocation.createFromBitmap(mRs, inBmp);
    Allocation outAlloc = Allocation.createFromBitmap(mRs, outBmp);
    siResize.setInput(inAlloc);
    siResize.forEach_bicubic(outAlloc);
    outAlloc.copyTo(outBmp);
    inAlloc.destroy();
    outAlloc.destroy();
    siResize.destroy();
    return outBmp;
}

OUTPUT_IMAGE is the integer value specifying the width of the output image.

NOTE: While using the RenderScript Allocation you have to be very careful as they lead to memory leakages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM