检查用于创建位图的图像的最有效方法是.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;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...