android 图片压缩,自动旋转保存,查看照片信息,拍照设备类型等

	/**
	 * 压缩图片 如果图片宽高有一个大于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;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值