即使设置了可用尺寸,Android相机预览也会延长

时间:2016-09-09 08:25:58

标签: android camera preview stretch

我正在安装Android设备上的自定义相机。预览是全屏活动。 为了解决拉伸问题,我使用如下代码:

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
    double ASPECT_TOLERANCE = 0.1;
    double targetRatio=(double)h / w;
    if (sizes == null) return null;
    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;
    int targetHeight = h;
    for (Camera.Size size : sizes) {
        double ratio = (double) size.height / size.width;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

//optimalSize 
myParam.setPreviewSize(optimalSize.width,optimalSize.height);
mCamera.setParameters(myParam);
mCamera.startPreview();
是的,它适用于几乎所有的设备。但在某些设备中它不起作用!无论我设置哪种尺寸的设备都支持它,它不起作用!那有什么不对?

0 个答案:

没有答案