4

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

rstk
  • 414
  • 5
  • 14
  • What about changing exposure compensate? you can set manually using `setExposureCompensation(int)`. – Sohyun Ahn Oct 25 '13 at 01:24
  • Thanks for your response. My app should be able to take good photos in different types of lightning, so I want to use only auto settings. – rstk Oct 25 '13 at 07:46

1 Answers1

2

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);
rstk
  • 414
  • 5
  • 14
  • my camera preview and taken picture have same ratios, resulting picture looks good on all tested devices. what is this??? can you please give a sample code logic because i have the same issue of custom camera app. – kiran boghra Dec 16 '13 at 11:17
  • Didn't make any difference to the brightness/exposure for me. The image was still dark. – Sam Dec 06 '14 at 03:11