【发布时间】:2014-04-08 08:11:52
【问题描述】:
我正在使用 ARGB_8888 配置传递位图。 我可以将灰度效果应用于图像,但应用后我失去了亮度。
我用谷歌搜索了很多,但找到了与我相同的实现。
这是我的原生实现::
JNIEXPORT void JNICALL Java_com_example_ndksampleproject_MainActivity_jniConvertToGray(JNIEnv * env, jobject obj, jobject bitmapcolor,jobject bitmapgray)
{
AndroidBitmapInfo infocolor;
void* pixelscolor;
AndroidBitmapInfo infogray;
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);
}
LOGI("unlocking pixels height = %d",infocolor.height);
// 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] = ((255-0.3 * line[x].red) + (255-0.59 * line[x].green) + (255-0.11*line[x].blue))/3;
}
pixelscolor = (char *)pixelscolor + infocolor.stride;
pixelsgray = (char *) pixelsgray + infogray.stride;
}
LOGI("unlocking pixels");
AndroidBitmap_unlockPixels(env, bitmapcolor);
AndroidBitmap_unlockPixels(env, bitmapgray);
}
结果 ::
如果您需要我这边的任何东西,请告诉我。 请帮助我摆脱这个问题,因为我已经陷入了很多小时。 非常感谢提前!!!
编辑 ::
应用 Mark Setchell 的建议后 ::
已编辑
如果你反转上面的图像,你会得到这个——这对我来说是正确的:
【问题讨论】:
-
@ChintanRathod 感谢您的快速回复。您能指导一下如何在本地应用相同的东西吗?
-
嗨,我不知道 Android 的原生编码,但我看到了一些算法实现。您可以使用johndcook.com/blog/2009/08/24/… 或tannerhelland.com/3643/grayscale-image-algorithm-vb6 链接并尝试实现算法。我认为只有你需要的是灰度的变化算法。只需执行流行的一个。 :)
-
@ChintanRathod 感谢您的链接。我会尽力让您知道结果。
标签: android c++ image-processing android-ndk grayscale