简体   繁体   English

如何修改NDK中的Android位图,以便在Java端使用它?

[英]How can I modify an Android bitmap in the NDK so that I can use it on the Java side?

I call a C++ function over JNI and pass a RGBA_8888 bitmap, lock it, change the values, unlock it, return and then display it in Java with this C++ code: 我通过JNI调用C ++函数并传递RGBA_8888位图,锁定它,更改值,解锁它,返回然后使用此C ++代码在Java中显示它:

AndroidBitmap_getInfo(env, map, &info) < 0);
AndroidBitmap_lockPixels(env, map, (void**)&pixel);

for(i=info.width*info.height-1;i>=0;i--)
{   pixel[i] = 0xf1f1f1f1;
}

AndroidBitmap_unlockPixels(env, map);

The problem I have is that the bitmaps looks not as I expect it and the pixel values (verified with getPixel) are not the same when I check them in Java from what I set them in C++. 我遇到的问题是位图看起来并不像我预期的那样,当我用Java在C ++中设置它们时,用Java检查它们的像素值(用getPixel验证)是不一样的。 When I set the bitmap values to 0xffffffff I get the correct value in Java, but for many others I don't. 当我将位图值设置为0xffffffff时,我在Java中获得了正确的值,但对于许多其他人,我没有。 0xf1f1f1f1 for example turns into 0xF1FFFFFF. 例如,0xf1f1f1f1变为0xF1FFFFFF。

What do I have to do to make it work ? 我需要做些什么来使它工作?

PS: I am using Android 2.3.4 PS:我使用的是Android 2.3.4

It appears the problem is because of the premultiplied alpha. 似乎问题是因为预乘alpha。

Further testing revealed that the r,g,b values get scaled by the alpha so if I pass a bitmap to jni I need to multiply the r,g,b values by 255 and divide them by the alpha to have the real value that I can do alpha-operations with. 进一步测试显示r,g,b值被alpha缩放,所以如果我将位图传递给jni我需要将r,g,b值乘以255并将它们除以alpha以得到实际值可以做alpha操作。 When I pass them back I have to do the reverse. 当我把它们传回来时,我必须做相反的事情。

For only copying and operations without alpha that is not necessary, as the alpha will be 255 for maximum opacity and * 255 / 255 negates itself. 仅用于不需要alpha的复制和操作,因为alpha最大不透明度为255,而255/255否定自身。

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

相关问题 修改位图后,如何修改“修改后的”位图等? - After modifying a Bitmap, how can I modify the “modified” Bitmap and so on? 如何在Java中的Android上重复位图? - How can i repeat bitmap on Android in Java? 如何在 Java 代码中使用 Android.so 库? - How can I use an Android .so library in Java code? 如何将位图从服务发送到Java,Android中的main? - How can I send bitmap from service to a main in java, android? 我可以修改 ReentrantLock 以便可以将它与资源一起使用吗? - Can i modify ReentrantLock so one can use it with try with resources? Android如何获取屏幕快照位图? - Android how can I get screenshot bitmap? 如何在Android中获取经度和纬度并将其传递给canvas类,以便我可以在位图上绘制 - How to get longitude and latitude in android and pass it on to a canvas class so i can draw on a bitmap 我可以修改Java Swing JTextArea,使其看起来像JPasswordField吗? - Can I modify a Java Swing JTextArea so that it looks like a JPasswordField? 如何包含Java src代码,以便我可以在Android Studio中使用它 - How to include java src code so i can use it inside Android studio 如何创建 android.jar 或者我从哪里得到它,以便我可以在 ZD5238197D7D5238197D7D52221 中使用 Android 库的所有组件 - How to create android.jar OR where do i get it so that i can use all the componants of Android in Java Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM