图片文件操作工具类---ImageUtil

来源:互联网 发布:1hhhh域名升级访问中 编辑:程序博客网 时间:2024/06/05 19:27

顶赞:
Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传 - 享受技术带来的快乐 - CSDN博客 http://blog.csdn.net/jdsjlzx/article/details/44228935方法简直太全了,强烈推荐。

学习网址:
1.Glide加载图片并保存到本地相册 - Franco的博客 - CSDN博客 http://blog.csdn.net/w372426096/article/details/52472984
2.android 图片压缩,base64转换上传 - 简书 http://www.jianshu.com/p/d4c013fdaa87
3.android bitmap和base64之间的转换 - Pig - CSDN博客 http://blog.csdn.net/chunlongyuan/article/details/7696070
4.【Android内存优化】Android内存优化之Bitmap最优加载 - 程序员互动联盟 http://www.coderonline.net/android-memory-optimization-android-bitmap-of-memory-optimization-optimal-loading.html
5.xutils按照图片的比例压缩接口 - zhiqiang_zhao - CSDN博客 http://m.blog.csdn.net/weixin_38501485/article/details/74529799

实现的功能

Base64字符串转为bitmap;
根据图片绝对路径将其转换成Base64字符串;
把bitmap转换成Base64字符串;
把bitmap压缩后转换成Base64字符串;
根据路径获得图片并压缩,返回bitmap用于显示;
计算图片的缩放值(比例压缩);
保存图片到本地指定路径;
比例压缩图片;
质量压缩图片;

补充:
文件相关操作,比如删除、重命名、查找、拷贝等等操作,详见文件操作工具类—FileUtil,这些操作依然对图片文件操作有效滴,毕竟都是文件嘛,还是有共同点滴。

具体代码

import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Base64;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class ImageUtil {    /**     * Base64转字符串为bitmap     * @param base64Data     * @return     */    public static Bitmap base64ToBitmap(String base64Data) {        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);    }    /**     * 根据图片绝对路径将其转换成Base64字符串     * @param filePath 图片绝对路径     * @return     */    public static String bitmapToBase64(String filePath) {        String result = null;        //获得原始图片        Bitmap bm = BitmapFactory.decodeFile(filePath);        ByteArrayOutputStream baos = new ByteArrayOutputStream();        //1.5M的压缩后在100Kb以内,测试得值,压缩后的大小=94486字节,压缩后的大小=74473字节        //这里的JPEG 如果换成PNG,那么压缩的就有600kB这样        //quality(0~100)为40        bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);        byte[] b = baos.toByteArray();        result = Base64.encodeToString(b, Base64.DEFAULT);        try{            baos.flush();            baos.close();        }catch (IOException e){            e.printStackTrace();        }try {            if (baos != null) {                baos.flush();                baos.close();            }        } catch (IOException e) {            e.printStackTrace();        }        return result;    }    /**     * 把bitmap转换成Base64字符串     * @param bitmap     * @return     */    public static String bitmapToBase64(Bitmap bitmap) {        String result = null;        ByteArrayOutputStream baos = null;        try {            if (bitmap != null) {                baos = new ByteArrayOutputStream();                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);                byte[] bitmapBytes = baos.toByteArray();                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);                baos.flush();                baos.close();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (baos != null) {                    baos.flush();                    baos.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return result;    }    /**     * 把bitmap压缩后转换成Base64字符串     * @param filePath 图片绝对路径     * @return     */    public static String bitmapDecodeToString(String filePath) {        //压缩图片        Bitmap bm = getSmallBitmap(filePath);        ByteArrayOutputStream baos = new ByteArrayOutputStream();        //1.5M的压缩后在100Kb以内,测试得值,压缩后的大小=94486字节,压缩后的大小=74473字节        //这里的JPEG 如果换成PNG,那么压缩的就有600kB这样        bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);        byte[] b = baos.toByteArray();        return Base64.encodeToString(b, Base64.DEFAULT);    }    /**     * 根据路径获得图片并压缩,返回bitmap用于显示     * @param filePath     * @return     */    public static Bitmap getSmallBitmap(String filePath) {        final BitmapFactory.Options options = new BitmapFactory.Options();        // 第一次解析时,inJustDecodeBounds设置为true,        // 禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小        options.inJustDecodeBounds = true;        BitmapFactory.decodeFile(filePath, options);        //设置图片的缩放值        options.inSampleSize = calculateInSampleSize(options, 480, 800);        // 使用计算得到的inSampleSize值再次解析图片        options.inJustDecodeBounds = false;        return BitmapFactory.decodeFile(filePath, options);    }    /**     * 计算图片的缩放值(比例压缩)     * @param options     * @param reqWidth 期望压缩的宽度     * @param reqHeight 期望压缩的高度     * @return     */    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;    }    /**     * 保存图片到本地指定路径     * @param fileDirPath 保存的文件夹绝对路径     * @param fileName 图片名     * @return     */    public static boolean saveImageFile(Bitmap mBitmap,String fileDirPath, String fileName){//        Bitmap mBitmap = BitmapFactory.decodeFile(fileDirPath + "/" + fileName);        File currentFile = new File(fileDirPath,fileName);        FileOutputStream fos = null;        try {            fos = new FileOutputStream(currentFile);            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();        } catch (FileNotFoundException e) {            e.printStackTrace();            return false;        } catch (IOException e) {            e.printStackTrace();            return false;        } finally {            try {                if (fos != null) {                    fos.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return true;    }    /**     * 比例压缩图片     *     * @param image  需要进行压缩的Bitmap对象     * @return     */    public static Bitmap proportionCompressImage(Bitmap image) {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);        //判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出        if (baos.toByteArray().length / 1024 > 1024) {            //重置baos即清空baos            baos.reset();            //这里压缩50%,把压缩后的数据存放到baos中            image.compress(Bitmap.CompressFormat.JPEG, 50, baos);        }        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());        BitmapFactory.Options newOpts = new BitmapFactory.Options();        // 开始读入图片,此时把options.inJustDecodeBounds 设回true了        newOpts.inJustDecodeBounds = true;        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);        newOpts.inJustDecodeBounds = false;        int w = newOpts.outWidth;        int h = newOpts.outHeight;        // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为        float hh = 800f;//这里设置高度为800f        float ww = 480f;//这里设置宽度为480f        // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可        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;//设置缩放比例        newOpts.inPreferredConfig = Bitmap.Config.RGB_565;//降低图片从ARGB888到RGB565        //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了        isBm = new ByteArrayInputStream(baos.toByteArray());        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);        return bitmap;    }    /**     * 质量压缩图片     *     * @param image 需要进行压缩的Bitmap对象     * @return     */    public static Bitmap qualityCompressImage(Bitmap image) {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        //质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);        int options = 100;        //循环判断如果压缩后图片是否大于100kb,大于继续压缩        while (baos.toByteArray().length / 1024 > 100) {            //重置baos即清空baos            baos.reset();            //每次都减少10            options -= 10;            //这里压缩options%,把压缩后的数据存放到baos中            image.compress(Bitmap.CompressFormat.JPEG, options, baos);        }        //把压缩后的数据baos存放到ByteArrayInputStream中        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());        //把ByteArrayInputStream数据生成图片        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);        return bitmap;    }}
原创粉丝点击