Wednesday, March 2, 2011

Android: Capture the best moments

I was expecting to write a new post on lambu's birthday...but it didn't happened..So here is another android application through which you can capture an image an view it as and when you capture it as a thumbnail....

My Activity

package com.apiit.TryCamera;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class TryCamera extends Activity implements SurfaceHolder.Callback{
private Camera camera;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView surface=(SurfaceView)findViewById(R.id.surface);
final SurfaceHolder holder=surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(150, 150);
Button tke=(Button)findViewById(R.id.take_btn);
tke.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
takePicture();
}

});
}

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

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

try{
camera = camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();
}
catch(IOException e)
{

}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}

private void takePicture(){
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
// TODO Do something when the shutter closes.
}
};
PictureCallback rawCallback = new PictureCallback() {

public void onPictureTaken(byte[] data, Camera camera) {
// TODO Do something with the image RAW data.

}
};
PictureCallback jpegCallback = new PictureCallback() {

public void onPictureTaken(byte[] data, Camera camera) {
// Save the image JPEG data to the SD card
FileOutputStream outStream=null;

FileInputStream fstream = null;
Bitmap bmp;
try {
File root=Environment.getExternalStorageDirectory();
File jpgfile=new File(root,"vinsol.jpg");
outStream=new FileOutputStream(jpgfile);
outStream.write(data);
outStream.flush();
outStream.close();
fstream = new FileInputStream(Environment.getExternalStorageDirectory() + "/" + "vinsol.jpg");
bmp = BitmapFactory.decodeStream(fstream);
ImageView iv=(ImageView)findViewById(R.id.image);
iv.setImageBitmap(bmp);

} catch (FileNotFoundException e) {
Log.d("CAMERAIO", e.getMessage());
} catch (IOException e) {
Log.d("CAMERAIO", e.getMessage());
}
camera.startPreview();
}
};
}





Layout


<?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"
>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="300dip"
android:id="@+id/surface"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="70dip"
android:layout_height="wrap_content"
android:text="Take Picture"
android:id="@+id/take_btn"/>
<ImageView
android:layout_marginTop="10dip"
android:layout_marginLeft="-5dip"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:id="@+id/image"/>

</LinearLayout>
</LinearLayout>





Important Points

You must be able to see a <SurfaceView> tag in my layout..
When you need to update the content of the view rapidly you need to use the SurfaceView..
as is the case of the camera (Updated frequently)

                                                                     A) Initial Screen

B) Captured Image Thumbnail


As I could not run the application on a device...the screenshot is not showing the proper preview...


Enjoy...Suggestions required..