Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement a flashlight program in Android

Shulou Source: shulou.com Published: 2022-05-31 23:35:24 09月19日 Update

This article mainly introduces the relevant knowledge of how to achieve a flashlight program in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Android article on how to achieve a flashlight program. Let's take a look at it.

After the development of the flashlight in iphone, I started the program on how to make the flashlight in Android. My cross-platform engine framework CloudBox has established the iphone version of the program, but I have no idea how to control the flash on the Android, so I made a small example of how to use Android to control the flash.

This code works in the Samsung Galaxy S2 test.

Public class CloudLed {boolean m_isOn; Camera m_isOn 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 deal with the code related to the flash.

In Android, to enable the camera, you only need to use Camera.Open to enable it. After enabling it, you can get Camera.Parameters to set the parameters.

For the flashlight function we need, just set it to FLASH_MODE_TROCH

When you turn it off, just set it to FLASH_MODE_OFF and release it.

Tags: Flashlight flashlight program knowledge flash flash code content camera article camera nothing next nothing value function parameters just engine Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft vpn Xiaomi MySQL