图片压缩

package com.leju.esf.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.format.DateFormat;

import com.leju.esf.application.AppContext;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

/**
* Description: 图片处理类
*/
public class ImageUtils {

    //计算图片的缩放值
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height/ (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    // 根据路径获得图片并压缩
    public static File compressImage(String filePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 640, 960);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        Bitmap bm = BitmapFactory.decodeFile(filePath, options);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int size = baos.toByteArray().length / 1024;
        baos.reset();//重置baos即清空baos
        if(size > 1024 * 4) { //判断如果图片大于4M,进行40%压缩
            return getSmallBitmap(bm, 40);
        }else if(size > 1024 * 2) { //判断如果图片大于2M,进行60%压缩
            return getSmallBitmap(bm, 60);
        }else if(size > 1024) { //判断如果图片大于1M,进行80%压缩
            return getSmallBitmap(bm, 80);
        }else if(size > 512) { //判断如果图片大于512k,进行90%压缩
            return getSmallBitmap(bm, 90);
        }
        return getSmallBitmap(bm,100);
    }

    public static File getSmallBitmap(Bitmap bm, int quality){
        String imageFolderPath = AppContext.getImageFolderPath();
        if (imageFolderPath == null) {
            return null;
        }
        String photo_path = imageFolderPath + DateFormat.format("yyyy-MM-dd-hh-mm-ss", new Date()) + ".jpg";
        File file = new File(photo_path);
        try {
            bm.compress(Bitmap.CompressFormat.JPEG, quality, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return file;
    }
}
</pre><pre name="code" class="html">调用相机拍照
photo_path = AppContext.getImageFolderPath() + DateFormat.format("yyyy-MM-dd-hh-mm-ss", new Date()) + ".jpg";
imageUrl = Uri.fromFile(new File(photo_path));
if(imageUrl != null){
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUrl);
startActivityForResult(intentCamera, SELECT_CRMERA);
}
</pre><pre name="code" class="html">.
上传服务器把图片的路径
if(imageUrl != null) {
filePath = getRealFilePath(mContext,imageUrl); //根据url获取图片的路径
file = ImageUtils.compressImage(filePath); //压缩后的图路径
params.put("file", file);
}
</pre><pre name="code" class="html">


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值