2014-01-09 67 views
2

我正在开发一个应用程序,我想要减少我的图像的大小。例如,如果大小为1MB,那么我希望它得到减少到kb。如何改变图像的大小而不改变resoultion?

但是分辨率不应该改变。

任何人都可以帮忙吗?

我想这个代码,但它不工作

public static Bitmap resizeBitMapImage1(String filePath, int targetWidth, int targetHeight) { 
Bitmap bitMapImage = null; 
try { 
    Options options = new Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, options); 
    double sampleSize = 0; 
    Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth 
      - targetWidth); 
    if (options.outHeight * options.outWidth * 2 >= 1638) { 
     sampleSize = scaleByHeight ? options.outHeight/targetHeight : options.outWidth/targetWidth; 
     sampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize)/Math.log(2d))); 
    } 
    options.inJustDecodeBounds = false; 
    options.inTempStorage = new byte[128]; 
    while (true) { 
     try { 
      options.inSampleSize = (int) sampleSize; 
      bitMapImage = BitmapFactory.decodeFile(filePath, options); 
      break; 
     } catch (Exception ex) { 
      try { 
       sampleSize = sampleSize * 2; 
      } catch (Exception ex1) { 

      } 
     } 
    } 
} catch (Exception ex) { 

} 
return bitMapImage; 

}

使用此代码它会降低分辨率,但图像没有太大的规模。

我也试过

public static Bitmap reduceImgSizeToHundredKB(Bitmap bitmap) { 
Bitmap scaled = bitmap; 
try { 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream); 

} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
    return null; 
} 
    return scaled; 

}

+2

“我想吃蛋糕,保持蛋糕”的东西。你所能做的只是使用不同的压缩类型,使用量化来减少颜色深度,但无论如何也没有什么大的魔力 –

回答

0

如果你不想调整图片大小,那么你唯一的选择就是对其进行压缩。

这里是如何做到这一点的例子:

byte[] data = null; 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    data = baos.toByteArray(); 
+0

请看我编辑的问题 – Meenal

0

在我的思想,在大多数情况下,我们不能这样做。因为它涉及图像的分辨率和图像的颜色范围。

所以,如果我们有很多颜色的大图像,那么缩小图像的尺寸会导致图像的分辨率降低。

0

有两个aproches:有损(你会失去图像质量)和无损

由rickman提供的答案是有损的,因此它可以很好地工作,但会降低图像的质量 - 对于使用相机拍摄的照片来说,它效果特别好。

无损方法使用PNG,它使用Bitmap.CompressFormat.PNG作为compress方法的参数。

一个不是众所周知的但非常有效的格式是WebP。它也可以作为压缩方法的参数(Bitmap.CompressFormat.PNG)。我建议用WebP进行测试,特别是考虑到它支持有损压缩和无损压缩。

如果从路径解码
0

使用此方法

public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
    int reqHeight) { 

final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(path, options); 

options.inSampleSize = calculateInSampleSize(options, reqWidth, 
     reqHeight); 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
Bitmap bmp = BitmapFactory.decodeFile(path, options); 
return bmp; 
} 
} 
    public int calculateInSampleSize(BitmapFactory.Options options, 
    int reqWidth, int reqHeight) { 

final int height = options.outHeight; 
final int width = options.outWidth; 
int inSampleSize = 1; 

if (height > reqHeight || width > reqWidth) { 
    if (width > height) { 
     inSampleSize = Math.round((float) height/(float) reqHeight); 
    } else { 
     inSampleSize = Math.round((float) width/(float) reqWidth); 
    } 
} 
return inSampleSize; 
} 

使用这个,如果你是从资源

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
    int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
    } 

public static int calculateInSampleSize(
    BitmapFactory.Options options, int reqWidth, int reqHeight) { 
// Raw height and width of image 
final int height = options.outHeight; 
    final int width = options.outWidth; 
int inSampleSize = 1; 

if (height > reqHeight || width > reqWidth) { 

// Calculate ratios of height and width to requested height and width 
final int heightRatio = Math.round((float) height/(float) reqHeight); 
    final int widthRatio = Math.round((float) width/(float) reqWidth); 

// Choose the smallest ratio as inSampleSize value, this will guarantee 
// a final image with both dimensions larger than or equal to the 
// requested height and width. 
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
} 

return inSampleSize; 
} 
+0

我会试试这个代码 – Meenal

0

如果你想减少图像文件的大小解码而不会损失大量的质量,你可以这样做: 首先,你需要将图像变平,然后将其保存为网页(ctrl + alt + shift + s) n选择预设的PNG-8 128抖动。 现在,您可以通过将颜色设置为“256”,然后选择PNG-8下的“选择性”,在“选择性”更改抖动到“扩散”下取消选择“隔行扫描”选项,并确保网络捕捉选项设置为0%。