1

We have an app that works with all our supported Android phones "except Samsung Galaxy S5". Our app uses the camera to take pictures at close range. We need torch mode ON the entire time we are focusing to take the picture. We check for supported parameters and set the values if supported.

The params get set but the event either never gets fired or the camera is ignoring my settings. I tested using OpenCamera and their app is able to turn the torch on yet I cannot find the difference between how I set my params vs. how they set theirs.

Here is our code:

//Set all camera parameters(flash, focus, white balance, etc)
private void setCameraParameters()
{
    //Rotate the orientation of the preview to match orientation of device
    camera.setDisplayOrientation(getCameraRotation());

    //A Parameters object must be used to set the other parameters.
    Parameters params = camera.getParameters();

        //Flash Mode to Torch if supported
        if(params.getSupportedFlashModes().contains("torch"))
        {
            // Torch mode
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        }

        //Focus Mode to Macro if supported, Auto if not
        if(params.getSupportedFocusModes().contains("macro"))
        {
            //Macro focus mode
            params.setFocusMode(Parameters.FOCUS_MODE_MACRO);
        }
        else
        {
            //Auto focus mode
            params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
        }


        //White Balance mode to Auto if available.
        List<String> supported_white = params.getSupportedWhiteBalance();
        if(supported_white!=null)
        {
            if(supported_white.contains("auto"))
            {
                params.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
            }
        }

        // Auto Exposure Lock to false if available 
        if(params.isAutoExposureLockSupported())
        {
            params.setAutoExposureLock(false);
        }

        // Auto White Balance Lock if available. 
        if(params.getAutoWhiteBalanceLock())
        {
            params.setAutoWhiteBalanceLock(false);
        }

        //JPEG quality set to 100(highest)
        {
            params.setJpegQuality(100);
        }

        //Set focus area and metering area
        List<Camera.Area> focusArea = calculateFocusArea();
        params.setFocusAreas(focusArea);
        params.setMeteringAreas(focusArea);
        Camera.Size size = pickCameraSize(params.getSupportedPictureSizes());
        params.setPictureSize(size.width, size.height);

    //Set new parameters for camera
    camera.setParameters(params);

    boolean torch = getTorchState(camera);
}

// Added this method from zxing github to see if the value is being set
boolean getTorchState(Camera camera) {
    if (camera != null) {
        Camera.Parameters parameters = camera.getParameters();
        if (parameters != null) {
            String flashMode = camera.getParameters().getFlashMode();
            return flashMode != null
                    && (Camera.Parameters.FLASH_MODE_ON.equals(flashMode) || Camera.Parameters.FLASH_MODE_TORCH
                            .equals(flashMode));
        }
    }
    return false;
}
Patricia
  • 5,019
  • 14
  • 72
  • 152
  • Added a method to check the torch state. It is set, even though it's not turning on. In the native camera app, the torch does come on when set. So, now I'm looking at the Google code. Still hoping someone will have some helpful info. :-) – Patricia Jan 27 '15 at 00:34

2 Answers2

1

I'm doing it slightly different.. maybe it will help you!

                params = getCamera().getParameters();
    ...

                //Check if device supports torch mode, If YES then enable
                List<String> supportedFlashModes = params.getSupportedFlashModes();
                if (supportedFlashModes != null && supportedFlashModes.contains(Parameters.FLASH_MODE_TORCH)){
                    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    torchModeOn  = true;
                }
...

                getCamera().setParameters(params);

In comparison I'm using Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE and NOT using setFocusAreas, setMeteringArea, setAutoWhiteBalanceLock, setWhiteBalance or setAutoExposureLock.

After seeing your code, i tried to incorporate each one individually to see if it would better affect my pictures, with no luck. (My app requires close-up pictures as well.

Parameters.FOCUS_MODE_MACRO was not working well for me at all on any of the devices i tried it with..

EDIT:

Here is the order i am setting up my camera incase it helps...

        setCameraDisplayRotation();

        params = getCamera().getParameters();

        setFocusMode();

        //Check if device supports torch mode, If you YES then set on
        List<String> supportedFlashModes = params.getSupportedFlashModes();
        if (supportedFlashModes != null && supportedFlashModes.contains(Parameters.FLASH_MODE_TORCH)){
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            torchModeOn  = true;
        }

        setImageResolution();

        getCamera().setParameters(params); // update params before preview.setCamera
        preview.setCamera(getCamera());   

        //... some custom code for determining the current screens available space for the preview

        params.setPreviewSize(size.width, size.height);

        if(setHiddenParameter(params, "zsl-values", "zsl", "on")){
            setUsingZsl(true);
        };

        getCamera().setParameters(params); //update params after preview init
BBaker
  • 156
  • 1
  • 7
  • Huh! We're able to use macro mode. The camera parameters show the flash mode is torch but the camera is not responding. Are you changing the flash mode to torch "AFTER" the preview is loaded? I tried changing the order and that didn't work. I'm wondering if there is a timing issue. – Patricia Jan 28 '15 at 21:47
  • I edited the response to include my order of configuring the camera, in case it helps you. I cannot get the Macro Mode to focus either by using tap or takePicture, however i haven't tried it again after the ZSL revelation. – BBaker Jan 29 '15 at 16:57
1

You have to use new camera2 API in Android Lollipop

Sample Code github

and developer site

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300