Taking a picture as fast as possible with Camera API on Android

Alin picture Alin · Aug 14, 2012 · Viewed 8.5k times · Source

Scenario:

I need to take a picture as fast as possible and save it to SD Card. It would be fantastic if I could do it in around 0.2 seconds both taking the picture and saving it.

What I did so far:

As normal I've created a SurfaceView to handle the Camera preview and initialized the camera object. The quality of the image doesn't need to be very high, that's why I am not using the largest resolution possible and also no autofocus is required. I set the parameters like this:

 Parameters parameters = camera.getParameters();
 parameters.set("jpeg-quality", 70);

 parameters.setPictureFormat(ImageFormat.JPEG);
 List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
 Size size = sizes.get(Integer.valueOf((sizes.size()-1)/2)); //choose a medium resolution
 parameters.setPictureSize(size.width, size.height);
 camera.setParameters(parameters);
 camera.setDisplayOrientation(90);

 List<Size> sizes2 = parameters.getSupportedPreviewSizes();
 Size size2 = sizes.get(0);

 parameters.setPreviewSize(size2.width, size2.height);
 camera.setPreviewDisplay(holder);
 camera.startPreview();

I save the image to SD card very simple with:

PictureCallback handlePictureStorage = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;

            try {
                outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();       
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
        }
    };

After making a few tests, on my Galaxy Nexus, the result looks like:

  • Setting picture size to : wigth=1600 height=1200
  • Jpeg quality : 70, Picture format JPEG
  • Fire take picture at: 00:13:23.603
  • Start saving picture on SD Card at: 00:13:23.956
  • Finished saving picture on SD Card at: 00:13:23.990

This is almost 0.4 seconds. Is there a way to tweak the Camera parameters even more to gain some faster speed ? The resolution is OK, the quality of the picture also. I know that there are apps on market that have 30 pictures per second but I think they use buffering to achieve that speed. However, as you may see the biggest time is lost with taking the picture rather than saving it to card. It would be great if I could tweak this a bit more.

Answer

Alin picture Alin · Aug 20, 2012

After I did a bit of testing with multiple parameters, conclusion is that not much is left to be done. Here are some parameters I've set:

  //set color efects to none
 cameraParameters.setColorEffect(Camera.Parameters.EFFECT_NONE);

  //set antibanding to none
 if (cameraParameters.getAntibanding() != null) {
 cameraParameters.setAntibanding(Camera.Parameters.ANTIBANDING_OFF);
 }

 // set white ballance
 if (cameraParameters.getWhiteBalance() != null) {
 cameraParameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
 }

  //set flash
 if (cameraParameters.getFlashMode() != null) {
 cameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
 }

  //set zoom
 if (cameraParameters.isZoomSupported()) {
 cameraParameters.setZoom(0);
 }

 //set focus mode
 cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);

However, the best idea is to get the full string list of parameters supported by the camera, and try to tweak them. To get the string, use the flatten method of Camera.Parameters - http://developer.android.com/reference/android/hardware/Camera.Parameters.html#flatten()

But in order to get images really quick, I had to use preview with buffer, and for each frame taken, try to save it on sd-card in a thread. The picture quality isn't fantastic, but it's a start.