how to access camera.java in on cick event?

Posted by Srikanth Naidu on Stack Overflow See other posts from Stack Overflow or by Srikanth Naidu
Published on 2010-05-25T10:44:18Z Indexed on 2010/05/25 12:11 UTC
Read the original article Hit count: 209

Filed under:

hi ,

i am making a app which takes photo on button click

i have camera.java which operates camera and takes photo

how to i call it on the below event?

public void onClick(DialogInterface arg0, int arg1) {

setContentView(R.layout.startcamera);

            }

Camera .java

package neuro.com;

import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;

import android.app.Activity; 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.View.OnClickListener; import android.widget.Button; import android.widget.FrameLayout;

public class CameraDemo extends Activity { private static final String TAG = "CameraDemo"; Camera camera; Preview preview; Button buttonClick;

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

    preview = new Preview(this);
    ((FrameLayout) findViewById(R.id.preview)).addView(preview);

    buttonClick = (Button) findViewById(R.id.buttonClick);
    buttonClick.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
        }
    });

    Log.d(TAG, "onCreate'd");
}


ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.d(TAG, "onShutter'd");
    }
};

/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(TAG, "onPictureTaken - raw");
    }
};

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream outStream = null;
        try {
            // write to local sandbox file system

// outStream = CameraDemo.this.openFileOutput(String.format("%d.jpg", System.currentTimeMillis()), 0); // Or write to sdcard outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data); outStream.close(); Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } Log.d(TAG, "onPictureTaken - jpeg"); } };

}

© Stack Overflow or respective owner

Related posts about android