android图片压缩问题

本文介绍了一种结合尺寸裁剪与质量压缩的方法来降低图片文件大小,适用于移动端上传大图的场景,确保图片清晰度的同时减少带宽消耗。

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

图片压缩:可以从两个方面入手:

第一:将图片剪切:

//压缩图片按倍数压:
private Options getBitmapOption(int inSampleSize){
        System.gc();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inSampleSize = inSampleSize;
        return options;
}



第二:质量压缩:

重要的函数: bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//如果签名是png的话,则不管quality是多少,都不会进行质量的压缩

//获取bitmap内存
public static long getSizeOfBitmap(Bitmap bitmap)
{
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//这里100的话表示不压缩质量


  long length=baos.toByteArray().length/1024;//读出图片的kb大小
  return length;
  
}


/压缩图片到指定范围
public static byte[] compressBitmap(Bitmap bitmap,float size){


        if(bitmap==null||getSizeOfBitmap(bitmap)<=size){


                return null;//如果图片本身的大小已经小于这个大小了,就没必要进行压缩


        }


        ByteArrayOutputStream baos = new ByteArrayOutputStream();


        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//如果签名是png的话,则不管quality是多少,都不会进行质量的压缩


        int quality=100;
          while (baos.toByteArray().length / 1024f>size) {


                   quality=quality-4;// 每次都减少4                              //如果把太大的图片压缩太多倍的的话,会压缩很久
                  if(quality<=0){


                                break;
                 }
                  baos.reset();// 重置baos即清空baos


               bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);   
               


     }


     return baos.toByteArray();


}



而在开发中,我一般压缩都是两者混合着用:

比如下面例子:

我将一个图片压缩到大概60kb左右,然后上传:


try
        {


       
        URL url = new URL(uploadUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url
        .openConnection();
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type",
        "multipart/form-data;boundary=" + boundary);


        DataOutputStream dos = new DataOutputStream(
        httpURLConnection.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + end);
        dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
        +tupianlujin+ filename.substring(filename.lastIndexOf("/") + 1)
        + "\""
        + end);
        dos.writeBytes(end);
       

/////////////////////////////////////////////////////////////////////////////////////////图片压缩///////////////////////////////////////
        //压缩图片:先转为Bitmap;然后裁剪,然后在转为文件格式                    
        Bitmap bitmap=BitmapFactory.decodeFile(filename,getBitmapOption(2));  //首先减半先
       
        InputStream fis ;
        if(getSizeOfBitmap(bitmap)<=61.2f)        //若大图小于61.2k,直接传
        {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
        fis = new ByteArrayInputStream(baos.toByteArray());
        }
        else                                 //压缩到特定范围
        {
        bitmap=BitmapFactory.decodeFile(filename,getBitmapOption(2)); //裁剪4分之一          //这步可以不要
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
        if(compressBitmap(bitmap,61.2f)==null)
        {
        fis = new ByteArrayInputStream(baos.toByteArray());
        }
        else
        {
        fis = new ByteArrayInputStream(compressBitmap(bitmap,61.2f));
        }
        }
        /////////////////////////////////////////////////////////////////////////////////////////图片压缩///////////////////////////////////////
        byte[] buffer = new byte[8192]; // 8k
        int count = 0;
        while ((count = fis.read(buffer)) != -1)
        {
        dos.write(buffer, 0, count);


        }
        fis.close();


        dos.writeBytes(end);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
        dos.flush();


        InputStream is = httpURLConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        BufferedReader br = new BufferedReader(isr);
        String result = br.readLine();


        //Toast.makeText(this, result, Toast.LENGTH_LONG).show();
        dos.close();
        is.close();
       


        }
        catch (Exception e)
        {
        setTitle(e.getMessage());
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值