Android Flashlight Implementation

Source: Internet
Author: User

Android phones have a lot of applications for flashlights, but some don't.

It was later discovered that the preview was opened in addition to setting the camera device's Flashmode to torch:

Here's the code:


Mainactivity.java

Package Com.android.flashlight;import Android.app.activity;import Android.os.bundle;import Android.view.motionevent;import Android.widget.imageview;public class Mainactivity extends Activity{private Flashlightsurface msurface;private ImageView mimageview;private Boolean Isflashlighton = false; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Contants.logi ("Mainactivity:oncreate ()"); msurface = (flashlightsurface) Findviewbyid (R.id.surfaceview); MImageView = (ImageView) Findviewbyid (r.id.image);} @Overridepublic boolean ontouchevent (Motionevent event) {if (motionevent.action_up = = Event.getaction ()) { Contants.logi ("Mainactivity:ontouchevent (): Action_up"), if (Isflashlighton) {Msurface.setflashlightswitch (false); Isflashlighton = False;mimageview.setimageresource (R.drawable.flashlight_off);} Else{msurface.setflashlightswitch (true); Isflashlighton = True;mimageview.setimageresource (R.drawable.flashlight_ on);}} Return SupeR.ontouchevent (event);}} 

Flashlightsurface.java

Package Com.android.flashlight;import Android.content.context;import Android.graphics.pixelformat;import Android.hardware.camera;import Android.util.attributeset;import Android.view.surfaceholder;import Android.view.surfaceview;public class Flashlightsurface extends Surfaceview implements surfaceholder.callback{ Private Surfaceholder mholder;private Camera mcameradevices;private camera.parameters mparameters;public Flashlightsurface (context context, AttributeSet Attrs) {Super (context, attrs); Contants.logi ("Flashlightsurface"); Mholder = This.getholder (); Mholder.addcallback (this); Mholder.settype ( Surfaceholder.surface_type_push_buffers);} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width,int height) {contants.logi (" Surfacechanged "); mparameters = Mcameradevices.getparameters (); if (mparameters! = null) Mparameters.setpictureformat ( PIXELFORMAT.JPEG); mparameters.setpreviewsize (480); Mparameters.setpicturesize (320, 480); Mcameradevices.setparameters (mparameters); MCAmeradevices.startpreview ();} @Overridepublic void surfacecreated (Surfaceholder holder) {contants.logi ("surfacecreated"); try {mcameradevices = Camera.open (); Mcameradevices.setpreviewdisplay (Mholder);} catch (Exception e) {if (mcameradevices! = null) mcameradevices.release (); mcameradevices = null;}} @Overridepublic void surfacedestroyed (Surfaceholder holder) {contants.logi ("surfacedestroyed"); if (mcameradevices = = NULL) Return;mcameradevices.stoppreview (); Mcameradevices.release (); mcameradevices = null;} /** * Set the switch status of the flashlight * @param on:true is turned on, false turns off */public void Setflashlightswitch (Boolean on) {if (mcameradevices = = null) r Eturn;if (mparameters = = null) {mparameters = Mcameradevices.getparameters ();} if (ON) {Mparameters.setflashmode (contants.flash_mode_torch);} Else{mparameters.setflashmode (Contants.flash_mode_off);} Contants.logi ("Setflashlightswitch-----------------" + on); Mcameradevices.setparameters (mparameters);}}

Layout file Main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout   xmlns:android= "http://schemas.android.com/apk /res/android "      android:layout_width=" fill_parent "      android:layout_height=" fill_parent ">    < Com.android.flashlight.FlashlightSurface        android:id= "@+id/surfaceview"        android:layout_width= "Fill_ Parent "      android:layout_height=" fill_parent "        ></com.android.flashlight.FlashlightSurface>    <imageview         android:id= "@+id/image"        android:layout_width= "fill_parent"      android:layout_height= " Fill_parent "    android:src=" @drawable/flashlight_off "    android:background=" #FFFFFFFF "        />    </RelativeLayout>

It was obvious that a preview had been opened, but the picture was covered and invisible. Otherwise the flash does not light.


There's a demo:http://download.csdn.net/detail/liu_zhen_wei/4801779 here.

Contains widgets form widgets usage and flashlight implementation, after installation, add the form widget (flashlight) to the desktop and then click on the widget to open the flashlight interface

Click on the interface to enable/disable the flashlight.

Android Flashlight Implementation

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.