当前位置:  编程技术>移动开发

android bitmap compress(图片压缩)代码

    来源: 互联网  发布时间:2014-10-17

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

android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:


1. 照相请查看http://www./article/37760.htm ->想要保存图片到制定目录,启动Camera应用时,需要指定文件
2. 压缩过程:
    2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
[java]

代码如下:

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);

 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]
代码如下:

int degree = readPictureDegree(filePath);
        bm = rotateBitmap(bm,degree) ;

int degree = readPictureDegree(filePath);
  bm = rotateBitmap(bm,degree) ;[java] view plaincopyprint?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 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 plaincopyprint?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 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]
代码如下:

bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0

bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0


完整的方法代码:
[java]
代码如下:

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 ;

    }

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 plaincopyprint?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;
    }

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


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • android 自定义Android菜单背景的代码
  • android panellistview 圆角实现代码
  • android 简单图片动画播放的实例代码
  • android网络编程之android连接网络的简单示例代码
  • 用ubuntu下载的android源代码不知下到哪去了
  • Android发送短信功能代码
  • Android Bitmap和Drawable相互转换的简单代码
  • android WakeLock使用方法代码实例
  • android自动安装apk代码实例(不使用apk安装器安装)
  • 控制Android LED灯颜色的代码实例
  • android通过bitmap生成新图片关键性代码
  • android 跳转进市场的实现代码
  • Android键盘显示与隐藏代码
  • Android中监听系统网络连接打开或者关闭的实现代码
  • Android 显示和隐藏输入法实现代码
  • Android点亮屏幕或屏幕解锁和锁定以及其他相关权限实现代码
  • Android中Root权限获取的简单代码
  • android downsample降低音频采样频率代码
  • android连接wifi时获取广播地址代码
  • android layout 按比例布局的代码
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android瀑布流实例 android_waterfall
  • Android开发需要的几点注意事项总结
  • Android系统自带样式 (android:theme)
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android网络共享软件 Android Wifi Tether
  • Android访问与手机通讯相关类的介绍
  • Android 图标库 Android GraphView
  • Android及andriod无线网络Wifi开发的几点注意事项
  • 轻量级Android开发工具 Android Tools
  • Android 2.3 下StrictMode介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3