-1

I am facing a problem in a custom camera application. With the flash function turned ON, the phone takes the 1st photo with flash, but on the 2nd photo it doesn't use the flash.

  flashCameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isPressed) {
                flashCameraButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.onflash));

                flashOnButton();
            } else if (isPressed) {
                flashCameraButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.offflash));
                isPressed = !isPressed;
                flashOffButton();
            } else
                flashCameraButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.onflash));
            flashOnButton();
        }
    });

    private void flashOnButton() {
    if (camera != null) {
        try {
            Camera.Parameters param = camera.getParameters();
            param.setFlashMode(!flashmode ? Camera.Parameters.FLASH_MODE_ON
                    : Camera.Parameters.FLASH_MODE_ON);
            camera.setParameters(param);
            flashmode = !flashmode;
        } catch (Exception e) {
            // TODO: handle exception
        }

    }
}
   private void flashOffButton() {
    if (camera != null) {
        try {
            Camera.Parameters param = camera.getParameters();
            param.setFlashMode(!flashmode ? Camera.Parameters.FLASH_MODE_OFF
                    : Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(param);
            flashmode = !flashmode;
        } catch (Exception e) {
            // TODO: handle exception
        }

    }
}
Ziffusion
  • 8,779
  • 4
  • 29
  • 57
Harpal Singh
  • 188
  • 1
  • 3
  • 16

1 Answers1

0

Take a look at the code below. You can do something similar to it:

btnSwitch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isFlashOn) {
                // turn off flash
                //change the picture 
                turnOffFlash();
            } else {
                // turn on flash
                //change the picture
                turnOnFlash();
            }
        }
    });
}


// Get the camera
private void getCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
        }
    }
}


 // Turning On flash
private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }


        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;


    }

}


// Turning Off flash
private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }


        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOn = false;


    }
}

What we are doing here is checking if the Flash is ON or OFF, if it is ON, we call the method turnOffFlash() to turn it off and if it is OFF we call the method turnOnFlash() to turn it on.

Anoop Kanyan
  • 618
  • 7
  • 19
  • ok thanks Anoop Kanyan and how can i declare (isFlashon) in this method – Harpal Singh Jan 01 '16 at 07:44
  • declare it outside all the methods and initially set it to false. – Anoop Kanyan Jan 01 '16 at 08:43
  • ok thanks but i want when flash is on no doubt it click first image with flash on and second time it click image with flash off automatically. i want if flash is on it should on until i we change the mode in flash off – Harpal Singh Jan 01 '16 at 09:35