上传图片压缩工具类
来源:互联网 发布: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
- 上传图片压缩工具类
- 上传图片压缩工具类
- java上传图片压缩工具类
- 图片上传服务器压缩工具类
- Android—图片上传压缩工具类
- android 图片压缩工具类
- 多功能图片压缩工具类
- 多功能图片压缩工具 类
- Android图片压缩工具类
- java 图片压缩工具类
- Android图片压缩工具类
- 图片压缩工具类
- Android图片压缩工具类
- android图片压缩工具类
- 图片的压缩工具类
- 图片压缩工具类
- android 图片压缩工具类
- 图片压缩工具类
- CentOS 安装svn服务And eclipse安装svn
- 2016苹果开发者账号注册申请流程链接
- 函数 INIT_WORK() / schedule_work()
- 机房收费系统——报表的使用
- 重启/关闭Windows
- 上传图片压缩工具类
- Redis搭建(二)
- java.lang.LinkageError: loader constraint violation
- 活动Activity的栈的存储方式
- GCov交叉编译覆盖率测试要点
- SSL,SSH,OpenSSL,OpenSSH,HTTPS的区别
- OLTP和OLAP浅析
- (一)VMware安装liunx虚拟机centos7
- HDU 3220 Alice’s Cube:题目解答源码