位图/画布使用和 NDK

2022-01-10 00:00:00 c android-ndk android java

我最近发现 NDK 代码可以分配的内存量没有硬性限制,而您可以在 Java 端分配的内存量非常有限(在大多数设备上约为 25Mb).

I've recently found out that there is no hard limit to the amount of memory NDK code can allocate in contrast to the heavily limited amount of memory (~25Mb on most devices) you can allocate on the Java side.

我想编写一个图像处理应用程序(类似于 Photoshop),它需要同时在内存中保存多个大位图,而位图数据将占用大约 20Mb 的内存.在 Java 中执行此操作会使应用程序在我尝试过的许多设备上容易出现内存不足异常.

I want to write an image processing app (something like Photoshop) that needs to keep several large bitmaps in memory at once where the bitmap data will take ~20Mb of memory. Doing this in Java makes the app prone to out of memory exceptions on many devices I've tried.

我当前的所有代码都使用 Bitmap 和 Canvas 类来进行图像处理.谁能建议一些方法让我在 C 端分配大部分内存并仍然使用 Bitmap+Canvas 来执行我的绘图操作(使用 Android 2.1 及更高版本)?

All my current code makes use of the Bitmap and Canvas class for doing my image manipulations. Can anyone suggest some way that allows me to allocate most of my memory on the C side and still make use of Bitmap+Canvas for performing my drawing operations (using Android 2.1 and above)?

例如,如果我的图像由 6 个位图图层组成,并且用户在第 3 层进行绘画,我需要在第 3 层绘制一个绘画斑点位图,然后更新屏幕以显示全部展平的结果实时叠加.我已经考虑过将 C 中的 6 个位图分配为 int 数组,并使用存储在 Bitmap 对象中的正在编辑的图层的副本使用 Canvas 在 Java 端执行绘画操作.不过,我不确定扁平化阶段将如何工作.

As an example, if my image is composed of 6 bitmap layers and the user is painting on the 3rd layer, I need to draw a paint blob bitmap to the 3rd layer and then update the screen to show the result of flattening all layers on top of each other in real time. I've considered something along the lines of allocating my 6 bitmaps in C as int arrays and performing the painting operation on the Java side with Canvas using a copy of the layer being edited stored in a Bitmap object. I'm not sure how the flattening stage is going to work though.

推荐答案

查看 NDK 中的bitmap-plasma"示例.它在 Java 中创建位图并在本机代码中操作位.一种可能的技术是您可以分配大块内存并将图像保存在本机代码中,然后简单地将视图"渲染到 Java 创建的位图中.渲染视图和扁平化"图像层的方法可能应该在本机代码中完成.大致如下:

Check out the "bitmap-plasma" sample in the NDK. It creates a bitmap in Java and manipulates the bits in native code. One possible technique is that you can allocate the large blocks of memory and hold your images in native code and simply render a "view" into a Java-created bitmap. The method to render the view and do the "flattening" of your image layers should probably be done in native code. Something along the lines of:

...用户更改了图层...

...user changed a layer...

My_native_render_code(MyDisplayBitmap);

My_native_render_code(MyDisplayBitmap);

无效();

相关文章