Android:自定义相机拍摄的图像太暗

在我的应用程序中,我必须使用自定义活动拍照.我实现了所有功能并可以保存我的照片,但在某些设备中它们太暗了.我有这个代码,但它对我没有帮助

            Parameters params = mCamera.getParameters();
            if (params.getSupportedWhiteBalance().contains(
                    Parameters.WHITE_BALANCE_AUTO)) {
                params.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
                Log.d(TAG, "white balance auto");
            }
            if (params.getSupportedFlashModes().contains(
                    Parameters.FLASH_MODE_AUTO)) {
                params.setFlashMode(Parameters.FLASH_MODE_AUTO);
                Log.d(TAG, "flash mode auto");
            }
            if (params.getSupportedSceneModes().contains(
                    Parameters.SCENE_MODE_AUTO)) {
                params.setSceneMode(Parameters.SCENE_MODE_AUTO);
                Log.d(TAG, "scene mode auto");
            }
            mCamera.setParameters(params);

在Logcat中我看到,所有参数都可以设置为AUTO.

我在三星Galaxy II中检查了它,它的工作原理很完美,但在某些LG手机中我甚至无法拍摄图像,因为它太暗了.由标准相机应用和Facebook相机应用拍摄的照片看起来很完美,所以可以做到.

对不起我的英文=)

最佳答案 我发现了一个非常奇怪的解决方案.当我的相机预览和拍摄的照片具有相同的比例时,所得到的图像在所有测试设备上看起因此,在获得最佳预览尺寸后,我将以相同的比率搜索支持的图片尺寸.

这很奇怪,但它确实有效.

所以,首先我们需要获得预览尺寸.

protected Size getOptimalPreviewSize(List<Size> sizes, int width, int height) {
    Log.d(TAG, String
            .format("getOptimalPreviewSize: width = %d, height = %d",
                    width, height));
    final double ASPECT_TOLERANCE = 0.01;
    final double targetRatio = (double) 4 / 3d;
    if (sizes == null)
        return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = height;

    // Try to find an size match aspect ratio and size
    double ratio;
    Size size;
    for (int i = 0; i < sizes.size(); i++) {
        size = sizes.get(i);
        ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    // Cannot find the one match the aspect ratio, ignore the requirement
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (int i = 0; i < sizes.size(); i++) {
            size = sizes.get(i);
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }

    if (optimalSize == null) {
        Log.d(TAG, "Optimal size not found");
    } else {
        Log.d(TAG,
                String.format(
                        "getOptimalPreviewSize result: width = %d, height = %d for input width = %d, height = %d",
                        optimalSize.width, optimalSize.height, width,
                        height));
    }

    return optimalSize;
}

然后我们需要获得图片大小,这与预览具有相同的大小比例.

private Size getOptimalPictureSize() {
    if (mCamera == null)
        return null;

    List<Size> cameraSizes = mCamera.getParameters()
            .getSupportedPictureSizes();
    Size optimalSize = mCamera.new Size(0, 0);
    double previewRatio = (double) mPreviewSize.width / mPreviewSize.height;

    for (Size size : cameraSizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - previewRatio) > 0.01f)
            continue;
        if (size.height > optimalSize.height) {
            optimalSize = size;
        }
    }

    if (optimalSize.height == 0) {
        for (Size size : cameraSizes) {
            if (size.height > optimalSize.height) {
                optimalSize = size;
            }
        }
    }
    return optimalSize;
}

然后将此尺寸应用于Camera.Parametres

        Size optimalSize = getOptimalPictureSize();

    Parameters params = mCamera.getParameters();
    Log.d(TAG, "picture size " + optimalSize.width + " "
            + optimalSize.height);
    params.setPictureSize(optimalSize.width, optimalSize.height);
    mCamera.setParameters(params);
点赞