Pages

Monday, July 23, 2012

How to Open camera in ANDROID?

In today’s tutorial I will show you how to use camera in ANDROID in your program.
In this tutorial we will be having a button which will open the camera and after taking the photo it will show it in an imageView.
Note: This program will work only in the real device not in the emulator. So make sure to test it in the device itself. Also make sure to add the permission while using camera as shown below in your AndroidManifest file.
1
<uses-feature android:name="android.hardware.camera"></uses-feature>
Here is the main java file code
?
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
package com.coderzheaven;
 
<span class="IL_AD" id="IL_AD8">import</span> android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
 
public class OpenCameraDemo extends Activity {
 
    private static final int CAMERA_PIC_REQUEST = 2500;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Button b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.<span class="IL_AD" id="IL_AD12">provider</span>.<span class="IL_AD" id="IL_AD6">MediaStore</span>.ACTION_IMAGE_CAPTURE);
                 startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        });
    }
 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
              Bitmap image = (Bitmap) data.getExtras().get("data");
              ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
              imageview.setImageBitmap(image);
        }
    }
}
Now the main.xml file which contains the button and the imageview.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://<span class="IL_AD" id="IL_AD5">schemas</span>.android.com/apk/res/android"
    android:<span class="IL_AD" id="IL_AD9">orientation</span>="<span class="IL_AD" id="IL_AD11">vertical</span>"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ImageView>
<Button
    android:text="Open Camera"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>
The strings.xml file
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, OpenCameraDemo!</string>
    <string name="app_name">OpenCamera</string>
</resources>
And the AndroidManifest.xml file.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
 
      <uses-feature android:name="android.hardware.camera"></uses-feature>
 
    <<span class="IL_AD" id="IL_AD3">application</span> android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenCameraDemo"
                  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>

No comments:

Post a Comment

Popular Posts