android bitmap compress
2016-04-15 14:50
405 查看
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些;
压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:
1. 照相请查看http://blog.csdn.net/luhuajcdd/article/details/8826587 ->想要保存图片到制定目录,启动Camera应用时,需要指定文件
2. 压缩过程:
2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
[java] view
plain copy
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);
2.2 处理图片旋转
[java] view
plain copy
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
[java] view
plain copy
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;
}
[java] view
plain copy
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);
}
2.3压缩图片
[java] view
plain copy
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0
完整的方法代码:
[java] view
plain copy
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);
if(bm == null){
return null;
}
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
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 ;
}
[java] view
plain copy
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;
}
相关文章推荐
- android中常用的读取文件的用法如下
- Android布局初步
- [置顶] android开发中防止刚进入activity时edittext获取焦点,防止自动自动弹出软键盘
- listView的使用之例如QQ消息列表
- 实例讲解Android应用中自定义组合控件的方法
- Android Studio下的项目结构以及Drawable的使用
- Android Studio 全屏问题
- 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色
- 【转】android的编译和运行过程深入分析
- Android开发,关于依赖库和JAR包的区别以及使用多个依赖库可能出现的问题(SlidingMenu、SwipeBackLib)
- Android头像选择(手机和相册)
- Android动画之ScaleAnimation使用
- android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@428f7
- Android Studio学习之安装插件
- Android中View自定义组合控件的基本编写方法
- Android软键盘弹出时把布局顶上去的解决方法
- 当 Activity 以全屏模式运行时,如何允许 Android 系统状态栏在顶层出现,而不迫使 Activity 重新布局让出空间?
- Android 五大布局讲解与应用
- 讯飞语音包实现Android语音理解①
- Android系统启动过程