上传图片压缩工具类

来源:互联网 发布:mac系统测试软件 编辑:程序博客网 时间:2024/06/05 09:47

1、校验图片角度,有旋转进行修改;然后校验图片大小,大于标清(720*1280)进行压缩;调用下面该方法传入需要压缩的图片文件,返回压缩后的图片文件;方法调用需要运行在子线程中进行

public File getModificationFile(File file){file=reviewPicRotate(file);if(verifyPictureSize(file.getPath())){//标清不压缩return file;}else{//压缩File compressionFile = saveBitmapToLocal(getBitmapFromFile(file.getPath()));if(compressionFile == null)return file;return compressionFile;}}

2、获取图片文件的信息,判断是否旋转矫正

public File reviewPicRotate(File file){//读取图片文件旋转的角度int degree  = 0;try {ExifInterface exifInterface = new ExifInterface(file.getPath());int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}if(degree != 0){//图片旋转了;进行图片旋转Bitmap bitmap = getBitmapFromFile(file.getPath());Matrix m = new Matrix();  int width = bitmap.getWidth();  int height = bitmap.getHeight();  m.setRotate(degree); // 旋转angle度  bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,m, true);return saveBitmapToLocal(bitmap);// 从新生成图片文件}return file;}

3、判断图片如果小于720 * 1280直接返回原图片文件,否则压缩

public static boolean verifyPictureSize(String path) {        BitmapFactory.Options opts = new BitmapFactory.Options();        opts.inJustDecodeBounds = true;        BitmapFactory.decodeFile(path, opts);        if (opts.outHeight <= 1280 && opts.outWidth <= 720)            return true;        else return false;    }

4、将文件转换为720*1280的bitmap

public static Bitmap getBitmapFromFile(String path) { int width = 720;     int height = 1280;     BitmapFactory.Options opts = null;     if(path != null) {          if (width > 0 && height > 0) {              opts = new BitmapFactory.Options();              opts.inJustDecodeBounds = true;              BitmapFactory.decodeFile(path, opts);              final int minSideLength = Math.min(width, height);              opts.inSampleSize = computeSampleSize(opts, minSideLength,width * height);              opts.inJustDecodeBounds = false;          }          return BitmapFactory.decodeFile(path, opts);      } else return null;  }

5、计算压缩比率

public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {        int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);        int roundedSize;        if (initialSize <= 8) {            roundedSize = 1;            while (roundedSize < initialSize) {                roundedSize <<= 1;            }        } else {            roundedSize = (initialSize + 7) / 8 * 8;        }        return roundedSize;    }

private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {        double w = options.outWidth;        double h = options.outHeight;        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));        if (upperBound < lowerBound) {            // return the larger one when there is no overlapping zone.            return lowerBound;        }        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {            return 1;        } else if (minSideLength == -1) {            return lowerBound;        } else {            return upperBound;        }    }

6、将转换后的bitmap对象保存为file文件并存储到本地

public File saveBitmapToLocal(Bitmap bitmap) {        if (null == bitmap) {            return null;        }        FileOutputStream fileOutput = null;        File imgFile = null;        try {            imgFile = SDUtils.getCreatFile(context,SDUtils.imgCachePicUrl, System.currentTimeMillis()+".jpg");//新创建的图片文件对象            fileOutput = new FileOutputStream(imgFile);            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutput);            fileOutput.flush();        } catch (FileNotFoundException e) {            e.printStackTrace();            imgFile = null;        } catch (IOException e) {            e.printStackTrace();            imgFile = null;        } finally {            if (null != fileOutput) {                try {                    fileOutput.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return imgFile;    }




0 0
原创粉丝点击