package com.hand.face.utils; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Matrix; import android.media.ExifInterface; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FaceUtil { public final static int REQUEST_PICTURE_CHOOSE = 1; public final static int REQUEST_CAMERA_IMAGE = 2; public final static int REQUEST_CROP_IMAGE = 3; /*** * 裁剪图片 * @param activity Activity * @param uri 图片的Uri */ public static void cropPicture(Activity activity, Uri uri) { Intent innerIntent = new Intent("com.android.camera.action.CROP"); innerIntent.setDataAndType(uri, "image/*"); 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); innerIntent.putExtra("scaleUpIfNeeded", true); File imageFile = new File(getImagePath(activity.getApplicationContext())); innerIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); innerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); activity.startActivityForResult(innerIntent, REQUEST_CROP_IMAGE); } /** * 保存裁剪的图片的路径 * @return */ public static String getImagePath(Context context){ String path; if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { path = context.getFilesDir().getAbsolutePath(); } else { path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/msc/"; } if(!path.endsWith("/")) { path += "/"; } File folder = new File(path); if (folder != null && !folder.exists()) { folder.mkdirs(); } path += "ifd.jpg"; return path; } /** * 读取图片属性:旋转的角度 * * @param path 图片绝对路径 * @return degree 旋转角度 */ 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; } /** * 旋转图片 * * @param angle 旋转角度 * @param bitmap 原图 * @return bitmap 旋转后的图片 */ public static Bitmap rotateImage(int angle, Bitmap bitmap) { // 图片旋转矩阵 Matrix matrix = new Matrix(); matrix.postRotate(angle); // 得到旋转后的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizedBitmap; } /** * 保存Bitmap至本地 */ public static void saveBitmapToFile(Context context,Bitmap bmp){ String file_path = getImagePath(context); File file = new File(file_path); FileOutputStream fOut; try { fOut = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut); fOut.flush(); fOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }