Android control flash method (open and close)

Android mobile operating system

Android is a free and open source operating system based on Linux, mainly used in mobile devices such as smartphones and tablets, led and developed by Google and the Open Handset Alliance. There is no unified Chinese name yet, and more people in mainland China use "Android" or "Anzhi".


This article mainly introduces the method of Android to control the flash, which can realize the effect of turning on and off the flash. It involves the related skills of Android to operate the Camera to take pictures of the flash. Friends who need it can refer to the following.

The example of this article describes the method of Android to control the flash. Share for your reference, the details are as follows:

I want to make a flashlight recently, I found some information on the Internet

First the flash can be controlled with android.hardware.camera

1. Add permissions in 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, to open the camera, you can simply open it through Camera.Open. After opening, you can get Camera.Parameters to set the parameters.

The flashlight function we need, just set it to FLASH_MODE_TROCH to turn it off, just set it to FLASH_MODE_OFF and release it.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855091&siteId=291194637