简体   繁体   English

NDK-将位图的像素保存到文件

[英]NDK - Save Bitmap's pixels to File

I'm currently trying to save a bitmap using the NDK without restoring the Java Object but using directly a C++ pixel array. 我目前正在尝试使用NDK保存位图,而不还原Java对象,而是直接使用C ++像素数组。

I have already the bitmap stored in a JniBitmapHolder and I can successfully get the stored pixels. 我已经将位图存储在JniBitmapHolder中,并且可以成功获取存储的像素。 I would like to try to save them without call the Bitmap.create method. 我想尝试保存它们而不调用Bitmap.create方法。

Currently I'm trying to do it like this (without success): 目前,我正在尝试这样做(没有成功):

Util.saveImageSync(Util.getOutputMediaFile("filtered"), jniBitmap.getBitmapArray());

And the method is implemented like this: 该方法是这样实现的:

public static void saveImageSync(final File outputFile, final ByteBuffer buffer){
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(outputFile);
        out.getChannel().write(buffer);
    } catch (Exception e) {
        Dbg.d("ERROR", e);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I've added the method getBitmaArray to the library, like this: 我已经将方法getBitmaArray添加到库中,如下所示:

JNIEXPORT jobject JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniGetBitmapArray(
    JNIEnv * env, jobject obj, jobject handle) {
JniBitmap *jniBitmap = (JniBitmap *) env->GetDirectBufferAddress(handle);
jniBitmap->_storedBitmapPixels;
return env->NewDirectByteBuffer(jniBitmap->_storedBitmapPixels, 0);

} }

But it seems that the direct pixel array isn't enough or correct. 但似乎直接像素阵列还不够或正确。 Any other idea to save pixels directly to file without the need of creating the Java Bitmap? 还有其他无需创建Java位图直接将像素保存到文件的想法吗?

Also hints/pointers are really appreciated. 还非常感谢提示/指针。

Yes, you need to write bitmap header to create a compliant file. 是的,您需要编写位图标题才能创建兼容文件。 See Writing BMP image in pure c/c++ without other libraries for explanation how this can be done from native code. 请参阅在没有其他库的情况下用纯c / c ++编写BMP图像,以了解如何通过本机代码完成此操作。

After some further investigation, the fastest way was to keep a support Bitmap, pass that to the C++ without creating a new bitmap every time I use that function, and just use it to use directly the Java method to save the pixels. 经过进一步的研究,最快的方法是保留支持的位图,将其传递给C ++,而无需每次使用该函数时都创建新的位图,而只是使用它直接使用Java方法来保存像素。

Thanks though to Alex for the answer :) actually the problem was the missing headers. 尽管感谢Alex的回答:)实际上问题是缺少标题。

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

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