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(); } } }