Android 相机2之常用工具代码(预览方向、预览尺寸、全屏显示、分辨平板)

2.1设置相机展示方向

public static void setCameraDisplayOrientation(Activity activity,int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

2.2 得到合适的预览尺寸

private List getSupportedPictureSizes(Camera.Parameters parameters) {
    if (parameters == null) {
        return null;
    }
    if (mPictureSizeList == null) {
        mPictureSizeList = new ArrayList<>();
    } else {
        mPictureSizeList.clear();
    }
    mSupportedPicSizeList = new ArrayList<>();
    for (Camera.Size size : parameters.getSupportedPictureSizes()) {
        if(equalsRate(size, 1.777f)) {
            mSupportedPicSizeList.add(size);
        }
    }
    for (Camera.Size size : mSupportedPicSizeList) {
        mPictureSizeList.add(size.width + "×" + size.height);
    }
    return mSupportedPicSizeList;
}

2.3 设置全屏显示

private void setFullScreen() {
    int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
    getWindow().setFlags(flag,flag);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

2.4判断是平板OR手机

public static boolean isTablet(Context context) {

return (context.getResources().getConfiguration().screenLayout

 & Configuration.SCREENLAYOUT_SIZE_MASK)

>= Configuration.SCREENLAYOUT_SIZE_LARGE;

}

你可能感兴趣的:(Android相机)