简体   繁体   English

使用 Renderscript 调整位图大小

[英]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.我目前正在做一个必须使用 RenderScript 的项目,所以我开始学习它,这是一项很棒的技术,因为就像 openGL 一样,它允许您使用达到本机级别的计算代码,并且不会不必使用dalvik vm。 This part of the code, being processed much faster than if you would use normal android code.这部分代码的处理速度比使用普通 android 代码要快得多。 I started working with image processing and what i was wondering is:我开始使用图像处理,我想知道的是:

Is it possible to resize a bitmap using RenderScript?是否可以使用 RenderScript 调整位图的大小? this should be much faster then resizing an bitmap using android code.这应该比使用 android 代码调整位图大小要快得多。 Plus, renderscript can process information that is bigger than 48mB (limit on some phones for each process).另外,renderscript 可以处理大于 48mB 的信息(每个进程限制在某些手机上)。

While you could use Rendscript to do the bitmap resize, I'm not sure if that's the best choice.虽然您可以使用 Rendscript 来调整位图大小,但我不确定这是否是最佳选择。 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.快速浏览 Android 代码库会发现 Java API 确实会进入本机代码来调整位图大小,但如果调整大小算法不符合您的需要,则您必须实现自己的。

There are a number of answers on SO for getting the bitmap scaled efficiently.有许多关于 SO 的答案可以有效地缩放位图。 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. OUTPUT_IMAGE是指定输出图像宽度的整数值。

NOTE: While using the RenderScript Allocation you have to be very careful as they lead to memory leakages.注意:在使用 RenderScript 分配时,您必须非常小心,因为它们会导致内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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