public class ImageUtil {
/**
* 给图片加边框等
* bitmap 源图片
* radius The radius of the blur Supported range 0 25){
* radius = 25.0f;
* }else if (radius
* srcBitmap 原图片
* borderWidth 边框宽度
* color 边框的颜色值
*/
public static Bitmap addFrameBitmap(Bitmap srcBitmap, int borderWidth, int color) {
if (srcBitmap == null) {
// Log.e(TAG, "the srcBitmap or borderBitmap is null");
return null;
}
int newWidth = srcBitmap.getWidth() + borderWidth;
int newHeight = srcBitmap.getHeight() + borderWidth;
Bitmap outBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outBitmap);
Rect rec = canvas.getClipBounds();
rec.bottom--;
rec.right--;
Paint paint = new Paint();
//设置边框颜色
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
//设置边框宽度
paint.setStrokeWidth(borderWidth);
canvas.drawRect(rec, paint);
canvas.drawBitmap(srcBitmap, borderWidth / 2f, borderWidth / 2f, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return outBitmap;
}
/**
* 图片按长宽压缩
*/
public static Bitmap compressImageOptimizedArea(Bitmap bitmap, Integer maxH, Integer maxW) {
// 获取这个图片的宽和高
float width = bitmap.getWidth();
float height = bitmap.getHeight();
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 计算宽高缩放率
float scaleWidth = ((float) maxW) / width;
float scaleHeight = ((float) maxH) / height;
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbitmap = Bitmap.createBitmap(bitmap, 0, 0, (int) width,
(int) height, matrix, true);
return newbitmap;
}
public static int getImageSize(Bitmap src) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19
return src.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
return src.getByteCount();
}
return src.getRowBytes() * src.getHeight();
}
public static String getRealPathFromURI(Uri contentUri, Context context) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
}
return res;
}
//旋转
public static Bitmap adjustPhotoRotation(Bitmap bm, final float orientationDegree) {
Bitmap bm1 = null;
if (bm.getHeight() < bm.getWidth()) {
Matrix m = new Matrix();
m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
m.postTranslate((bm.getHeight() - bm.getWidth()) / 2, -(bm.getHeight() - bm.getWidth()) / 2);//原点对齐
bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
Canvas canvas = new Canvas(bm1);
canvas.drawBitmap(bm, m, paint);
} else {
return bm;
}
return bm1;
}
/**
* 裁剪图片
*
* @param uri 图片的Uri
*/
public static Intent cropPicture(Uri uri) {
Intent innerIntent = new Intent("com.android.camera.action.CROP");
innerIntent.setDataAndType(uri, "p_w_picpath/*");
innerIntent.putExtra("crop", "true");// 才能出剪辑的小方框,不然没有剪辑功能,只能选取图片
innerIntent.putExtra("aspectX", 1); // 放大缩小比例的X
innerIntent.putExtra("aspectY", 1);// 放大缩小比例的X 这里的比例为: 1:1
innerIntent.putExtra("outputX", 320); //这个是限制输出图片大小
innerIntent.putExtra("outputY", 320);
innerIntent.putExtra("return-data", true);
innerIntent.putExtra("scale", true);
return innerIntent;
}
//压缩并保存
public static File saveBitmap(Bitmap bitmap, String dumpFileName, int degrees) {
File file = new File(getSaveDir(), dumpFileName);
FileOutputStream fos = null;
// 打开文件输出流
try {
//大于800K的图片压缩
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 100;//从100开始
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
//遍历压缩
while (baos.toByteArray().length / 1024 > 800) {
baos.reset();
options -= 10;
if (options > 0) {
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
}
//写入文件
fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
//把图片的旋转角信息写入新文件
setPictureDegree(file.getPath(), degrees);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bitmap) {
bitmap.recycle();
bitmap = null;
System.gc();
}
}
return file;
}
/**
* 得到图片的旋转角度
*
* @param path
* @return
*/
public static int readPictureDegree(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;
}
} catch (IOException e) {
e.printStackTrace();
return degree;
}
return degree;
}
/**
* 将图片的旋转角度置为0
*
* @param path
* @return void
* @Title: setPictureDegree
* @date 2012-12-10 上午10:54:46
*/
private static void setPictureDegree(String path, int degree) {
try {
ExifInterface exifInterface = new ExifInterface(path);
//修正图片的旋转角度,设置其不旋转。这里也可以设置其旋转的角度,可以传值过去,
//例如旋转90度,传值ExifInterface.ORIENTATION_ROTATE_90,需要将这个值转换为String类型的
String orientation_value = "1";//正常
switch (degree) {
case 90:
orientation_value = "6";
break;
case 180:
orientation_value = "3";
break;
case 270:
orientation_value = "8";
break;
default:
break;
}
exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, orientation_value);
exifInterface.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
}
}
转载于:https://blog.51cto.com/7183397/1915686