After the development of the iPhone flashlight, I began to work on how Android made the flashlight.Program
My cross-platform engine framework cloudbox has built an iPhone program, but I don't know how to manipulate the flashlight on Android.
So I made a small example to demonstrate how to use android to control the flash.
ThisCodeAvailable in Samsung Galaxy S2
Public class cloudled {Boolean m_ison; camera m_camera; Public Boolean getison () {return m_ison;} public cloudled () {m_ison = false;} public void turnon () {If (! M_ison) {m_ison = true; 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) {}} public void turnoff () {If (m_ison) {m_ison = false; 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 ){}}}}
I created a cloudled class to encapsulate camera again and only Process Code related to the Flash.
In Android, to enable the camera, you can simply enable the camera through camera. Open. After enabling the camera, you can get camera. parameters to set parameters.
We only need to set the flashlight to flash_mode_troch.
When it is disabled, you only need to set it to flash_mode_off and then release it.
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. clouddevelop. ledtest "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <activity Android: Name = ". ledtestactivity "Android: Label =" @ string/app_name "Android: screenorientation =" portrait "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> <uses-Permission Android: Name =" android. permission. camera "> </uses-Permission> </manifest>
Note that you must set the camera access permission in androidmanifest. xml.
<Uses-Permission Android: Name = "android. Permission. Camera"> </uses-Permission>
Download the complete example