Friday, December 3, 2010

Camera Preview on SurfaceView

With little bit of modification on last exercise "Play 3gp video file using MediaPlayer", it's easy to implement a app to preview Android camera on SurfaceView.

Camera Preview on SurfaceView

Modify AndroidManifest.xml to grant permission to access Camera
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidCamera"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidCamera"
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>
<uses-sdk android:minSdkVersion="4" />

<uses-permission android:name="android.permission.CAMERA"></uses-permission>
</manifest>


Modify main.xml, basically same as the one in the last exercise.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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"
/>
<Button
android:id="@+id/startcamerapreview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start Camera Preview -"
/>
<Button
android:id="@+id/stopcamerapreview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Stop Camera Preview -"
/>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Modify the source code, AndroidCamera.
package com.exercise.AndroidCamera;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;;

String stringPath = "/sdcard/samplevideo.3gp";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);

getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!previewing){
camera = Camera.open();
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}});

buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(camera != null && previewing){
camera.stopPreview();
camera.release();
camera = null;

previewing = false;
}
}});

}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}
}


Download the files.

Related Article:
- Camera Preview, version II



10 comments:

stefan2k said...

Thank you works like a charm!

Anonymous said...

Tnx! worked at once

Anonymous said...

Thats rock!

Thanks!

Soraia Olveira said...

Thank you very much :D

John said...

Finally a code that really works!

I've been searching for days, thanks.

Sotaya Yakubu said...

Hey, i have been getting this error whenever i press the start preview button, i don't know what is wrong. do you have a guess?


04-07 00:26:32.190: E/AndroidRuntime(18438): java.lang.RuntimeException: startPreview failed
04-07 00:26:32.190: E/AndroidRuntime(18438): at android.hardware.Camera.startPreview(Native Method)
04-07 00:26:32.190: E/AndroidRuntime(18438): at com.Server.camera.Main$1.onClick(Main.java:47)
04-07 00:26:32.190: E/AndroidRuntime(18438): at android.view.View.performClick(View.java:2485)

Sotaya Yakubu said...
This comment has been removed by the author.
Anonymous said...

How do I use this???

Anonymous said...

That code is perfect. It is clean and it does exactly what it is supposed to do.

erum hannan said...

can someone pls help me how can i display live cameraPreview on more than one SurfaceView or more than one FrameLayouts its like live camera in more than one surfaceview
i have implemented a class that extends from SurfaceView like this :
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback


and then add this surfaceView class in Framelayout now i want to show same surfaceView in another framelayout but its giving me error how can i achieve this