How to create Flashlight application in android.
How to create Flashlight application in android.
In this tutorial we will learn how to use android built in camera led as flashlight/torchlight.
by using camera object and by adding flash mode to camera object parameters using setFlashMode(Parameters.FLASH_MODE_TORCH) we can use camera flash as torch.
(only device which has camera LED Hardware)
first create a new android project.
open main layout file and add a ToggleButton to your main layout file(eg.:activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ToggleButton android:id="@+id/on_off_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textOff="Flash Light Off" android:textOn="Flash Light On" /> </RelativeLayout>
and now open main Activity file and made these changes(eg.:MainActivity.java)
package com.example.torchlight; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.Window; import android.widget.CompoundButton; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity { Camera camera = null; Parameters parameters; Context context = null; ToggleButton tbOnOff = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); tbOnOff = (ToggleButton) findViewById(R.id.on_off_button); tbOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { if (camera == null) { // get camera parameters and set flash mode to FLASH_MODE_TORCH camera = Camera.open(); parameters = camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); } } else { if (camera != null) { parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); camera.release(); camera = null; } } } }); //check that device has flash or not boolean checkFlash = context.getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA_FLASH); if (!checkFlash) { tbOnOff.setEnabled(false); Toast.makeText(context, "LED Not Available!", Toast.LENGTH_LONG) .show(); } } // release camera when onPause called @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if (camera != null) { parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); camera.release(); camera = null; } } }
don’t forget to add camera permission in Manifest file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.torchlight" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.CAMERA" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.torchlight.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
by running this application on actual android device you can use your android device camera LED as flash light/torch light.
(Visited 442 times, 1 visits today)