Android flashlight Principle
I. Principles
Currently, the android app store has many flashlight applications. Its core principle is very simple. In fact, it is to use the Camera in android to control the flashing. Now, Let's explain how android controls the flashlight step by step.
2. Add Permissions
When using android camera, you must first add the android permission to control camera in the AndroidManifest. xml file.
What are these permissions for? For details, refer to my blog.
Http://blog.csdn.net/stoppig/article/details/20458865
Iii. Control Code
After the flash control is added, you can write the code for controlling the flash.
1. Open the camera and turn on the flashlight.
Before using the flashlight, use the Camera. open () method to open the camera and obtain the Camera object. Then the Parameters parameter is obtained through the getParameters () method of the camera object. The following is the sample code.
Camera = Camera. open (); parameters = camera. getParameters (); parameters. setFlashMode (Parameters. FLASH_MODE_TORCH); // enable camera. setParameters (parameters );
2. Turn off the flashlight and camera
When the flashlight is turned off, it is also set through the Parameters parameter. Set the FlashMode variable of Parameters to Parameters. FLASH_MODE_OFF, and then close the camera. The following is the sample code.
Parameters. setFlashMode (Parameters. FLASH_MODE_OFF); // disable light. setImageResource (R. drawable. light_off); camera. setParameters (parameters); camera. release ();
This is a simple android flashlight code description.
Source code: http://download.csdn.net/detail/stop_pig/8102453