android bitmap compress

android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:
 
1. 照相请查看http://blog.csdn.net/luhuajcdd/article/details/8826587 ->想要保存图片到制定目录,启动Camera应用时,需要指定文件
2. 压缩过程:
    2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
  1. final BitmapFactory.Options options = new BitmapFactory.Options();
  2. options.inJustDecodeBounds = true;
  3. BitmapFactory.decodeFile(filePath, options);
  4. // Calculate inSampleSize
  5. options.inSampleSize = calculateInSampleSize(options, 480, 800);
  6. // Decode bitmap with inSampleSize set
  7. options.inJustDecodeBounds = false;
  8. Bitmap bm = BitmapFactory.decodeFile(filePath, options);

2.2 处理图片旋转

  1. int degree = readPictureDegree(filePath);
  2. bm = rotateBitmap(bm,degree) ;
  1. private static int readPictureDegree(String path) {
  2. int degree  = 0;
  3. try {
  4. ExifInterface exifInterface = new ExifInterface(path);
  5. int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  6. switch (orientation) {
  7. case ExifInterface.ORIENTATION_ROTATE_90:
  8. degree = 90;
  9. break;
  10. case ExifInterface.ORIENTATION_ROTATE_180:
  11. degree = 180;
  12. break;
  13. case ExifInterface.ORIENTATION_ROTATE_270:
  14. degree = 270;
  15. break;
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. return degree;
  21. }
  1. private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
  2. if(bitmap == null)
  3. return null ;
  4. int w = bitmap.getWidth();
  5. int h = bitmap.getHeight();
  6. // Setting post rotate to 90
  7. Matrix mtx = new Matrix();
  8. mtx.postRotate(rotate);
  9. return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
  10. }

2.3压缩图片

  1. bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0
 
完整的方法代码:
  1. public static Bitmap getSmallBitmap(String filePath) {
  2. final BitmapFactory.Options options = new BitmapFactory.Options();
  3. options.inJustDecodeBounds = true;
  4. BitmapFactory.decodeFile(filePath, options);
  5. // Calculate inSampleSize
  6. options.inSampleSize = calculateInSampleSize(options, 480, 800);
  7. // Decode bitmap with inSampleSize set
  8. options.inJustDecodeBounds = false;
  9. Bitmap bm = BitmapFactory.decodeFile(filePath, options);
  10. if(bm == null){
  11. return  null;
  12. }
  13. int degree = readPictureDegree(filePath);
  14. bm = rotateBitmap(bm,degree) ;
  15. ByteArrayOutputStream baos = null ;
  16. try{
  17. baos = new ByteArrayOutputStream();
  18. bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
  19. }finally{
  20. try {
  21. if(baos != null)
  22. baos.close() ;
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. return bm ;
  28. }
  1. private static int calculateInSampleSize(BitmapFactory.Options options,
  2. int reqWidth, int reqHeight) {
  3. // Raw height and width of image
  4. final int height = options.outHeight;
  5. final int width = options.outWidth;
  6. int inSampleSize = 1;
  7. if (height > reqHeight || width > reqWidth) {
  8. // Calculate ratios of height and width to requested height and
  9. // width
  10. final int heightRatio = Math.round((float) height
  11. / (float) reqHeight);
  12. final int widthRatio = Math.round((float) width / (float) reqWidth);
  13. // Choose the smallest ratio as inSampleSize value, this will
  14. // guarantee
  15. // a final image with both dimensions larger than or equal to the
  16. // requested height and width.
  17. inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
  18. }
  19. return inSampleSize;
  20. }

最新文章

  1. Merge Intervals 运行比较快
  2. 开发错误记录8:Unable to instantiate application com
  3. 为什么我的SQL server 在附加数据库后,数据库总是变成了只读?
  4. Pre-Query trigger in Oracle D2k / Oracle Forms
  5. 自定义UISearchDisplayController中搜索到结果的cell的位置
  6. Java Servlet与Applet、CGI、JSP的比较
  7. C_FORCE_ROOT linux环境变量设置
  8. external 里面文件的介绍
  9. 纯JS实现像素逐渐显示
  10. Elasticsearch分片、副本与路由(shard replica routing)
  11. 解释器模式(Interpreter)
  12. httpclient的理解(代码理解)
  13. 【BZOJ1717】产奶的模式(后缀数组)
  14. 阿里云API网关(1)服务网关的产品概述
  15. java异常处理之throw, throws,try和catch
  16. [Error]Python虚拟环境报错 OSError: setuptools pip wheel failed with error code 2
  17. Linux下0号进程的前世(init_task进程)今生(idle进程)----Linux进程的管理与调度(五)【转】
  18. Sqlserver 命令行方式修改 用户密码的方法
  19. unity, 在image effect shader中用_CameraDepthTexture重建世界坐标
  20. java继承,final,super,Object类,toString,equals,

热门文章

  1. SharePoint 2010 应用url参数过滤列表视图数据(应用get办法过滤列表数据)
  2. Java-多重if 结构
  3. 近十年one-to-one最短路算法研究整理【转】
  4. js判断当前时间前几天和格式校验
  5. Git中的merge命令实现中出现问题及其解决
  6. runtime基础
  7. merge 语句的语法
  8. 小师妹问 easyUI mergeCells 行合并后表头和内容对不齐
  9. Swift 与 JSON 数据
  10. JSON文件存入MySQL数据库