/**

* @param ftRect FT人脸框

* @param previewWidth 相机预览的宽度

* @param previewHeight 相机预览高度

* @param canvasWidth 画布的宽度

* @param canvasHeight 画布的高度

* @param cameraOri 相机预览方向

* @param mCameraId 相机ID

* @return

*/

public static Rect adjustRect(Rect ftRect, int previewWidth, int previewHeight, int canvasWidth, int canvasHeight, int cameraOri, int mCameraId,boolean isMirror) {

if (ftRect == null) {

return null;

}

Rect rect = new Rect(ftRect);

if (canvasWidth < canvasHeight) {

int t = previewHeight;

previewHeight = previewWidth;

previewWidth = t;

}

float horizontalRatio;

float verticalRatio;

if (cameraOri == 0 || cameraOri == 180) {

horizontalRatio = (float) canvasWidth / (float) previewWidth;

verticalRatio = (float) canvasHeight / (float) previewHeight;

} else {

horizontalRatio = (float) canvasHeight / (float) previewHeight;

verticalRatio = (float) canvasWidth / (float) previewWidth;

}

rect.left *= horizontalRatio;

rect.right *= horizontalRatio;

rect.top *= verticalRatio;

rect.bottom *= verticalRatio;

Rect newRect = new Rect();

switch (cameraOri) {

case 0:

if (mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {

newRect.left = canvasWidth - rect.right;

newRect.right = canvasWidth - rect.left;

} else {

newRect.left = rect.left;

newRect.right = rect.right;

}

newRect.top = rect.top;

newRect.bottom = rect.bottom;

break;

case 90:

newRect.right = canvasWidth - rect.top;

newRect.left = canvasWidth - rect.bottom;

if (mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {

newRect.top = canvasHeight - rect.right;

newRect.bottom = canvasHeight - rect.left;

} else {

newRect.top = rect.left;

newRect.bottom = rect.right;

}

break;

case 180:

newRect.top = canvasHeight - rect.bottom;

newRect.bottom = canvasHeight - rect.top;

if (mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {

newRect.left = rect.left;

newRect.right = rect.right;

} else {

newRect.left = canvasWidth - rect.right;

newRect.right = canvasWidth - rect.left;

}

break;

case 270:

newRect.left = rect.top;

newRect.right = rect.bottom;

if (mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {

newRect.top = rect.left;

newRect.bottom = rect.right;

} else {

newRect.top = canvasHeight - rect.right;

newRect.bottom = canvasHeight - rect.left;

}

break;

default:

break;

}

if (isMirror){

int left = newRect.left;

int right = newRect.right;

newRect.left = canvasWidth - right;

newRect.right = canvasWidth - left;

}

return newRect;

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐