对图片做处理的一个的工具类

话不多说直接上代码,一切话都在代码中。

package com.example.demo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageUtils {
    /**
     * 从本地path中获取bitmap,压缩后保存小图片到本地
     * 
     * @param context
     * @param path
     *            图片存放的路径
     * 
     * @return 返回压缩后图片的存放路径
     */
    public static String saveBitmap(Context context, String path) {
        String compressdPicPath = "";

        // ★★★★★★★★★★★★★★重点★★★★★★★★★★★★★
        /*
         * //★如果不压缩直接从path获取bitmap,这个bitmap会很大,下面在压缩文件到100kb时,会循环很多次, //
         * ★而且会因为迟迟达不到100k,options一直在递减为负数,直接报错 //★
         * 即使原图不是太大,options不会递减为负数,也会循环多次,UI会卡顿,所以不推荐不经过压缩,直接获取到bitmap Bitmap
         * bitmap=BitmapFactory.decodeFile(path);
         */
        // ★★★★★★★★★★★★★★重点★★★★★★★★★★★★★

        // 建议先将图片压缩到控件所显示的尺寸大小后,再压缩图片质量
        // 首先得到手机屏幕的高宽,根据此来压缩图片,当然想要获取跟精确的控件显示的高宽(更加节约内存),可以使用getImageViewSize();
        DisplayMetrics displayMetrics = context.getResources()
                .getDisplayMetrics();
        int width = displayMetrics.widthPixels; // 屏幕宽度(像素)
        int height = displayMetrics.heightPixels; // 屏幕高度(像素)
        // 获取按照屏幕高宽压缩比压缩后的bitmap
        Bitmap bitmap = decodeSampledBitmapFromPath(path, width, height);

        String oldName = path.substring(path.lastIndexOf("/"),
                path.lastIndexOf("."));
        String name = oldName + "_compress.jpg";// ★很奇怪oldName之前不能拼接字符串,只能拼接在后面,否则图片保存失败
        String saveDir = Environment.getExternalStorageDirectory()
                + "/compress";
        File dir = new File(saveDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        // 保存入sdCard
        File file = new File(saveDir, name);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        /* options表示 如果不压缩是100,表示压缩率为0。如果是70,就表示压缩率是70,表示压缩30%; */
        int options = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        while (baos.toByteArray().length / 1024 > 500) {
            // 循环判断如果压缩后图片是否大于500kb继续压缩

            baos.reset();
            options -= 10;
            if (options < 11) {// 为了防止图片大小一直达不到500kb,options一直在递减,当options<0时,下面的方法会报错
                // 也就是说即使达不到500kb,也就压缩到10了
                bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
                break;
            }
            // 这里压缩options%,把压缩后的数据存放到baos中
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
        }

        try {
            FileOutputStream out = new FileOutputStream(file);
            out.write(baos.toByteArray());
            out.flush();
            out.close();
            compressdPicPath = file.getAbsolutePath();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return compressdPicPath;
    }

    /**
     * 根据ImageView获取适当的压缩的宽和高 尽可能得到ImageView的精确的宽高
     * 
     * @param imageView
     * @return
     */
    public static ImageSize getImageViewSize(ImageView imageView) {

        // 得到屏幕的宽度
        DisplayMetrics displayMetrics = imageView.getContext().getResources()
                .getDisplayMetrics();

        ImageSize imageSize = new ImageSize();
        ViewGroup.LayoutParams lp = imageView.getLayoutParams();

        // ------------------------------------尽可能得到ImageView的精确的宽高-------------------------------------------------------------
        // 得到imageView的实际宽度(为了压缩图片,这里一定要得到imageview的宽高)必须压缩!,否则OOM!!!!!!!!!!
        int width = imageView.getWidth();

        if (width <= 0) {// 可能imageView刚new出来还没有添加到容器当中,width可能为0
            width = lp.width;// 获取imageView在layout中声明的宽度
        }
        if (width <= 0) {// 如果imageView设置的是WRAP_CONTENT=-1或者MATHC_PARENT=-2,得到的width还是0
            // TODO: 2016/3/19 此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取,已解决
            // width =
            // imageView.getMaxWidth();//检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
            width = getImageViewFieldValue(imageView, "mMaxWidth");// 检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
        }
        if (width <= 0) {// 如果还是得不到width,就设置为屏幕的宽度
            width = displayMetrics.widthPixels;
        }

        // ----------------------------------------

        // 得到imageView的实际高度(为了压缩图片,这里一定要得到imageview的宽高)必须压缩!,否则OOM!!!!!!!!!!
        int height = imageView.getHeight();

        if (height <= 0) {// 可能imageView刚new出来还没有添加到容器当中,width可能为0
            height = lp.height;// 获取imageView在layout中声明的高度
        }
        if (height <= 0) {// 如果imageView设置的是WRAP_CONTENT=-1或者MATHC_PARENT=-2,得到的width还是0
            // TODO: 2016/3/19 此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取,已解决
            // height =
            // imageView.getMaxHeight();//检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
            height = getImageViewFieldValue(imageView, "mMaxHeight");// 检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
        }
        if (height <= 0) {// 如果还是得不到width,就设置为屏幕的高度
            height = displayMetrics.heightPixels;
        }
        // --------------------------------尽可能得到ImageView的精确的宽高------------------------------------------------------------------

        imageSize.width = width;
        imageSize.height = height;
        return imageSize;
    }

    /**
     * 通过反射获取imageview的某个属性值(imageView.getMaxWidth()这个方法在api16以上才可以用,
     * 所以使用反射得到这个width属性值)
     * 
     * @param object
     * @param fieldName
     * @return
     */
    private static int getImageViewFieldValue(Object object, String fieldName) {

        int value = 0;

        try {
            Field field = ImageView.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            int fieldValue = field.getInt(object);
            if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
                value = fieldValue;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    /**
     * 根据图片要显示的宽和高,对图片进行压缩,避免OOM
     * 
     * @param path
     * @param width
     *            要显示的imageview的宽度
     * @param height
     *            要显示的imageview的高度
     * @return
     */
    private static Bitmap decodeSampledBitmapFromPath(String path, int width,
            int height) {

        // 获取图片的宽和高,并不把他加载到内存当中
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        options.inSampleSize = caculateInSampleSize(options, width, height);
        // 使用获取到的inSampleSize再次解析图片(此时options里已经含有压缩比
        // options.inSampleSize,再次解析会得到压缩后的图片,不会oom了 )
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        return bitmap;

    }

    /**
     * 根据需求的宽和高以及图片实际的宽和高计算SampleSize
     * 
     * @param options
     * @param reqWidth
     *            要显示的imageview的宽度
     * @param reqHeight
     *            要显示的imageview的高度
     * @return
     * @compressExpand 这个值是为了像预览图片这样的需求,他要比所要显示的imageview高宽要大一点,放大才能清晰
     */
    private static int caculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        int width = options.outWidth;
        int height = options.outHeight;

        int inSampleSize = 1;

        if (width >= reqWidth || height >= reqHeight) {

            int widthRadio = Math.round(width * 1.0f / reqWidth);
            int heightRadio = Math.round(width * 1.0f / reqHeight);

            inSampleSize = Math.max(widthRadio, heightRadio);

        }

        return inSampleSize;
    }

    /**
     * This method is used to get real path of file from from uri<br/>
     * http://stackoverflow.com/questions/11591825/how-to-get-image-path-just-
     * captured-from-camera 这个方法是可以使用的(网上搜了一堆好多没用的代码)
     * 
     * @param contentUri
     * @return String
     */
    public static String getRealPathFromURI(Activity activity, Uri contentUri) {
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            // Do not call Cursor.close() on a cursor obtained using this
            // method,
            // because the activity will do that for you at the appropriate time
            Cursor cursor = activity.managedQuery(contentUri, proj, null, null,
                    null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            return contentUri.getPath();
        }
    }

    /**
     * 存放获取imageview高宽的一个类
     */
    class ImageSize {
        public int width;
        public int height;
    }

    /**
     * Compress image by size, this will modify image width/height. Used to get
     * thumbnail
     * 
     * @param image
     * @param pixelW
     *            target pixel of width
     * @param pixelH
     *            target pixel of height
     * @return
     */
    public static Bitmap ratio(Bitmap image, float pixelW, float pixelH) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, os);
        if (os.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
            os.reset();// 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, 50, os);// 这里压缩50%,把压缩后的数据存放到baos中
        }
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 开始读入图片,此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        newOpts.inPreferredConfig = Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
        float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
        // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;// be=1表示不缩放
        if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;// 设置缩放比例
        // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        is = new ByteArrayInputStream(os.toByteArray());
        bitmap = BitmapFactory.decodeStream(is, null, newOpts);
        // 压缩好比例大小后再进行质量压缩
        // return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
        return bitmap;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值