Pages

Monday, July 23, 2012

A Simple FlashLight Application.

Here is a simple Application on how to use flashlight in android.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.coderzheaven.pack;
 
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class FlashLightActivity extends Activity {
 
    //flag to detect flash is on or off
    private boolean isLighOn = false;
 
    private Camera camera;
 
    private Button button;
 
    @Override
    protected void onStop() {
        super.onStop();
 
        if (camera != null) {
            camera.release();
        }
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        button = (Button) findViewById(R.id.buttonFlashlight);
 
        Context context = this;
        PackageManager pm = context.getPackageManager();
 
        // if device support camera?
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }
        try{
            camera = Camera.open();
            final Parameters p = camera.getParameters();
     
            button.setOnClickListener(new OnClickListener() {
     
                @Override
                public void onClick(View arg0) {
     
                    if (isLighOn) {
     
                        Log.i("info", "torch is turn off!");
     
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
                        camera.setParameters(p);
                        camera.stopPreview();
                        isLighOn = false;
     
                    } else {
     
                        Log.i("info", "torch is turn on!");
     
                        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
     
                        camera.setParameters(p);
                        camera.startPreview();
                        isLighOn = true;
     
                    }
     
                }
            });
        }catch(Exception e){
            Toast.makeText(this, "Your device doesnot have FlashLight capability", Toast.LENGTH_LONG).show();
        }
 
    }
}
Now the layout file main.xml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>
AndroiManifest.xml file contents.
Make sure to add the permissions.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coderzheaven.pack"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="5" />
 
    <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>

No comments:

Post a Comment

Popular Posts