2012-09-11 30 views
1

我想知道是否有人知道強制Android使用compress函數創建索引彩色PNG文件的方法。用Android創建索引彩色PNG

例如:

InputStream FIS = ... 
    BitmapFactory.Options opt = new BitmapFactory.Options(); 
    opt.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    opt.inScaled = false; 
    Bitmap img = BitmapFactory.decodeStream(FIS, null, opt); 

    // Resize 
    float scale = 0.8f; 
    Matrix matrix = new Matrix(); 
    matrix.postScale(scale, scale); 
    Bitmap scaledBitmap = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true); 
    img = null; // Free 

    // Write 
    File sdCard = Environment.getExternalStorageDirectory(); 
    File dir = new File(sdCard.getAbsolutePath() + "/scaled/"); 
    FileOutputStream FOS = new FileOutputStream(new File(dir, "out.png")); 
    scaledBitmap.compress(Bitmap.CompressFormat.PNG, 100, FOS); 
    scaledBitmap = null; // Free 

這段代碼打開一個PNG文件,它會調整到80%,並將其保存到SD卡。生成的圖像確實可以縮放到80%,但生成的文件大小几乎是原始大小的5倍。

 
-rw-rw-r-- 1 user user 55878 Sep 10 19:00 8500_001.2B.png <- Input 
-rwxr--r-- 1 user user 245933 Sep 10 21:49 out.png   <- Output 

這樣做的原因是因爲原來的文件使用索引顏色(僞類),而不是真彩色(DirectClass)。 [1]

 
$ identify 8500_001.2B.png 
8500_001.2B.png PNG 1712x2200 1712x2200+0+0 8-bit PseudoClass 2c 55.9KB 0.000u 0:00.000 

$ identify out.png 
out.png PNG 1370x1760 1370x1760+0+0 8-bit DirectClass 246KB 0.000u 0:00.000 

ImageMagick足夠智能,可以使用索引顏色和雙色彩圖對原始雙色圖像進行編碼。在Android中打開,縮放和重新編碼之後,該文件不會這樣做,而是使用每個像素的真實顏色,並導致更大的文件大小。

問題

  1. 有誰知道是否有一種方法來強制標準Android庫壓縮使用顏色表文件?
  2. 如果沒有,是否有人知道是否有任何純Java實現可以完成這3個任務(解碼,縮放,編碼)?

在此先感謝。

[1] The PNG Specification

回答