android学习笔记

android自定义camera预览拉伸问题修改思路

2019-04-25  本文已影响0人  heheworld

1.问题背景
自定义相机,实现预览,拍照等功能
2.初步方案
根据屏幕宽高比,在相机预览尺寸中获取相等宽高比的。
3.遇到问题
手机虚拟键盘,全面屏等因素,导致预览宽高比 没有跟 屏幕宽高比 相等的,
百度到很多方案是选择最接近的,但还是会有拉升。
4.继续百度
有个思路是获取最接近屏幕宽高比的预览尺寸,去等比缩放预览view的宽高,
这样从另一个源头避免了相机预览的拉升

整理了下,基本的代码如下

 private Camera.Size getPreviewSize(List<Camera.Size> supportSizes, int screenWidth, int screenHeight) {
        List<Camera.Size> sizes = null;
        Camera.Size resultSize = null;
        //屏幕的宽高比
        float ratio = (screenHeight * 1.0f) / (screenWidth * 1.0f);
        //跟屏幕宽高比相同的预览尺寸
        List<Camera.Size> sameRatios = new ArrayList<>();

        for (Camera.Size s : supportSizes) {
            if ((s.width * 1.0f) / (s.height * 1.0f) == ratio) {
                sameRatios.add(s);
            }
        }
        //取同宽高比最大的预览尺寸
        if (!sameRatios.isEmpty()) {
            Camera.Size tempSameSize = null;
            for (Camera.Size ss : sameRatios) {
                if (tempSameSize == null) {
                    tempSameSize = ss;
                } else {
                    if (ss.height < tempSameSize.height) {
                        tempSameSize = ss;
                    } else {
                        if (tempSameSize.width < ss.width) {
                            tempSameSize = ss;
                        }
                    }
                }
                resultSize = tempSameSize;
            }
        } else {
            List<Camera.Size> nearRatios = new ArrayList<>();
            float minus = 100.0f;
            for (Camera.Size s : supportSizes) {
                if (s.width < 1000) {
                    //过滤下高小于1000的预览尺寸,避免获取到过低的预览尺寸
                    continue;
                }
                float curRatio = (s.width * 1.0f) / (s.height * 1.0f);
                if (Math.abs(curRatio - ratio) < minus) {
                    minus = Math.abs(curRatio - ratio);
                    nearRatios.clear();
                    nearRatios.add(s);
                } else if (Math.abs(curRatio - ratio) == minus) {
                    nearRatios.add(s);
                }
            }

            int width = 0;
            for (Camera.Size ns : nearRatios) {
                if (ns.height > width) {
                    resultSize = ns;
                    width = ns.height;
                }
            }
        }
        return resultSize;
    }

重置预览view宽高的代码

private void reSizeCameraView(Camera.Size previewsize) {
        if (screenHeight != previewsize.width || screenWidth != previewsize.height) {
            if (screenHeight > previewsize.width) {
                int resultW = screenHeight * previewsize.height / previewsize.width;
                int resultH = screenHeight;
                ViewGroup.LayoutParams params = cameraView.getLayoutParams();
                params.width = resultW;
                params.height = resultH;
                cameraView.setLayoutParams(params);
                cameraView.invalidate();
            } else {
                int resultW = previewsize.height;
                int resultH = previewsize.width;
                ViewGroup.LayoutParams params = cameraView.getLayoutParams();
                params.width = resultW;
                params.height = resultH;
                cameraView.setLayoutParams(params);
                cameraView.invalidate();
            }
        }
    }

参考自:# CameraView

上一篇下一篇

猜你喜欢

热点阅读