如何在不改变分辨率的情况下改变图像的大小?

时间:2014-01-09 11:13:56

标签: android image-processing

我正在开发一个应用程序,我希望减少图像的大小。例如,如果大小是1MB,那么我希望它减少到kb。

但决议不应该改变。

有人可以提供帮助吗?

我尝试了这段代码,但它无效

 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;

}

5 个答案:

答案 0 :(得分:0)

如果您不想调整图片大小,那么您唯一的选择就是压缩它。

以下是如何执行此操作的示例:

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

答案 1 :(得分:0)

在我的想法中,在大多数情况下,我们不能这样做。因为它与图像的分辨率和图像的颜色范围有关。

所以,如果我们有大量颜色的大图像,那么缩小图像的大小会诱导降低它的分辨率。

答案 2 :(得分:0)

有两个aproches:有损(您将失去图像的质量)和无损

rickman提供的答案是有损的,因此效果不错但会降低图像质量 - 对于使用相机拍摄的照片效果特别好。

无损方法正在使用PNG,其中包括使用Bitmap.CompressFormat.PNG作为compress方法的参数。

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

答案 3 :(得分: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;
}

答案 4 :(得分:0)

如果您希望在不失去大量质量的情况下缩小图像的文件大小,可以执行以下操作: 首先你需要压平图像,然后保存为web(ctrl + alt + shift + s) 然后选择预设的PNG-8 128 Dithered。 您现在可以通过将颜色设置为“256”来自定义此预设,然后在PNG-8下选择“选择性”,在“选择性”下将抖动更改为“扩散”,取消选择“隔行扫描”选项并使网络快照变为舒适选项设置为0%。

相关问题