android bitmap compress(图片压缩)代码

android bitmap compress(图片压缩)代码,第1张

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

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


1. 照相请查看http://www.jb51.net/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,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,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,true);
 }


2.3压缩图片      
[java]
复制代码 代码如下:
bm.compress(Bitmap.CompressFormat.JPEG,30,baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0

bm.compress(Bitmap.CompressFormat.JPEG,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,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,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);
  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,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;
 }

您可能感兴趣的文章:详解android 通过uri获取bitmap图片并压缩Android Bitmap压缩方式分析Android实现图片压缩(bitmap的六种压缩方式)Android Bitmap压缩方法的选择详解详解Android Bitmap的常用压缩方式 总结

以上是内存溢出为你收集整理的android bitmap compress(图片压缩)代码全部内容,希望文章能够帮你解决android bitmap compress(图片压缩)代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142866.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存