I have a camera util class with which I take images from the camera intent, also resize the image taken.
However images taken are around 100K (After resizing), How can I make it smaller with preserving the quality. Quality needs only fo showing on the screen in the size - x,y min 320 pixels.
Here is the compression method in the class:
/*
* 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;
}
I think I am doing everything "by the booK".