views:

394

answers:

0

Hi, I want to write a Android Program for Camera that captures pictures. I written code in such a way when I click a button, it should be captured, but noting is happeneing. Button click happens , but control doesnot come to "onPictureTaken". I am pasting my code here:

Activity

package com.andr.sai.camerademo;

import java.io.IOException;

import android.app.Activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
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.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;

public class camerademo extends Activity implements SurfaceHolder.Callback 
 {
    /** Called when the activity is first created. */
 SurfaceView mSurfaceView ;
 SurfaceHolder    mSurfaceHolder;
 boolean mPreviewRunning;
 Camera  mCamera;
 Bitmap bitmap = null; 
 Button b1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.camera_surface);
        b1=(Button)findViewById(R.id.b1);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        b1.setOnClickListener(new OnClickListener()
        {

  @Override
   public void onClick(View arg0) {
   System.out.println("111111111111111111111111");
   Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() 
   {


    public void onPictureTaken(byte[]imageData, Camera arg1) {
     System.out.println("2222222222222222222222222222222222222");
     BitmapFactory.Options opt = new BitmapFactory.Options(); 
                 bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, opt); 


    }

   };


   }
        }
        );


    }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int arg1, int w, int h) {
  if (mPreviewRunning) {
   mCamera.stopPreview();
   }
   Camera.Parameters p = mCamera.getParameters();
   p.setPreviewSize(w, h);
   mCamera.setParameters(p);
   try {
   mCamera.setPreviewDisplay(holder);
   } catch (IOException e) {
   e.printStackTrace();
   }
   mCamera.startPreview();
   mPreviewRunning = true;

 }

 @Override
 public void surfaceCreated(SurfaceHolder arg0) {
  mCamera = Camera.open(); 

 }

 @Override
 public void surfaceDestroyed(SurfaceHolder arg0) {
  mCamera.stopPreview();
  mPreviewRunning = false;
  mCamera.release();



 }




 public void onDraw(Canvas c)
  {

  c.drawColor(Color.WHITE);
  c.drawBitmap( bitmap, 10, 10, null);

 }

}

camera_surface.xml

<?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:id="@+id/surface_camera"
   android:layout_width="fill_parent" 
   android:layout_height="10dip"
   android:layout_weight="1"
> 
</SurfaceView> 

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Gettt"
   android:id="@+id/b1"

></Button>

</LinearLayout>

In debug mode I ran this one and I am getting the following debug strings:

08-24 17:43:41.293: ERROR/MediaPlayerService(30):   error: -2
08-24 17:43:41.303: ERROR/MediaPlayer(30): Unable to to create media player
08-24 17:43:41.303: ERROR/CameraService(30): Failed to load CameraService sounds.
08-24 17:43:41.334: ERROR/MediaPlayerService(30):   error: -2
08-24 17:43:41.344: ERROR/MediaPlayer(30): Unable to to create media player
08-24 17:43:41.344: ERROR/CameraService(30): Failed to load CameraService sounds.
08-24 17:43:41.354: DEBUG/CameraService(30): Client::Client X (pid 421)
08-24 17:43:41.364: DEBUG/CameraService(30): CameraService::connect X
08-24 17:43:41.364: DEBUG/CameraService(30): getParameters(picture-format=jpeg;picture-size=213x350;preview-format=yuv422sp;preview-frame-rate=15;preview-size=176x144)
08-24 17:43:41.445: DEBUG/CameraService(30): setParameters(preview-size=320x432;preview-format=yuv422sp;picture-size=213x350;picture-format=jpeg;preview-frame-rate=15)
08-24 17:43:41.445: DEBUG/CameraHardwareStub(30): initHeapLocked: preview size=320x432
08-24 17:43:41.455: DEBUG/CameraService(30): setPreviewDisplay(0xe1f0) (pid 421)
08-24 17:43:41.464: DEBUG/CameraService(30): startPreview (pid 421)
08-24 17:43:41.464: DEBUG/CameraService(30): startCameraMode(0) (pid 421)
08-24 17:43:41.464: DEBUG/CameraService(30): startPreviewMode (pid 421)

Can u help me how to run Sample camera program in Android , to take pictures.