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. }

android bitmap compress(图片压缩)的更多相关文章

  1. [Android算法] bitmap 将图片压缩到指定的大小

    Bitmap压缩到指定大小: private void imageZoom() {//图片允许最大空间 单位:KBdouble maxSize =400.00;//将bitmap放至数组中,意在bit ...

  2. Bitmap之compress图片压缩

    package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; i ...

  3. Android中的图片压缩

    1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB ...

  4. 有关Option.inSamplSize 和 Compress 图片压缩

    1.图片占用内存:占用的内存 = 图片长度 * 图片宽度 * 单位像素占用的字节数 注: 图片长度和图片宽度就是图片在行列上的像素数量. 图片格式: ALPHA_8:表示8位Alpha位图,即A=8, ...

  5. Android笔记(七十五) Android中的图片压缩

    这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...

  6. Android学习之图片压缩,压缩程度高且失真度小

    曾经在做手机上传图片的时候.直接获取相机拍摄的原图上传,原图大小一般1~2M.因此上传一张都比較浪费资源,有些场景还须要图片多张上传,所以近期查看了好多前辈写的关于图片处理的资料.然后试着改了一个图片 ...

  7. android bitmap compress

    android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片. 有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量 ...

  8. Android 简单介绍图片压缩和图片内存缓存

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工, ...

  9. Android 中图片压缩分析(上)

    作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...

随机推荐

  1. [转]Zend Studio 文件头和方法注释设置

    在zend studio ide 7.1 中选择窗口->首选项->PHP–>编辑器 –>模板 –>新建然后添加 funinfo或fileinfo 模板代码根据下边定义的C ...

  2. 改良UIScrollView滚动视图

    #define HEIGHT  self.view.frame.size.height #define WIDTH    self.view.frame.size.width @interface V ...

  3. 不注册COM组件直接调用接口

    本文以COM组件AppTest.dll为例,AppTest.dll中提供了ITest接口,在不使用regsvr32命令向系统注册的情况下创建ITest接口并调用. 一.导入组件或类型库: 在C++中使 ...

  4. 【转】configure/make/make install的使用说明

    这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GCC ...

  5. 35W行数据的文本文件,每500行添加4个换行符。代码实现思路

  6. 美团,点评,澎湃等APP的启示

    事先声明,因个人能力尚浅,文章若有不足之处,望留言指出,也欢迎成为好朋友. 本来想打算写团购类APP的竞品分析,但是发现不管是天天果园这样生鲜APP,还是澎湃这样的新闻资讯APP,思路差不多,都是: ...

  7. jsp base标签与meta标签学习小结

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  8. JDBC 事务隔离级别

    JDBC 事务隔离级别     先解释一下:a:脏读取:一个事务读取了另外一个并行事务未提交的数据b:不可重复读取:一个事务再次读取之前的数据时得到的数据不一致,被另外一个事务修改c:虚读:一个事务重 ...

  9. JSP 基础

    定义 JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写html,但它相 ...

  10. Cocos2d 使用控制台打印的方法

    1.打开当前项目的win32解决方案. 2.在解决方案管理器的win32文件夹下打开main.cpp 3.增加以下代码: #define USE_WIN32_CONSOLE //以下加到入口函数 #i ...