Android method for controlling flash light of on and off

  • 2021-01-06 00:44:06
  • OfStack

This article illustrates the Android method of controlling flash by example. To share with you for your reference, as follows:

Want to do a flashlight recently, search to 1 point data on the net

Firstly, the flash can be controlled with android.hardware.camera

1. Add permissions to the Manifest.xml file

<uses-permission android:name="android.permission.CAMERA" />

2. Turn on the flash


try{
  m_Camera = Camera.open();
  Camera.Parameters mParameters;
  mParameters = m_Camera.getParameters();
  mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
  m_Camera.setParameters(mParameters);
} catch(Exception ex){}

3 Turn off the flash


try{
  Camera.Parameters mParameters;
  mParameters = m_Camera.getParameters();
  mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
  m_Camera.setParameters(mParameters);
  m_Camera.release();
} catch(Exception ex){}

In Android, you can simply open the camera through Camera.Open, and then get Camera.Parameters to set the parameters

For the flashlight function we need, just set it to FLASH_MODE_TROCH and then turn it off. Just set it to FLASH_MODE_OFF and then set it to release

Don't forget to call release() to terminate the application when it terminates

For more information on Android development, please check out the Android Development Introduction and Advanced Tutorials.

I hope this article described to everyone Android programming help.


Related articles: