/** * 判断原始素材的方向 (适用于相机拍摄的照片) * * @param path 图片的地址 * @return angle->角度 */ @Override public int isRotatedImage(String path) { int angle = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientationPhoto = exifInterface.getAttributeInt("Orientation", ExifInterface.ORIENTATION_NORMAL); switch (orientationPhoto) { case ExifInterface.ORIENTATION_ROTATE_90: angle = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: angle = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: angle = 270; break; default: angle = 0; break; } } catch (IOException e) { Utils.debug(e.toString()); } return angle; }
/** * 保存Bitmap到文件 * * @param file 需要保存到的文件File * @param bitmap 需要保存的源Bitmap * @param quality 图片压缩质量,使用Constant中的IMAGE_QUALITY * @param format 压缩格式 * @return 是否压缩成功 */ private boolean saveBitmap(File file, Bitmap bitmap, int quality, CompressFormat format) { if (sdCard.isSdcardAvailable()) { try { FileOutputStream out = new FileOutputStream(file); if (bitmap.compress(format, quality, out)) { out.flush(); out.close(); return true; } } catch (IOException e) { Utils.debug(e.toString()); } } else { Utils.debug(context.getString(R.string.error_can_not_find_sdcard)); } return false; }
/** * 获取重新计算大小的Bitmap 最大边不超过max * * @param path 图片路径 * @param max 最大边长度 * @return 新Bitmap */ @Override public Bitmap resizeBitmap(String path, int max) { int sample = 1; BitmapFactory.Options opts = new Options(); opts.inJustDecodeBounds = true; opts.inSampleSize = sample; BitmapFactory.decodeFile(path, opts); int w = opts.outWidth; int h = opts.outHeight; if (Math.max(w, h) > max * 4) { sample = 8; } else if (Math.max(w, h) > max * 2) { sample = 4; } else if (Math.max(w, h) > max) { sample = 2; } opts.inPreferredConfig = Config.ARGB_8888; opts.inJustDecodeBounds = false; opts.inSampleSize = sample; try { return (sample == 1) ? BitmapFactory.decodeFile(path, opts) : getScaledBitmap(BitmapFactory.decodeFile(path, opts), max); } catch (OutOfMemoryError e) { Utils.debug(e.toString()); opts.inSampleSize = sample * 2; try { return getScaledBitmap(BitmapFactory.decodeFile(path, opts), max); } catch (OutOfMemoryError e1) { try { Utils.debug(e1.toString()); opts.inSampleSize = sample * 2; return getScaledBitmap(BitmapFactory.decodeFile(path, opts), max); } catch (Exception e2) { Utils.debug(e2.toString()); return null; } } } }
/** * 获取Bitmap数据 * * @param source Bitmap数据源 * @return Bitmap的字节数据 */ @Override public byte[] getBitmapData(Bitmap source) { if (source == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); source.compress(CompressFormat.JPEG, 80, baos); byte[] result = baos.toByteArray(); if (null != baos) { try { baos.close(); } catch (IOException e) { Utils.debug(e.toString()); } } return result; }
/** * 旋转Bitmap * * @param bitmap 需要旋转的Bitmap * @param angle 旋转的角度,正数将顺时针旋转图像,负数将逆时针旋转图像 * @return 旋转后的Bitmap */ @Override public Bitmap rotateBitmap(Bitmap bitmap, int angle) { Matrix m = new Matrix(); m.setRotate(angle); int width = bitmap.getWidth(); int height = bitmap.getHeight(); try { bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true); } catch (OutOfMemoryError oom) { try { m.postScale(1.0f, 1.0f); bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true); } catch (Exception e) { Utils.debug(e.toString()); } } return bitmap; }