android bitmap compress

本文探讨了Android应用中优化图片上传速度与流量消耗的方法,通过压缩图片大小来提高应用性能。介绍了从读取图片、旋转图片、计算缩放比例,到最终压缩并保存图片的技术流程,确保在保持图片清晰度的同时,减少上传时间与流量使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。


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


1. 照相请查看http://blog.youkuaiyun.com/luhuajcdd/article/details/8826587 ->想要保存图片到制定目录,启动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;
 }

















package com.testbitmapscale; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Iterator; 
import com.testbitmapscale.R.drawable; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.PorterDuff.Mode; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.Rect; 
import android.graphics.RectF; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.media.ThumbnailUtils; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.widget.ImageView; 
//方法: 
//1 生成圆角Bitmap图片 
//2 生成Bitmap缩量图 
//3 压缩图片场长宽以及kB 
//注意: 
//以上代码,测试其中一个方法时最好注释掉其余的代码 
public class MainActivity extends Activity { 
private ImageView imageView; 
private Bitmap copyRawBitmap1; 
private Bitmap copyRawBitmap2; 
private Bitmap copyRawBitmap3; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
imageView = (ImageView) findViewById(R.id.imageView); 
//第一种方式:从资源文件中得到图片 
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha); 
copyRawBitmap1=rawBitmap; 
copyRawBitmap2=rawBitmap; 
copyRawBitmap3=rawBitmap; 
//第二种方式:从SD卡中得到图片(方法1) 
String SDCarePath=Environment.getExternalStorageDirectory().toString(); 
String filePath=SDCarePath+"/"+"haha.jpg"; 
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null); 
//第二种方式:从SD卡中得到图片(方法2) 
InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg"); 
Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream); 

//————>以下为将设置图片的圆角 
Bitmap roundCornerBitmap=this.toRoundCorner(rawBitmap, 40); 
imageView.setImageBitmap(roundCornerBitmap); 
//————>以上为将设置图片的圆角 

//————>以下为将图片高宽和的大小kB压缩 
// 得到图片原始的高宽 
int rawHeight = rawBitmap.getHeight(); 
int rawWidth = rawBitmap.getWidth(); 
// 设定图片新的高宽 
int newHeight = 500; 
int newWidth = 500; 
// 计算缩放因子 
float heightScale = ((float) newHeight) / rawHeight; 
float widthScale = ((float) newWidth) / rawWidth; 
// 新建立矩阵 
Matrix matrix = new Matrix(); 
matrix.postScale(heightScale, widthScale); 
// 设置图片的旋转角度 
//matrix.postRotate(-30); 
// 设置图片的倾斜 
//matrix.postSkew(0.1f, 0.1f); 
//将图片大小压缩 
//压缩后图片的宽和高以及kB大小均会变化 
Bitmap newBitmap = Bitmap.createBitmap(rawBitmap, 0, 0, rawWidth,rawWidth, matrix, true); 
// 将Bitmap转换为Drawable 
Drawable newBitmapDrawable = new BitmapDrawable(newBitmap); 
imageView.setImageDrawable(newBitmapDrawable); 
//然后将Bitmap保存到SDCard中,方便于原图片的比较 
this.compressAndSaveBitmapToSDCard(newBitmap, "xx100.jpg", 80); 
//问题: 
//原图大小为625x690 90.2kB 
//如果设置图片500x500 压缩后大小为171kB.即压缩后kB反而变大了. 
//原因是将:compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream); 
//第二个参数quality设置得有些大了(比如100). 
//常用的是80,刚设100太大了造成的. 
//————>以上为将图片高宽和的大小kB压缩 


//————>以下为将图片的kB压缩,宽高不变 
this.compressAndSaveBitmapToSDCard(copyRawBitmap1,"0011fa.jpg",80); 
//————>以上为将图片的kB压缩,宽高不变 

//————>以下为获取SD卡图片的缩略图方法1 
String SDCarePath1=Environment.getExternalStorageDirectory().toString(); 
String filePath1=SDCarePath1+"/"+"haha.jpg"; 
Bitmap bitmapThumbnail1=this.getBitmapThumbnail(filePath1); 
imageView.setImageBitmap(bitmapThumbnail1); 
//————>以上为获取SD卡图片的缩略图方法1 

//————>以下为获取SD卡图片的缩略图方法2 
String SDCarePath2=Environment.getExternalStorageDirectory().toString(); 
String filePath2=SDCarePath2+"/"+"haha.jpg"; 
Bitmap tempBitmap=BitmapFactory.decodeFile(filePath2); 
Bitmap bitmapThumbnail2=ThumbnailUtils.extractThumbnail(tempBitmap, 100, 100); 
imageView.setImageBitmap(bitmapThumbnail2); 
//————>以上为获取SD卡图片的缩略图方法2 


//读取SD卡下的图片 
private InputStream getBitmapInputStreamFromSDCard(String fileName){ 
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
String SDCarePath=Environment.getExternalStorageDirectory().toString(); 
String filePath=SDCarePath+File.separator+fileName; 
File file=new File(filePath); 
try { 
FileInputStream fileInputStream=new FileInputStream(file); 
return fileInputStream; 
} catch (Exception e) { 
e.printStackTrace(); 



return null; 



//获取SDCard的目录路径功能 
private String getSDCardPath() { 
String SDCardPath = null; 
// 判断SDCard是否存在 
boolean IsSDcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
if (IsSDcardExist) { 
SDCardPath = Environment.getExternalStorageDirectory().toString(); 

return SDCardPath; 

//压缩且保存图片到SDCard 
private void compressAndSaveBitmapToSDCard(Bitmap rawBitmap,String fileName,int quality){ 
String saveFilePaht=this.getSDCardPath()+File.separator+fileName; 
File saveFile=new File(saveFilePaht); 
if (!saveFile.exists()) { 
try { 
saveFile.createNewFile(); 
FileOutputStream fileOutputStream=new FileOutputStream(saveFile); 
if (fileOutputStream!=null) { 
//imageBitmap.compress(format, quality, stream); 
//把位图的压缩信息写入到一个指定的输出流中 
//第一个参数format为压缩的格式 
//第二个参数quality为图像压缩比的值,0-100.0 意味着小尺寸压缩,100意味着高质量压缩 
//第三个参数stream为输出流 
rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream); 

fileOutputStream.flush(); 
fileOutputStream.close(); 
} catch (IOException e) { 
e.printStackTrace(); 





//获取图片的缩略图 
private Bitmap getBitmapThumbnail(String filePath){ 
BitmapFactory.Options options=new BitmapFactory.Options(); 
//true那么将不返回实际的bitmap对象,不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息 
options.inJustDecodeBounds=true; 
//此时rawBitmap为null 
Bitmap rawBitmap = BitmapFactory.decodeFile(filePath, options); 
if (rawBitmap==null) { 
System.out.println("此时rawBitmap为null"); 

//inSampleSize表示缩略图大小为原始图片大小的几分之一,若该值为3 
//则取出的缩略图的宽和高都是原始图片的1/3,图片大小就为原始大小的1/9 
//计算sampleSize 
int sampleSize=computeSampleSize(options, 150, 200*200); 
//为了读到图片,必须把options.inJustDecodeBounds设回false 
options.inJustDecodeBounds = false; 
options.inSampleSize = sampleSize; 
//原图大小为625x690 90.2kB 
//测试调用computeSampleSize(options, 100, 200*100); 
//得到sampleSize=8 
//得到宽和高位79和87 
//79*8=632 87*8=696 
Bitmap thumbnailBitmap=BitmapFactory.decodeFile(filePath, options); 
//保存到SD卡方便比较 
this.compressAndSaveBitmapToSDCard(thumbnailBitmap, "15.jpg", 80); 
return thumbnailBitmap; 


//参考资料: 
//http://my.youkuaiyun.com/zljk000/code/detail/18212 
//第一个参数:原本Bitmap的options 
//第二个参数:希望生成的缩略图的宽高中的较小的值 
//第三个参数:希望生成的缩量图的总像素 
public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { 
int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels); 
int roundedSize; 
if (initialSize <= 8) { 
roundedSize = 1; 
while (roundedSize < initialSize) { 
roundedSize <<= 1; 

} else { 
roundedSize = (initialSize + 7) / 8 * 8; 

return roundedSize; 


private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { 
//原始图片的宽 
double w = options.outWidth; 
//原始图片的高 
double h = options.outHeight; 
System.out.println("========== w="+w+",h="+h); 
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math 
.sqrt(w * h / maxNumOfPixels)); 
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( 
Math.floor(w / minSideLength), Math.floor(h / minSideLength)); 
if (upperBound < lowerBound) { 
// return the larger one when there is no overlapping zone. 
return lowerBound; 

if ((maxNumOfPixels == -1) && (minSideLength == -1)) { 
return 1; 
} else if (minSideLength == -1) { 
return lowerBound; 
} else { 
return upperBound; 



/** 
* @param bitmap 需要修改的图片 
* @param pixels 圆角的弧度 
* @return 圆角图片 
*/ 
//参考资料: 
//http://blog.youkuaiyun.com/c8822882/article/details/6906768 
public Bitmap toRoundCorner(Bitmap bitmap, int pixels) { 
Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
Canvas canvas = new Canvas(roundCornerBitmap); 
int color = 0xff424242;//int color = 0xff424242; 
Paint paint = new Paint(); 
paint.setColor(color); 
//防止锯齿 
paint.setAntiAlias(true); 
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
RectF rectF = new RectF(rect); 
float roundPx = pixels; 
//相当于清屏 
canvas.drawARGB(0, 0, 0, 0); 
//先画了一个带圆角的矩形 
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
//再把原来的bitmap画到现在的bitmap!!!注意这个理解 
canvas.drawBitmap(bitmap, rect, rect, paint); 
return roundCornerBitmap; 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值