10

I'm creating an Android application which uses user captured images as part of a larger process. So far my XML layout has a SurfaceView and Button inside a RelativeLayout. I've managed to get the camera preview to show on the SurfaceView but I'm stuck on how to take a picture and save it when a user presses the button.

My class file looks something like the CameraPreview API demo: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

Ideally, when the button it pressed the camera should autofocus, snap a picture (with the clicky sound), save it to /data/data/app_package_structure/files/file_name.jpg, then pop up a Toast to tell the user their image has been saved.

Any help is much appreciated :)

greenie
  • 1,732
  • 4
  • 18
  • 33

2 Answers2

12

I think CommonsWare has really already answered most of this question, but this might work for the auto focus and the shutter sound. This is a guess, since I'm not at a machine where I can compile/test any of this.

In your button-press-handling code, I believe you should call (possibly by message passing)

camera.autoFocus(new Camera.AutoFocusCallback() {
  Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {
      // Play your sound here.
    }
  };
  public void onAutoFocus(boolean success, Camera camera) {
    camera.takePicture(shutterCallback, null, photoCallback);
  }
});  

where camera is your camera object, and photoCallback is the same as in CommonsWare's example.

Exactly what is it that you are stuck on?

Oh, and don't forget to add the <uses-feature> tag android.hardware.camera.autofocus. :)

Mikael Ohlson
  • 3,074
  • 1
  • 22
  • 28
  • This is excellent help. My autofocus is working great now. I've just got one hurdle to get over, saving the picture. What is returned by takePicture(), what do I do with it in photoCallback and how do I save that result as an image? – greenie May 07 '10 at 15:40
  • 1
    Well, my guess is that `takePicture` returns nothing, instead the picture is passed as a JPEG format byte-array to photoCallback via its `onPictureTaken(byte[] data, Camera camera)` callback method. You probably only have to write those bytes straight to file. – Mikael Ohlson May 11 '10 at 17:12
  • At least that was my impression from http://developer.android.com/reference/android/hardware/Camera.PictureCallback.html and http://developer.android.com/reference/android/hardware/Camera.html#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29 – Mikael Ohlson May 11 '10 at 17:14
  • The shutter sound should be in the shutterCallback that's passed into takePicture. Thanks for the tip Mikael! – just_another_coder Aug 03 '10 at 11:49
9

Here is a sample application that handles the take-a-picture-and-save-it part. Auto-focus, clicky, Toast, and saving to the app-local file store vs. the SD card are left as exercises for the student. :-)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for that sameple (which I've already seen before) but I'm really looking for a more complete answer to my question. – greenie Apr 26 '10 at 13:43
  • @CommonsWare: Just an update - I was looking at SurfaceHolder in the developer docs and it is stated that the method setType has been deprecated and so are the constants SURFACE_TYPE_PUSH_BUFFERS, SURFACE_TYPE_NORMAL, SURFACE_TYPE_HARDWARE and SURFACE_TYPE_GPU. – Abhijit Nov 23 '11 at 17:32
  • @Abhijit: They were deprecated as of Android 3.0. You need them for Android 1.x and 2.x. They can be skipped for Android 3.x and higher. – CommonsWare Nov 23 '11 at 17:35
  • @CommonsWare:Thank you for the clarification! – Abhijit Nov 23 '11 at 17:42
  • @Commons i used same code and it works fine except in samsung galaxy s4. – Sagar Maiyad Aug 23 '13 at 04:53