2012-06-18 40 views
0

我有一個相機util類,我從相機意圖拍攝圖像,也調整拍攝的圖像的大小。Android相機的圖像文件大小

但是,拍攝的圖像大約在100K(調整大小後)時,如何使其保持較小並保持質量。質量只需要在屏幕上顯示大小 - x,y min 320像素。

這裏是班上的壓縮方法:

/* 
* quality Hint to the compressor, 0-100. 0 meaning compress for small size, 
* 100 meaning compress for max quality. Some formats, like PNG which is 
* lossless, will ignore the quality setting 
*/ 
private boolean c(final String i_ImageFileName, final String i_OutputImageFileName) 
{ 
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

bitmapOptions.inJustDecodeBounds = true; 

try 
{ 
     BitmapFactory.decodeStream(new FileInputStream(i_ImageFileName), 
            null, 
            bitmapOptions); 
    } 
catch(FileNotFoundException e) 
{ 
    Log.e(mTAG, "c()- decodeStream- file not found. " + e.getMessage()); 
    return false; 
    } 

//Find the correct scale value. It should be the power of 2. 
final int REQUIRED_SIZE = 320; 
int width_tmp = bitmapOptions.outWidth; 
int height_tmp = bitmapOptions.outHeight; 
int scale  = 1; 

while(true) 
{ 
    if(width_tmp < REQUIRED_SIZE || 
     height_tmp < REQUIRED_SIZE) 
    { 
     break; 
    } 

    width_tmp /= 2; 
    height_tmp /= 2; 
    scale  *= 2; 
} 

// Decode with inSampleSize 
BitmapFactory.Options newBitmapOptions = new BitmapFactory.Options(); 

newBitmapOptions.inSampleSize=scale; 

Bitmap newBitmap = null; 

    newBitmap = BitmapFactory.decodeFile(/*getImageFile*/(i_ImageFileName)/*.getPath()*/ , newBitmapOptions); 

    ByteArrayOutputStream os = new ByteArrayOutputStream(); 

newBitmap.compress(CompressFormat.PNG, 
         100, 
         os); 

    byte[] array = os.toByteArray(); 

    try 
    { 
     FileOutputStream fos = new FileOutputStream(getImageFile(i_OutputImageFileName)); 
     fos.write(array); 
    } 
    catch(FileNotFoundException e) 
    { 
     Log.e(mTAG, "codec- FileOutputStream failed. " + e.getMessage()); 
     return false; 
    } 
    catch(IOException e) 
    { 
     Log.e(mTAG, "codec- FileOutputStream failed. " + e.getMessage()); 
     return false; 
    } 

    return true; 
} 

我想我「經書」無所不爲。

回答

1

那麼,當然,尺寸和質量是你折衷的兩件事。您不能同時擁有最小的文件大小和最高的質量。你在這裏要求最高的質量,它適合你的尺寸太大了。所以,降低質量。

對於PNG,我不知道質量設置做什麼(?)。這是一個無損格式。 (例如,設置爲100甚至可能禁用壓縮。)

這些是哪些圖像?如果他們是線條藝術,如徽標(而不是照片),那麼如果壓縮的PNG很大,我會感到驚訝;這種圖像數據壓縮得很好。 (假設壓縮已開啓!)

對於照片,壓縮不會很好。對於320×320圖像,100KB大約是每像素1個字節。對於PNG,這是一張8位顏色表,如果你想到文件大小,並且256色甚至不能提供很好的圖像質量。

如果他們是照片,你肯定會使用JPG。它更合適。即使採用高質量設置,其有損編碼也應該輕鬆低於100KB。