[Android] How to Create a flashlight Program

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.