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;
}
我想我「經書」無所不爲。