Write your own Android flashlight, support 5.0

Source: Internet
Author: User

The Android flashlight principle is simple, is to turn on the camera flash.

The core code is so much:

Turn on the camera

Set the Flash to open

Open Preview

Setting the camera's autofocus

Camera camera = Camera.open ();    Parameters mparameters = Camera.getparameters ();     Mparameters.setflashmode (Camera.Parameters.FLASH_MODE_TORCH);    Camera.setparameters (mparameters);    Camera.startpreview (); Camera.autofocus (New Autofocuscallback () {public void Onautofocus (Boolean success, Camera camera) {}});
These few steps are indispensable. I tried it on the internet before, and it never worked. Just because there's something missing. Open preview, some missing autofocus


Then it's set permissions.

<uses-permission android:name= "Android.permission.CAMERA"/>    <uses-permission android:name= "  Android.permission.FLASHLIGHT "/> <uses-feature android:name=" Android.hardware.camera "/> <uses-feature Android:name= "Android.hardware.camera.autofocus"/>    <uses-feature android:name= " Android.hardware.camera.flash "/>

Finally, the following complete activity is posted. My this is very simple, create the time to turn on the flashlight, the screen shows "Flashlight Open", press the Return key, close and exit the app.

Package Org.lhc.flashlight;import Android.app.activity;import Android.hardware.camera;import Android.hardware.camera.autofocuscallback;import Android.hardware.camera.parameters;import Android.os.Bundle; Import Android.util.log;import Android.view.keyevent;import Android.widget.textview;public class MainActivity Extends Activity {private camera camera = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); This.camera = Camera.open (); if ( This.camera = null) {Parameters mparameters = Camera.getparameters (); Mparameters.setflashmode ( Camera.Parameters.FLASH_MODE_TORCH); Camera.setparameters (mparameters); Camera.startpreview (); Camera.autofocus (new Autofocuscallback () {public void Onautofocus (Boolean success, Camera camera) {}}); TextView info = (TextView) This.findviewbyid (r.id.flashlight_info); Info.settext ("Flashlight Open");}} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = KeyeVent. Keycode_back) {if (This.camera! = null) {Parameters mparameters = Camera.getparameters (); Mparameters.setflashmode ( Camera.Parameters.FLASH_MODE_OFF); camera.setparameters (mparameters); Camera.stoppreview (); Camera.release ();} LOG.I ("Flashlight", "exit"); This.finish (); System.exit (0);} Return Super.onkeydown (KeyCode, event);}}

Reprint please indicate the source

Http://blog.csdn.net/redstarofsleep

Write your own Android flashlight, support 5.0

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.