Showing posts with label CAMERA APP ANDROID. Show all posts
Showing posts with label CAMERA APP ANDROID. Show all posts

Monday, 15 October 2012

Create your own camera app and take images android

  1. Create a SurfaceView as follow (it will interact with camera)

    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import android.content.Context;
    import android.hardware.Camera;
    import android.hardware.Camera.PreviewCallback;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class CameraView extends SurfaceView implements 
    SurfaceHolder.Callback
    {
     SurfaceHolder holder;
     Camera camera;
     public CameraView(Context context) 
     {
      super(context);
      holder=getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     }
     public void surfaceChanged(SurfaceHolder holder, int format,
       int width,int height) 
     {
    
      Camera.Parameters parameters=camera.getParameters();
      parameters.set("flash-mode", "on");
      camera.setParameters(parameters);
      camera.startPreview();
     }
     public void surfaceCreated(SurfaceHolder holder) 
     {
      try 
      {
       camera=Camera.open();
       camera.setPreviewDisplay(holder);
      } catch (IOException e) 
      {
       e.printStackTrace();
      }
     }
     public void surfaceDestroyed(SurfaceHolder holder) 
     {
      camera.stopPreview();
      camera.setPreviewCallback(null);
      camera.release();
      camera=null;
     }
    }
    

  2. Now Create front interface for camera Activity

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import android.app.Activity;
    import android.content.Intent;
    import android.hardware.Camera;
    import android.hardware.Camera.PictureCallback;
    import android.hardware.Camera.ShutterCallback;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.widget.*;
    
    public class CameraFront extends Activity
    {
     CameraView camview;
     FrameLayout frame;
     Button Click,Close;
     static String filename; 
     public void onCreate(Bundle bundle)
     {
      super.onCreate(bundle);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.camframe);
      Click=(Button)findViewById(R.id.buttonClick); 
      Click.setOnClickListener(new Button.OnClickListener(){
    
      public void onClick(View v) {
        
       camview.camera.takePicture(shutterCallback, 
         rawCallback, jpegCallback);
    
       }});
      Close=(Button)findViewById(R.id.buttonClose);
      Close.setOnClickListener(new Button.OnClickListener(){
    
      public void onClick(View v) {
      
       CameraFront.this.finish();
    
       }});
      camview=new CameraView(this);
      frame=(FrameLayout)findViewById(R.id.preview);
      frame.addView(camview);
     }
     ShutterCallback shutterCallback = new ShutterCallback() {
      public void onShutter() {
    
      }
     };
     PictureCallback rawCallback = new PictureCallback() {
      public void onPictureTaken(byte[] data, Camera camera) {
    
      }
     };
     PictureCallback jpegCallback = new PictureCallback() {
      public void onPictureTaken(byte[] data, Camera camera) {
       FileOutputStream outStream = null;
      try {
    
       long d=System.currentTimeMillis();
       filename="/sdcard/"+d+".jpg";
    
       outStream = new FileOutputStream(
         String.format("/sdcard/%d.jpg", d));
       outStream.write(data);
       outStream.close();
    
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
    
      }
      CameraFront.this.finish();
      //ImageViewer to Show Image taken from camera
      Intent intent=new Intent(CameraFront.this,ImageViewer.class);
      intent.putExtra("imagefile", filename);
      startActivity(intent);
     }
    };
    }
    
    

  3. XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <FrameLayout
            android:id="@+id/preview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >
        </FrameLayout>
    
        <LinearLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:weightSum="1.0" >
    
            <Button
                android:id="@+id/buttonClick"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="0.5"
                android:text="Click" >
            </Button>
    
            <Button
               android:id="@+id/buttonClose"
               android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="0.5"
               android:text="Close" >
            </Button>
        </LinearLayout>
    
    </LinearLayout>
    
    

  4. It will store image in sdcard