您的位置:首页 > 移动开发 > Android开发

Android:指定分辨率和清晰度的图片压缩方法源码

2012-02-23 10:16 483 查看
public void transImage(String fromFile, String toFile, int width, int height, int quality)

{

try

{

Bitmap bitmap = BitmapFactory.decodeFile(fromFile);

int bitmapWidth = bitmap.getWidth();

int bitmapHeight = bitmap.getHeight();

// 缩放图片的尺寸

float scaleWidth = (float) width / bitmapWidth;

float scaleHeight = (float) height / bitmapHeight;

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

// 产生缩放后的Bitmap对象

Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);

// save file

File myCaptureFile = new File(toFile);

FileOutputStream out = new FileOutputStream(myCaptureFile);

if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)){

out.flush();

out.close();

}

if(!bitmap.isRecycled()){

bitmap.recycle();//记得释放资源,否则会内存溢出

}

if(!resizeBitmap.isRecycled()){

resizeBitmap.recycle();

}

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException ex)

{

ex.printStackTrace();

}

}


http://www.wizzer.cn/?p=1792
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: