Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the camera on Android phones?

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!

like image 704
George Avatar asked Sep 23 '10 23:09

George


1 Answers

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.

like image 162
UpCat Avatar answered Oct 18 '22 22:10

UpCat