Now all I want is an app that just takes the picture from the camera upon launching. No layout, no camera preview...nothing, just a simple app icon to launch the app. Once l
The camera preview is just a special kind of View in your layout, so if you don't want to display it but still use your existing code without a lot of modifications, there are some tricks you can do.
For example, in your layout file where you're adding the camera preview SurfaceView, you can just add another view on top of it to block the preview. That way you'll still be able to use your existing code without modifying too much and the camera preview will work just like it did before, but you'll have the effect you're looking for which is not showing the camera preview.
For those who are looking for doing the same thing, but with CAMERA 2 API, you could check my answer here. Capture picture without preview using camera2 API.
As of API-Level 11 the SurfaceTexture was added. With it a SurfaceView is no longer needed. I tested the following code with my Samsung Galaxy S3 Neo.
mCamera = Camera.open();
try {
mCamera.setPreviewTexture(new SurfaceTexture(10));
} catch (IOException e1) {
Log.e(Version.APP_ID, e1.getMessage());
}
Parameters params = mCamera.getParameters();
params.setPreviewSize(640, 480);
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
params.setPictureFormat(ImageFormat.JPEG);
mCamera.setParameters(params);
mCamera.startPreview();
mCamera.takePicture(null, null, null, new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(Version.APP_ID, "picture-taken");
}
});
I've tried exactly what you need before and after alot of research found out that for some reason you must have a preview started and even showing that content in a SurfaceView to be able to get the picture, apparently the system behind the scenes flushes whatever is in the buffer for the SurfaceView rendering the preview, now what I did to overcome this issue (taking the picture without preview) was just to set the SurfaceView in my layout to 1dp width and 1dp height (notice it has to be 1dp, 0dp will cause a crash).
<SurfaceView
android:layout_width="1dp"
android:layout_height="1dp"/>
It definitely might not be a very elegant way to get rid of the "preview" Image but it definitely does the trick and left me room to do everything else I had in mind.
Hope this helps...
Regards
Use below code. Its Tested and Worked for me. Any issues please feel free to put comments.
private void takeSnapShots()
{
Toast.makeText(getApplicationContext(), "Image snapshot Started",Toast.LENGTH_SHORT).show();
// here below "this" is activity context.
SurfaceView surface = new SurfaceView(this);
Camera camera = Camera.open();
try {
camera.setPreviewDisplay(surface.getHolder());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
camera.takePicture(null,null,jpegCallback);
}
/** picture call back */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera)
{
FileOutputStream outStream = null;
try {
String dir_path = "";// set your directory path here
outStream = new FileOutputStream(dir_path+File.separator+image_name+no_pics+".jpg");
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally
{
camera.stopPreview();
camera.release();
camera = null;
Toast.makeText(getApplicationContext(), "Image snapshot Done",Toast.LENGTH_LONG).show();
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};