Wednesday, May 21, 2014

How to turn on/off camera LED / flashlight in Android

In this tutorial, we show you how to turn on/off the phone camera led or flashlight in Android. See code snippets :

1. Turn on
  1. camera = Camera.open();
  2. Parameters p = camera.getParameters();
  3. p.setFlashMode(Parameters.FLASH_MODE_TORCH);
  4. camera.setParameters(p);
  5. camera.startPreview();



2. Turn off
  1. camera = Camera.open();
  2. Parameters p = camera.getParameters();
  3. p.setFlashMode(Parameters.FLASH_MODE_OFF);
  4. camera.setParameters(p);
  5. camera.stopPreview();

And, put following permission on AndroidManifest.xml.

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


P.S This project is developed in Eclipse 3.7, and tested with Samsung Galaxy S2 (Android 2.3.3).

1. Android Layout


A button only.

File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <Button
        android:id="@+id/buttonFlashlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="Torch" />
 
</RelativeLayout>

2. Activity


Read the code, a button to turn on / off the flashlight, it should be self-explanatory.

  1. package com.mkyong.android;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.pm.PackageManager;
  5. import android.hardware.Camera;
  6. import android.hardware.Camera.Parameters;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. public class FlashLightActivity extends Activity {
  13. //flag to detect flash is on or off
  14. private boolean isLighOn = false;
  15. private Camera camera;
  16. private Button button;
  17. @Override
  18. protected void onStop() {
  19. super.onStop();
  20. if (camera != null) {
  21. camera.release();
  22. }
  23. }
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28. button = (Button) findViewById(R.id.buttonFlashlight);
  29. Context context = this;
  30. PackageManager pm = context.getPackageManager();
  31. // if device support camera?
  32. if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
  33. Log.e("err", "Device has no camera!");
  34. return;
  35. }
  36. camera = Camera.open();
  37. final Parameters p = camera.getParameters();
  38. button.setOnClickListener(new OnClickListener() {
  39. @Override
  40. public void onClick(View arg0) {
  41. if (isLighOn) {
  42. Log.i("info", "torch is turn off!");
  43. p.setFlashMode(Parameters.FLASH_MODE_OFF);
  44. camera.setParameters(p);
  45. camera.stopPreview();
  46. isLighOn = false;
  47. } else {
  48. Log.i("info", "torch is turn on!");
  49. p.setFlashMode(Parameters.FLASH_MODE_TORCH);
  50. camera.setParameters(p);
  51. camera.startPreview();
  52. isLighOn = true;
  53. }
  54. }
  55. });
  56. }
  57. }

3. Android Permission

 

Assign CAMERA permission.

File : AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="10" />
 
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
 
    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".FlashLightActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

4. Demo


None, until i have 2nd hand phone to capture the current flashlight on my phone :)

Download Source Code














No comments:

Post a Comment