whi*_*awn 6 android android-ndk
我正在尝试通过本机函数将图像转换为灰度,使用从Android in Action中获取的一段代码(第2版;您也可以在此处看到它).不幸的是,返回的位图对象(而不是灰度)最终为空.
这是我加载(.png)图像的方式:
Bitmap original = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample, options);
Run Code Online (Sandbox Code Playgroud)
位图通过了许多安全条件(请在下面查看).这是Java中的本机函数定义:
public native void convertToGray(Bitmap bitmapIn,Bitmap bitmapOut);
Run Code Online (Sandbox Code Playgroud)
和电话:
// Grayscale bitmap (initially empty)
Bitmap gray = Bitmap.createBitmap(original.getWidth(),original.getHeight(),Config.ALPHA_8);
// Native function call
convertToGray(original,gray);
Run Code Online (Sandbox Code Playgroud)
这是功能:
JNIEXPORT void JNICALL Java_com_example_Preprocessor_convertToGray(JNIEnv * env, jobject obj, jobject bitmapcolor,jobject bitmapgray)
{
AndroidBitmapInfo infocolor;
AndroidBitmapInfo infogray;
void* pixelscolor;
void* pixelsgray;
int ret;
int y;
int x;
LOGI("convertToGray");
if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}
if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}
LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags);
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888 !");
return;
}
LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags);
if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) {
LOGE("Bitmap format is not A_8 !");
return;
}
if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
}
if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
}
// modify pixels with image processing algorithm
for (y=0;y<infocolor.height;y++) {
argb * line = (argb *) pixelscolor;
uint8_t * grayline = (uint8_t *) pixelsgray;
for (x=0;x<infocolor.width;x++) {
grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue;
}
pixelscolor = (char *)pixelscolor + infocolor.stride;
pixelsgray = (char *) pixelsgray + infogray.stride;
}
LOGI("Done! Unlocking pixels...");
AndroidBitmap_unlockPixels(env, bitmapcolor);
AndroidBitmap_unlockPixels(env, bitmapgray);
}
Run Code Online (Sandbox Code Playgroud)
颜色位图正确传递,代码的处理部分似乎工作正常,但bitmapgray保持为空.我想我在这里缺少一些至关重要的东西.
测试环境:模拟器,v2.2.使用此版本,该功能在从主线程调用本机代码时有效.在2.3仿真器上,无论调用C代码的线程或加载位图的方式如何,该函数都不起作用.Android NDK:4b和6b.
更新#1:您将在此处找到完整的源代码.
更新#2: RGB_565而不是ALPHA_8给出了一些结果.似乎Java中的setPixels()不适用于ALPHA_8,而且我在查找此配置类型的信息时遇到问题.任何形式的帮助将不胜感激.
小智 1
我有类似的问题。首先,我对infogray使用android RGBA_8888格式而不是A_8(原始位图是使用ARGB_8888格式创建的)。然后,如果您使用 argb 结构代替 uint_8 表示灰线,则可以像这样填充像素:
for (y = 0; y < infocolor.height; ++y) {
argb* line = (argb*) pixelscolor;
argb* grayline = (argb*) pixelsgray;
for (x = 0; x < infocolor.width; ++x) {
grayline[x].red = grayline[x].green = grayline[x].blue = 0.3 * line[x].red + 0.59 * line[x].green + 0.11 * line[x].blue;
}
pixelscolor = (char*)pixelscolor + infocolor.stride;
pixelsgray = (char*)pixelsgray + infogray.stride;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6081 次 |
| 最近记录: |