工具類系類1

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

/**
 * 在指定畫布上將人臉框出來
 * 
 * @param canvas 給定的畫布
 * @param face 需要繪製的人臉信息
 * @param width 原圖寬
 * @param height 原圖高
 * @param frontCamera 是否爲前置攝像頭,如爲前置攝像頭需左右對稱
 * @param DrawOriRect 可繪製原始框,也可以只畫四個角
 */
static public void drawFaceRect(Canvas canvas, FaceRect face, int width, int height, boolean frontCamera, boolean DrawOriRect) {
    if(canvas == null) {
        return;
    }

    Paint paint = new Paint(); 
    paint.setColor(Color.rgb(255, 203, 15));
    int len = (face.bound.bottom - face.bound.top) / 8;
    if (len / 8 >= 2) paint.setStrokeWidth(len / 8);
    else paint.setStrokeWidth(2);

    Rect rect = face.bound;

    if(frontCamera) {
        int top = rect.top;
        rect.top = width - rect.bottom;
        rect.bottom = width - top;
    }

    if (DrawOriRect) {
        paint.setStyle(Style.STROKE);
        canvas.drawRect(rect, paint);
    } else {
        int drawl = rect.left   - len;
        int drawr = rect.right  + len;
        int drawu = rect.top    - len;
        int drawd = rect.bottom + len;

        canvas.drawLine(drawl,drawd,drawl,drawd-len, paint);
        canvas.drawLine(drawl,drawd,drawl+len,drawd, paint);
        canvas.drawLine(drawr,drawd,drawr,drawd-len, paint);
        canvas.drawLine(drawr,drawd,drawr-len,drawd, paint);
        canvas.drawLine(drawl,drawu,drawl,drawu+len, paint);
        canvas.drawLine(drawl,drawu,drawl+len,drawu, paint);
        canvas.drawLine(drawr,drawu,drawr,drawu+len, paint);
        canvas.drawLine(drawr,drawu,drawr-len,drawu, paint);
    }

    if (face.point != null) {
        for (Point p : face.point) 
        {
            if(frontCamera) {
                p.y = width - p.y;
            }
            canvas.drawPoint(p.x, p.y, paint);
        }
    }
}

/**
 * 將矩形隨原圖順時針旋轉90度
 * 
 * @param r
 * 待旋轉的矩形
 * 
 * @param width
 * 輸入矩形對應的原圖寬
 * 
 * @param height
 * 輸入矩形對應的原圖高
 * 
 * @return
 * 旋轉後的矩形
 */
static public Rect RotateDeg90(Rect r, int width, int height) {
    int left = r.left;
    r.left  = height- r.bottom;
    r.bottom= r.right;
    r.right = height- r.top;
    r.top   = left;
    return r;
}

/**
 * 將點隨原圖順時針旋轉90度
 * @param p
 * 待旋轉的點
 * 
 * @param width
 * 輸入點對應的原圖寬
 * 
 * @param height
 * 輸入點對應的原圖寬
 * 
 * @return
 * 旋轉後的點 
 */
static public Point RotateDeg90(Point p, int width, int height) {
    int x = p.x;
    p.x = height - p.y;
    p.y = x;
    return p;
}

public static int getNumCores() {
    class CpuFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            if(Pattern.matches("cpu[0-9]", pathname.getName())) {
                return true;
            }
            return false;
        }      
    }
    try {
        File dir = new File("/sys/devices/system/cpu/");
        File[] files = dir.listFiles(new CpuFilter());
        return files.length;
    } catch(Exception e) {
        e.printStackTrace();
        return 1;
    }
}

/**
 * 保存Bitmap至本地
 * @param 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();
    }
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章