在手机应用中处理图片是经常的事情,有很多操作都是会反复使用的,例如图片压缩、裁剪...... 索性就直接写一个封装类,方便日后调用。
一个图片工具类,包括以下常用操作:
图片获取(从 Uri 获取、从绝对路径获取)
图片压缩
图片裁剪
获取图片角度
图片旋转
Bitmap 转 Drawable、Drawable 转 Bitmap
通过 Uri 得到绝对路径
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.File;
import java.io.IOException;
/**
* 一个图片工具类
* 包括:图片获取(从 Uri 获取、从绝对路径获取)
* 图片压缩
* 图片裁剪
* 获取图片角度
* 图片旋转
* Bitmap 转 Drawable、Drawable 转 Bitmap
* 通过 Uri 得到绝对路径
* */
public class ImageUtil {
/**
* 通过 Uri 获取图片
* */
public static Bitmap getBitmapFromUri(Uri uri, Context context) {
if (uri == null || context == null) {
return null;
}
try {
return MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 通过图片的绝对路径获取图片
* */
public static Bitmap getBitmapFromAbsolutePath(String path){
File file = new File(path);
if (file.exists()){
return BitmapFactory.decodeFile(path);
}
return null;
}
/**
* 将图片压缩到指定宽高(单位 px)
* 这个函数和下面的函数搭配使用的
**/
public static Bitmap compressBitmap(String filePath, int width, int height) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
/**
* 计算inSampleSize值
*
* @param options
* 用于获取原图的长宽
* @param reqWidth
* 要求压缩后的图片宽度
* @param reqHeight
* 要求压缩后的图片长度
* @return
* 返回计算后的inSampleSize值
*/
//计算图片的缩放值
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;
}
/**
* 裁剪bitmap
*
* @param bitmap 传入的bitmap
* @param rect 需要被裁剪的区域
* @return 被裁剪后的bitmap
*/
public static Bitmap imageCrop(Bitmap bitmap, Rect rect) {
if (bitmap == null || rect == null || rect.isEmpty() || bitmap.getWidth() < rect.right || bitmap.getHeight() < rect.bottom) {
return null;
}
return Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), null, false);
}
/**
* 获取图片的角度
* */
public static int getImageDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
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;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
degree = 0;
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
degree = 180;
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
degree = 90;
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 图片旋转
* */
public static Bitmap getRotateBitmap(Bitmap b, float rotateDegree) {
if (b == null) {
return null;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotateDegree);
return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, false);
}
/**
* 将已有的 drawable 资源转换为 bitmap 对象
*/
public static Bitmap drawableToBitamp(Drawable drawable)
{
BitmapDrawable bd = (BitmapDrawable) drawable;
Bitmap bitmap = bd.getBitmap();
return bitmap;
}
/**
* Bitmap 转化为 drawable
*/
public static Drawable bitmapToDrawable(Bitmap bitmap) {
return new BitmapDrawable(bitmap);
}
/**
* 通过给定的 Uri 获取文件在文件系统中的绝对路径
* */
public static String getAbsolutePathByUri(final Context context, final Uri uri ) {
if ( null == uri ) return null;
final String scheme = uri.getScheme();
String data = null;
if ( scheme == null )
data = uri.getPath();
else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
data = uri.getPath();
} else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
if ( null != cursor ) {
if ( cursor.moveToFirst() ) {
int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
if ( index > -1 ) {
data = cursor.getString( index );
}
}
cursor.close();
}
}
return data;
}
}