I'm being frustrated by something that should very simple: taking a picture using Android's camera API (with no preview). I've been following several answers from stackoverflow, but with no success.
Initially I followed this answer (@sush): How to take pictures from the camera without preview when my app starts?
But the PictureCallback was never called. I then followed this answer (@Alex Cohn):
You correctly found that takePicture() should not be called before startPreview() or immediately after it. (onPictureTaken never called)
Which led me to try three solutions.
Solution 1
The first solution places a callback in SurfaceHolder:
public class TestActivity extends Activity
{
private Camera mCamera;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takeSnapShot();
}
@Override
protected void onPause()
{
clearCamera();
super.onPause();
}
private void takeSnapShot()
{
if (mCamera == null)
{
try { mCamera = Camera.open(); }
catch (Exception e) { e.printStackTrace(); }
SurfaceView surface = (SurfaceView) findViewById(R.id.preview);
surface.getHolder().addCallback(mSurfaceHolderCallback);
try { mCamera.setPreviewDisplay(surface.getHolder()); }
catch (IOException e) { e.printStackTrace(); }
mCamera.startPreview();
}
}
private final SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback()
{
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
mCamera.takePicture(null, null, mPictureCallback);
}
catch (IOException e) { e.printStackTrace(); }
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
};
private final Camera.PictureCallback mPictureCallback = new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
FileOutputStream outStream = null;
try
{
File dir_path =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File pictureFile = new File(dir_path, "snapshot-test.jpg");
outStream = new FileOutputStream(pictureFile);
outStream.write(data);
outStream.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
finally { clearCamera(); }
}
};
private void clearCamera()
{
if (mCamera != null)
{
try
{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
catch (final Exception e) { e.printStackTrace(); }
}
}
This first solution works if I call takeSnapShot just once. Unfortunately, I can't get mSurfaceHolderCallback to run SurfaceCreated multiple times.
Solution 2
My second solution was to use PreviewCallback. In this solution I've re-used onPause:
public class TestActivity extends Activity
{
private Camera mCamera;
private boolean mReadyToTakePicture;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takeSnapShot();
}
private void takeSnapShot()
{
if (mCamera == null)
{
try { mCamera = Camera.open(); }
catch (Exception e) { e.printStackTrace(); }
mCamera.setPreviewCallback(mRawPreviewCallback);
SurfaceView surface = (SurfaceView) findViewById(R.id.preview);
try { mCamera.setPreviewDisplay(surface.getHolder()); }
catch (IOException e) { e.printStackTrace(); }
mCamera.startPreview();
}
}
private final Camera.PreviewCallback mRawPreviewCallback = new Camera.PreviewCallback()
{
@Override
public void onPreviewFrame(byte [] rawData, Camera camera)
{
int rawDataLength = 0;
if (rawData != null) { rawDataLength = rawData.length; }
if (rawDataLength > 0 && !mReadyToTakePicture)
{
mReadyToTakePicture = true;
mCamera.takePicture(null, null, mPictureCallback);
}
else { mReadyToTakePicture = false; }
}
};
private final Camera.PictureCallback mPictureCallback = new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
FileOutputStream outStream = null;
mReadyToTakePicture = false;
try
{
File dir_path =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File pictureFile = new File(dir_path, "snapshot-test.jpg");
outStream = new FileOutputStream(pictureFile);
outStream.write(data);
outStream.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
finally { clearCamera(); }
}
};
private void clearCamera()
{
if (mCamera != null)
{
try
{
mReadyToTakePicture = false;
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
catch (final Exception e) { e.printStackTrace(); }
}
}
}
In this solution both mRawPreviewCallback and mPictureCallback are never called.
Solution 3
My last example is using an AsyncTask that waits for a while before actually taking a picture. Hopefully this would give enough time for the preview to be set. In this solution I've re-used onPause and clearCamera:
public class TestActivity extends Activity
{
private Camera mCamera;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takeSnapShot();
}
private void takeSnapShot()
{
if (mCamera == null)
{
try { mCamera = Camera.open(); }
catch (Exception e) { e.printStackTrace(); }
SurfaceView surface = (SurfaceView) findViewById(R.id.preview);
try { mCamera.setPreviewDisplay(surface.getHolder()); }
catch (IOException e) { e.printStackTrace(); }
mCamera.startPreview();
new CameraTask().execute(mCamera);
}
}
private class CameraTask extends AsyncTask<Camera, Void, Void>
{
protected String doInBackground(Camera... cameras)
{
try { Thread.sleep(1500); } catch (InterruptedException e)
{ e.printStackTrace(); }
mCamera = cameras[0];
mCamera.takePicture(null, null, mPictureCallback);
return mPicturePath;
}
private final Camera.PictureCallback mPictureCallback = new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
FileOutputStream outStream = null;
try
{
File dir_path =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File pictureFile = new File(dir_path, "snapshot-test.jpg");
mPicturePath = pictureFile.getAbsolutePath();
outStream = new FileOutputStream(pictureFile);
outStream.write(data);
outStream.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
finally { clearCamera(); }
}
};
}
}
As with solution 2, onPictureTaken is never called.
In all examples the layout of my SurfaceView is as follows (for no preview):
<SurfaceView
android:id="@+id/preview"
android:layout_width="1dp"
android:layout_height="1dp"></SurfaceView>
At this point I'm running out of ideas. My first example was successful in taking one picture, so if I could at least force the app to run surfaceCreated multiple times, it should work. Any ideas?