2014-01-09 64 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%。