android相机拉伸解决办法

公司要做安卓开发板的双目识别项目,采用的是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();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值