blankwall
blankwall

Reputation: 353

Trying to get custom android camera working

I have developed a basic camera and am trying to get it working.

Here is the camera code

package com.example.camera;

   import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holder;
 private Camera camera; 
 Object size;

 public CameraView(Context context) {

        super(context);
        holder = this.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        holder.addCallback(this);
}
 public Camera getCamera(){
        return camera;
} 

 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

      Camera.Parameters params = camera.getParameters();//Getting paramaters.

        if(size!=null){ //make sure we don't pull a NullException.
                params.setPreviewSize(width, height);//gotta set the size, since we know it. 
        } 
        camera.setParameters(params);//gotta set the paramaters now. 
        camera.startPreview();//starting the preview.
}

public void surfaceCreated(SurfaceHolder arg0) {
    try{
        camera = Camera.open();//setting the camera up.
        camera.setPreviewDisplay(holder);//making the display our current SurfaceHolder
} catch (IOException e){
        e.printStackTrace();//printing out the error message if it happens.
} 

}

public void surfaceDestroyed(SurfaceHolder arg0) {
    camera.stopPreview();
    camera.release();
    camera = null;


} 

        }

Now Im trying to call the camera from my main method and I just cant seem to make it work

package com.example.camera;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.hardware.Camera;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.ImageView;

public class main {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;


     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            this.imageView = (ImageView)this.findViewById(R.id.imageView1);
            Button photoButton = (Button) this.findViewById(R.id.button1);
            photoButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                     cam = cv.getCamera(); //notice how we used that method? ;)
                     cam.takePicture(null,null,PictureCallback_);

                }
            });
        }
     Camera.PictureCallback PictureCallback_ = new Camera.PictureCallback() {


            public void onPictureTaken(byte[] imageData, Camera c) {

                    InputStream is = new ByteArrayInputStream(imageData);


                    Bitmap bmp = BitmapFactory.decodeStream(is); 
            } 
    } ;

Any help is appreciated. Trying to call the original camera activity getCamera and then get the picture taken and put it into imageView1 any help is appreciated.

Upvotes: 1

Views: 394

Answers (2)

Girish Nair
Girish Nair

Reputation: 5216

I have created a camera library project here you can use this library to call the camera and take pictures and save it or read code on how to use the camera. Sample is given you can check it out

Upvotes: 0

David
David

Reputation: 910

Try to do this in your onClick() method:

cam.autoFocus(new AutoFocusCallback(){
    Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
        public void onShutter() {
          // If you want to play your own sound (otherwise, it plays the sound by default)
        }
    };

    @Override
    public void onAutoFocus(boolean arg0, Camera arg1) {
        cam.takePicture(shutterCallback, null, PictureCallback_);
    }
});

Upvotes: 1

Related Questions