图片压缩笔记

package com.wsjia.internalusers.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2018/3/16 0016.
 */

public class ImageCompress {

    public static String compress(String path){

        Bitmap bit = BitmapFactory.decodeFile(path,getBitmapOption(4));
//        Matrix matrix = new Matrix();
//        matrix.setScale(0.3f, 0.3f);
//        Bitmap bm = Bitmap.createBitmap(bit, 0, 0, bit.getWidth()/3,
//                bit.getHeight()/3, matrix, true);
//        Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024)
//                + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());
        sizeCompress(bit,path);
        return path;
//        return saveBitmapFile(bm,path);
    }

    private static BitmapFactory.Options getBitmapOption(int inSampleSize){
        System.gc();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inSampleSize = inSampleSize;
        return options;
    }

    private static void sizeCompress(Bitmap bmp, String path) {
        // 尺寸压缩倍数,值越大,图片尺寸越小
        int ratio = 2;
        // 压缩Bitmap到对应尺寸
        int width = bmp.getWidth();
        int height = bmp.getHeight();
        if(width<500||height<600){
            return;
        }

        Bitmap result = Bitmap.createBitmap(width / ratio, height / ratio, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Rect rect = new Rect(0, 0, bmp.getWidth() , bmp.getHeight() );
        canvas.drawBitmap(bmp, null, rect, null);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 把压缩后的数据存放到baos中
        result.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        try {
            File file = new File(path);
            if(file.exists()){
                if (file.delete())
                {
                    file = new File(path);
                    file.createNewFile();
                }
            }else {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String saveBitmapFile(Bitmap bitmap,String path){
        File file=new File(path);//将要保存图片的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值