android 图片压缩,自动旋转保存,查看照片信息,拍照设备类型等
2013-11-26 15:20
681 查看
/** * 压缩图片 如果图片宽高有一个大于2000,并且压缩比例小于等于2,则调为4 如果图片宽高小于1000*1000则不压缩 * * @param orgImagePath * 图片原路径 * @param desertImagePath * 压缩图片保存路径 * @return true,压缩成功;false,压缩失败。 */ public static boolean compressImageFile(String orgImagePath, String desertImagePath) { CompressFormat format = Bitmap.CompressFormat.JPEG; OutputStream stream = null; Bitmap bitmap = null; boolean b = false; int scale=1; try { int quality = 100; File file = new File(orgImagePath); long fileSize = file.length(); if (200 * 1024 < fileSize && fileSize <= 1024 * 1024) { quality = 85; } else if (1024 * 1024 < fileSize) { quality = 80; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(orgImagePath, o); // 如果图片宽高大于2000,并且压缩比例小于等于2,则调为8 int width_tmp = o.outWidth, height_tmp = o.outHeight; if ((width_tmp > 2000 || height_tmp > 2000) && scale <= 2) { scale = 8; } else if (width_tmp < 500 && height_tmp < 500) { // 如果图片小于500*500则不压缩 scale = 1; } else if (width_tmp < 1000 && height_tmp < 1000) { // 如果图片小于1000*1000则调为2 scale = 2; } else { // 如果图片大于1000*1000小于2000+则调为4 scale = 4; } if (scale == 1 && quality == 100) { File file2 = new File(desertImagePath); boolean b2 = copyfile(file, file2); Log.i("xpf","desertImagePath=" + desertImagePath); Log.i("xpf","不用压缩" + b2 + "原图大小=" + file.length() / 1024 + "KB"); return b2; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = scale; options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; bitmap = BitmapFactory.decodeFile(orgImagePath, options); // 旋转图片 int degrees = readPictureDegree(orgImagePath); if (degrees != 0 && bitmap != null) { Log.i("xpf","图片旋转=" + degrees); Matrix m = new Matrix(); m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); } stream = new FileOutputStream(desertImagePath); if (bitmap != null) { b = bitmap.compress(format, quality, stream); File file2 = new File(desertImagePath); Log.i("xpf","desertImagePath=" + desertImagePath); Log.i("xpf","scale=" + scale + " 图片质量=" + quality + " 原图为" + width_tmp + "*" + height_tmp + " 压缩后为" + options.outWidth + "*" + options.outHeight); Log.i("xpf","原图大小=" + file.length() / 1024 + "KB" + " 压缩后图片=" + file2.length() / 1024 + "kb" + " 实际压缩率为:" + file2.length() * 100 / file.length() + "% 标准压缩率为:" + 100 / (scale * scale) + "%"); if(file2.length()>file.length()*2/3){ boolean b2 = copyfile(file, file2); Log.i("xpf", "压缩后大于原图*2/3,图片比较小才会有这种情况,使用原图" + b2 + "原图大小=" + file.length() / 1024 + "KB"); return b2; } } bitmap.recycle(); bitmap = null; System.gc(); if (stream != null) { stream.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return b; } /** * 复制文件 * * @param fromFile * 源文件路径 * @param toFile * 目标文件路径,如果存在,先删除再复制 * @return true 复制成功;false 复制失败 */ public static boolean copyfile(File fromFile, File toFile) { if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) { return false; } try { if (!toFile.getParentFile().exists()) { toFile.getParentFile().mkdirs(); } toFile.delete(); java.io.FileInputStream fisfrom = new java.io.FileInputStream(fromFile); java.io.FileOutputStream fosto = new FileOutputStream(toFile); byte bt[] = new byte[1024]; int c; while ((c = fisfrom.read(bt)) > 0) { fosto.write(bt, 0, c); // 将内容写到新文件当中 } if (fisfrom != null) { fisfrom.close(); } fosto.flush(); if (fosto != null) { fosto.close(); } return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; } /** * 获取图片的旋转角度 * * @Title: readPictureDegree * @param path * @return int * @date 2013-11-27 上午9:22:33 */ private static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); // 得到照片的信息,ExifInterface.TAG_MAKE拍照设备型号,ExifInterface.TAG_MODEL拍照设备品牌,ExifInterface.TAG_ORIENTATION照片旋转角度 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; default: degree = 0; } } catch (IOException e) { e.printStackTrace(); } return degree; }
相关文章推荐
- Android ImageUtils 图片工具类,包括:小米从相册调图像空指针,三星旋转照片,压缩图片,保存Bitmap到本地,获取本地视频的缩略图,毛化玻璃效果,代码以后会更新
- TakePhoto是一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库,目前最新版本3.0.0。
- 一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库
- Android设备上获取照片、裁剪图片、压缩图片
- Android学习之调用系统照相机拍照并保存(照片会被压缩)
- 图片压缩保存 处理三星拍照图片旋转问题的部分方法
- android选择图片拍照详解(裁剪-旋转-压缩)
- android 拍照图片自动旋转了?
- 在 Android 设备上获取照片、裁剪图片、压缩图片 @TakePhoto V3.0 发布
- android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
- Android拍照并保存(对图片进行压缩,分解)
- Android部分手机拍照上传返回为空处理,和拍照后图片压缩后保存到指定路径下
- Android调用相机拍照,压缩图片后保存SD卡中
- Android中相机拍摄照片,以及相册选择图片压缩上传(压缩后保存进SD中)(可用于修改头像等)
- Android拍照并保存(对图片进行压缩,分解)
- Android拍照按路径保存图片以及图片压缩不失真
- Android开发之获取相册照片和获取拍照照片三之 图片旋转
- android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
- android没有插SD卡/TF卡(TF卡其实就是microSD卡)时的图库(拍照的照片)里图片的保存路径(位置)
- Android拍照压缩裁剪并保存图片