我有一个应用程序内置在Android 2.2和我使用inPreferredConfig()
切换到ARGB_8888
格式的位图,但是,这似乎不工作,因为当以后立即检查位图是仍然在RGB_565
格式。我试图将其更改为任何其他格式,而且这两种格式都无效。inPreferredConfig()在Android中不工作姜饼
如果手机或模拟器运行的是Android 2.2,但该功能无法正常工作,则该功能可以正常工作。有谁知道为什么会发生这种情况?在以后的Android版本中贬值inPreferredConfig()
?
我在做什么:
我使用NDK一些C代码我发现运行一些图像处理功能(从http://www.ibm.com/developerworks/opensource/tutorials/os-androidndk/section5.html拍摄)。 C代码希望图像格式在ARGB_8888中,尽管Android文档说默认情况下格式应该已经在8888中,但它肯定在565中,所以我非常困惑。
我猜我可以将它转换成C ...但我在C上很糟糕,所以我不知道从哪里开始。
我的C函数:
{
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);
}
// 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("unlocking pixels");
AndroidBitmap_unlockPixels(env, bitmapcolor);
AndroidBitmap_unlockPixels(env, bitmapgray);
}
我的Java功能:
// load bitmap from resources
BitmapFactory.Options options = new BitmapFactory.Options();
// Make sure it is 24 bit color as our image processing algorithm expects this format
options.inPreferredConfig = Config.ARGB_8888;
bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.sampleimage,options);
if (bitmapOrig != null)
ivDisplay.setImageBitmap(bitmapOrig);
-
bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8);
convertToGray(bitmapOrig,bitmapWip);
ivDisplay.setImageBitmap(bitmapWip);
感谢,N
PS我的同一主题的最后一个问题了删除,其中h很烦人,因为我无法在任何地方找到任何答案。
如果你添加一些代码,演示,你说的话,这将使它更容易为我们提供帮助。 – inazaruk
我对使用Bitmap.Config方法更改位图格式的知识并不能保证所有时间。 – confucius
感谢您的评论,我用一些代码片段更新了我的问题。 如果不能保证所有的时间都有其他方式来执行任务吗?也许我可以用C来转换它? –