检查用于创建位图的图像是否有效的方法是.PNG-Android



给定一个位图对象,我想确定用于创建位图的图像是.PNG格式还是.JPG格式。我写了下面的代码来确定这一点。我对位图执行缩放操作,然后逐像素搜索,以查找是否有透明像素。有更好的方法吗?

public static boolean isBitmapPNG(Bitmap bitmap)
{
int scaledWidth=20;
int scaledHeight=20;
Bitmap scaledBitmap = resizeBitmap(bitmap,scaledWidth,scaledHeight);
for (int x=0;x<scaledBitmap.getWidth();x++)
for (int y=0;y<scaledBitmap.getHeight();y++)
if (((scaledBitmap.getPixel(x,y) & 0xff000000) >> 24)==0)
return true;

return false;     
}

public static Bitmap resizeBitmap(Bitmap image, int maxWidth, int maxHeight)
{
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > ratioBitmap) {
finalWidth = (int) ((float)maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float)maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}
}

尝试:

CCD_ 1方法。如果它返回true,那么它就是一个png。如果没有,则为jpg。

最新更新