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

Android图片压缩处理

2016-01-15 16:18 417 查看
在Android APP开发中经常会有上传图片和上传用户头像的功能,但是我们知道,手机拍摄的照片大多都有2M - 5M,把这么大的图片上传上去是需要很长时间的,并且图片过大非常容易导致应用卡顿甚至是OOM直接崩溃掉,那么为了优化性能就必须要做图片压缩处理,在最近的项目中通过查阅资料自己封装了图片压缩处理的工具类,大家在开发中应该用的到。代码如下:

/**
* Author: lihongxiang
* Time: 上午 11:38
* Email:lihongxiangleo@163.com
*/

public class CompressImageUtils {
public static Bitmap getSmallBitmap(String filePath) {

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;

Bitmap bm = BitmapFactory.decodeFile(filePath, options);
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm, degree);
if (bm == null) {
return null;
}
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);

} finally {
try {
if (baos != null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;

}

private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}

//处理图片旋转
private static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
if (bitmap == null)
return null;

int w = bitmap.getWidth();
int h = bitmap.getHeight();

// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}

return inSampleSize;
}

/**
* 将Bitmap保存到本地
*
* @param srcPath
* @param mBitmap
* @throws IOException
*/
public static void saveMyBitmap(String srcPath, Bitmap mBitmap) throws IOException {
File f = new File(srcPath);
f.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}


以上工具类可以完成(1)图片的压缩 (2)将压缩后的Bitmap保存到本地

然后就可以从本地拿到图片上传到服务器了。压缩后的图片上传的时间就会大大缩短,并且会避免程序的OOM。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: