package com.shaodianbao.util;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.media.ExifInterface;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;/** * Created by Administrator on 2017/4/7 0007. * 对图片的处理 */public class ImageUtil {    /**     * 图片的压缩(尺寸压缩和质量压缩)     *     * @param srcPath   源文件的路径     * @param desPath   压缩后的保存路径     * @param maxWidth  最大的高度     * @param maxHeight 最大的宽度     * @param quality   不压缩百分比(quality表示不压缩的量 100表示不压缩)     * @throws IOException     */    public static void compressPicture(String srcPath, String desPath, int maxWidth, int maxHeight, int quality) {        try {            BitmapFactory.Options opts = new BitmapFactory.Options();            //开始读入图片,此时把options.inJustDecodeBounds 设回true            opts.inJustDecodeBounds = true;            BitmapFactory.decodeFile(srcPath, opts);            int w = opts.outWidth;            int h = opts.outHeight;            int inSampleSize = 1;            if (w > h && w > maxWidth) {                inSampleSize = w / maxWidth;            } else if (w < h && h > maxHeight) {                inSampleSize = h / maxHeight;            }            opts.inSampleSize = inSampleSize;            opts.inJustDecodeBounds = false;            Bitmap bm = BitmapFactory.decodeFile(srcPath, opts);            OutputStream out = new FileOutputStream(desPath);            bm.compress(Bitmap.CompressFormat.JPEG, quality, out);            out.close();            bm.recycle();        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 缩放图片     *     * @param srcPath   原路径     * @param desPath   缩放后的保存路径     * @param newWidth  缩放后的宽度     * @param newHeight 缩放后的高度     */    public static void zoomImage(String srcPath, String desPath, int newWidth, int newHeight) {        try {            Bitmap bitmap = BitmapFactory.decodeFile(srcPath);            //获取这个图片的宽和高            float width = bitmap.getWidth();            float height = bitmap.getHeight();            //计算宽高缩放率            float scaleWidth = newWidth / width;            float scaleHeight = newHeight / height;            //创建操作图片用的matrix            Matrix matrix = new Matrix();            //缩放图片动作            matrix.postScale(scaleWidth, scaleHeight);            Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) height, matrix, true);            FileOutputStream out = new FileOutputStream(desPath);            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }}

更多相关文章

  1. Android三种方法设置ImageView的图片
  2. Gradle离线配置、.android、.AndroidStudio、.gradle、.m2缓存文
  3. Android WebView获取上一个链接的路径
  4. Android 如何加载大图片
  5. Android 图片加载库Glide
  6. android 自定义图片剪裁
  7. Android——ImageButton【图片按钮】的点击事件与属性
  8. android 选择本地图片并预览

随机推荐

  1. TransactionTooLargeExceptions on Andro
  2. Android(安卓)中文API (46) —— SimpleAda
  3. Android(安卓)积累一些常见的开发异常与
  4. Android(安卓)studio 百度地图开发(3)地图
  5. Android(安卓)Activity 之横竖屏的生命周
  6. Android学习笔记10:TextView的使用
  7. Android(安卓)学习笔记之网络通信基础+We
  8. Android的Fragment中onActivityResult不
  9. Android(安卓)控件之DatePicker,TimePick
  10. 构建自定义组件