Android解决相机预览拉伸问题

拉伸的原因

1.主要是由于surfaceview的尺寸和 相机预览尺寸不一致造成的

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.e("相机--》surfaceview", "width--" + width);
        Log.e("相机--》surfaceview", "height--" + height);
        Log.e("相机--》mCamera", "width--" + mCamera.getParameters().getPreviewSize().width);
        Log.e("相机--》mCamera", "height--" +  mCamera.getParameters().getPreviewSize().height);
    }

log为
相机–》surfaceview: width–720
相机–》surfaceview: height–360
相机–》mCamera: width–1920
相机–》mCamera: height–1080
可以看到 surfaceview宽高比为2/1 但是相机预览尺寸比为16/9,所以造成了拉伸
2.解决办法获取相机预览的支持尺寸,动态设置最接近的尺寸

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Camera.Size closelyPreSize = getCloselyPreSize(width, height, mCamera.getParameters().getSupportedPreviewSizes());
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(closelyPreSize.width,closelyPreSize.height);
        mCamera.setParameters(parameters);
        Log.e("相机--》surfcaeview", "width--" + width);
        Log.e("相机--》surfcaeview", "height--" + height);
        Log.e("相机--》closelyPreSize", "width--" + closelyPreSize.width);
        Log.e("相机--》closelyPreSize", "height--" + closelyPreSize.height);
        Log.e("相机--》mCamera", "width--" + mCamera.getParameters().getPreviewSize().width);
        Log.e("相机--》mCamera", "height--" +  mCamera.getParameters().getPreviewSize().height);
    }

log为
相机–》surfcaeview: width–720
相机–》surfcaeview: height–360
相机–》closelyPreSize: width–1920
相机–》closelyPreSize: height–960
相机–》mCamera: width–1920
相机–》mCamera: height–960
可以看到此时相机和surfaceview的比例都为2/1,预览就没有了拉伸

3最后附上获取接近比例的方法,

protected Camera.Size getCloselyPreSize(int surfaceWidth, int surfaceHeight,
                                            List<Camera.Size> preSizeList) {

        int ReqTmpWidth;
        int ReqTmpHeight;
        // 当屏幕为垂直的时候需要把宽高值进行调换,保证宽大于高
        if (false) {
            ReqTmpWidth = surfaceHeight;
            ReqTmpHeight = surfaceWidth;
        } else {
            ReqTmpWidth = surfaceWidth;
            ReqTmpHeight = surfaceHeight;
        }
        //先查找preview中是否存在与surfaceview相同宽高的尺寸
        for (Camera.Size size : preSizeList) {
            if ((size.width == ReqTmpWidth) && (size.height == ReqTmpHeight)) {
                return size;
            }
        }

        // 得到与传入的宽高比最接近的size
        float reqRatio = ((float) ReqTmpWidth) / ReqTmpHeight;
        float curRatio, deltaRatio;
        float deltaRatioMin = Float.MAX_VALUE;
        Camera.Size retSize = null;
        for (Camera.Size size : preSizeList) {
            curRatio = ((float) size.width) / size.height;
            deltaRatio = Math.abs(reqRatio - curRatio);
            if (deltaRatio < deltaRatioMin) {
                deltaRatioMin = deltaRatio;
                retSize = size;
            }
        }

        return retSize;

    }

相机开发 学习
https://gitbook.cn/books/5aedb7582b543f6855b437af/index.html

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页