Android: Image taken by custom camera is too dark

久未见 提交于 2019-12-19 09:07:34

问题


In my app I must take photo with custom activity. I implement all functionality and can save my photos, but in some devices they are too dark. I have this code, but it doesn't help me

            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);

In Logcat I see, that all params can be setted to AUTO.

I checked it in Samsung Galaxy II, and it works perfect, but in some LG phone I even can't an image, because it's too dark. Photos, taken by standart camera app and Facebook camera app looks perfect, so it can be done.

Sorry for my English=)


回答1:


I found one very strange solution to that problem. When my camera preview and taken picture have same ratios, resulting picture looks good on all tested devices. So after getting optimal preview size I am searching for supported picture size with the same ratio.

It's strange, but it works.

So, first we need to get preview size.

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;
}

And then we need to get picture size, which would have the same size ratio with preview.

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;
}

And then apply this sizes to 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);


来源:https://stackoverflow.com/questions/19571378/android-image-taken-by-custom-camera-is-too-dark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!