公司要做安卓开发板的双目识别项目,采用的是Camera+SurfaceView ,其实TextureView这个也是一样的原理。采用的是腾讯本地人脸识别方案,其实所有家的人脸识别道理都是相通的,主要相机采集数据-分析人脸数据-识别,,,,
但是同样遇到了一些问题,首先是摄像头拉伸问题,解决这个问题需要了解相机原理,以分辨率640*480为例? ? 那相机呈现的是这样的
那这样是不拉伸的 我的机器是1280*800的 那如果铺满全屏 那就是相机分辨率和屏幕分辨率是成比例放大的 那就需要拉伸SurfaceView达到和布局一样的分辨率,如图所示
那就是把SurfaceView相机的比例同比扩大相应的倍数才可以下面是我处理方式 然后我在调整布局到中间 就可以了,相机拉伸问题就解决了
private void initLayout() {
int surfaceViewWidth = getWidth();
int surfaceViewHeight = getHeight();
int cameraWidth = 0;
int cameraHeight = 0;
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) faceRelativeLayout.getLayoutParams();
//判断相机方向需要改变更改宽和高
if (StaticVariables.isRotation) {
cameraWidth = StaticVariables.cameraWidth;
cameraHeight = StaticVariables.cameraHeight;
} else {
cameraWidth = StaticVariables.cameraHeight;
cameraHeight = StaticVariables.cameraWidth;
}
//如果相机比例相等 不做拉伸
if (Chufa(surfaceViewWidth, cameraWidth) == Chufa(surfaceViewHeight, cameraHeight)) {
layoutParams.width = surfaceViewWidth;
layoutParams.height = surfaceViewHeight;
StaticVariables.faceCameraViewWidth = layoutParams.width;
StaticVariables.faceCameraViewHeight = layoutParams.height;
StaticVariables.faceCameraViewLeft = 0;
StaticVariables.faceCameraViewRight = surfaceViewWidth;
StaticVariables.faceCameraViewTop = 0;
StaticVariables.faceCameraViewDown = surfaceViewHeight;
} else
//如果相机与画布比例不等 ,计算画布大小 以及识别范围
if (Chufa(surfaceViewWidth, cameraWidth) > Chufa(surfaceViewHeight, cameraHeight)) {
layoutParams.width = surfaceViewWidth;
layoutParams.height = (int) (cameraHeight * Chufa(surfaceViewWidth, cameraWidth));
layoutParams.topMargin = (surfaceViewHeight - layoutParams.height) / 2;
layoutParams.bottomMargin = (surfaceViewHeight - layoutParams.height) / 2;
StaticVariables.faceCameraViewWidth = layoutParams.width;
StaticVariables.faceCameraViewHeight = layoutParams.height;
StaticVariables.faceCameraViewLeft = 0;
StaticVariables.faceCameraViewRight = surfaceViewWidth;
StaticVariables.faceCameraViewTop = -layoutParams.topMargin;
StaticVariables.faceCameraViewDown = -layoutParams.topMargin + surfaceViewHeight;
} //如果相机与画布比例不等 ,计算画布大小 以及识别范围
else {
layoutParams.width = (int) (cameraWidth * Chufa(surfaceViewHeight, cameraHeight));
layoutParams.height = surfaceViewHeight;
layoutParams.leftMargin = (surfaceViewWidth - layoutParams.width) / 2;
layoutParams.rightMargin = (surfaceViewWidth - layoutParams.width) / 2;
StaticVariables.faceCameraViewWidth = layoutParams.width;
StaticVariables.faceCameraViewHeight = layoutParams.height;
StaticVariables.faceCameraViewLeft = -layoutParams.leftMargin;
StaticVariables.faceCameraViewRight = -layoutParams.leftMargin + surfaceViewWidth;
StaticVariables.faceCameraViewTop = 0;
StaticVariables.faceCameraViewDown = surfaceViewHeight;
}
faceRelativeLayout.setLayoutParams(layoutParams);
faceRelativeLayout.requestLayout();
}
|