Alex Rivera | Logout

How do I access the camera on Android phones?

Asked 2010-09-23T23:01:07.310
12

I've written a program in Java that takes in an image file and manipulates the image. Now I'm trying to access the camera so that I can take the photo and give it to the image processing program however I'm lost as to how to do this. I've read the information about the camera class and how to ask for permissions but I don't know how to actually take the photo. If anyone has any tips on where I should begin or if they know of a good tutorial I'd really appreciate it. Thanks!

Edit
Report

1 Answer

7

Google is your best friend, here are some tutorials:

Using the camera

How-To Program The Google Android Camera To Take Pictures

Take Picture from Camera Emulator

camera

First edit your AndroidManifest.xml, add the camera permission:

<uses-permission android:name=”android.permission.CAMERA”/>

Camera service has to be opened and closed:

Camera camera = Camera.open();
 //Do things with the camera
camera.release();

You can set camera settings, e.g.:

Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG); 
camera.setParameters(parameters);

To take a picture:

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) {
    // TODO Do something with the image JPEG data.
  }
};

Do not forget to add the camera layout to your main layout xml.

answered 2010-09-24T04:54:01.833

Your Answer