我有一个相机工具类,我用它从相机意图中获取图像,还调整所拍摄图像的大小。
但是拍摄的图像约为 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;
}
我认为我正在“按规定”做所有事情。