public class ImageUtil {

public static final String TAG = ImageUtil.class.getSimpleName();

/**
* 判斷圖片是否需要翻轉(正方向的不用翻轉)
*
* @param fileName
* @return
*/
public static boolean needRotate(CoNtext coNtext, String fileName) {
try {
ExifInterface exif = new ExifInterface(fileName);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
LogUtil.d(TAG, "orientation is " + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return true;
case ExifInterface.ORIENTATION_ROTATE_270:
return true;
case ExifInterface.ORIENTATION_ROTATE_180:
return true;
}

return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

/**
* 獲取翻轉到正方向的的圖片
*
* @param fileName
* @param maxWidth
* @param maxHeight
* @return
*/
public static void rotatedBitmap(CoNtext coNtext, String fileName,
String dstFile) {
try {
ExifInterface exif = new ExifInterface(fileName);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
LogUtil.d(TAG, "orientation is " + orientation);
Matrix matrix = null;

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix = new Matrix();
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix = new Matrix();
matrix.postRotate(270);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix = new Matrix();
matrix.postRotate(180);
break;
}

Bitmap bmp = getSmallBitmap(coNtext,
Uri.fromFile(new File(fileName)));
if (matrix != null) {
Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, true);
bmp.recycle();

FileOutputStream fos = new FileOutputStream(dstFile);
bmp2.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
bmp2.recycle();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static boolean compress(CoNtext coNtext, Uri src, File dst, int rate) {
try {
Bitmap bmp = getSmallBitmap(coNtext, src);
FileOutputStream fos = new FileOutputStream(dst);
bmp.compress(Bitmap.CompressFormat.JPEG, rate, fos);
bmp.recycle();
bmp = null;
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {

final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;

}

public static Bitmap getSmallBitmap(CoNtext coNtext, Uri uri) {
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is = coNtext.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(is, null, options);
is.close();

Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 720, 1280);

Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;

is = coNtext.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
is.close();
return bmp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()