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 launched the camera should take the picture.
Here is the preview image, that i do not want to appear in my app. I just want to eanble my app to click the take picture button encircled in the image here:
Once the picture is taken then I need to save it in my photo gallery.
Can someone guide me here? Thanks
Here is some code that I have tried on emulator and device.
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class Main extends Activity implements SurfaceHolder.Callback{
private Camera camera;
private ImageButton cameraClick;
private SurfaceHolder mHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
mHolder = surfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraClick = (ImageButton) findViewById(R.id.cameraClick);
cameraClick.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
camera.takePicture(shutterCallback, rawCallback,jpegCallback);
}
});
}
// Handles when shutter open
ShutterCallback shutterCallback = new ShutterCallback()
{
public void onShutter()
{
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
// we do something using return byte[] of taken image.
}
};
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)
{
// Set camera preview size,orientation,rotation using parameters
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
camera.stopPreview();
camera.release();
camera = null;
}
}
Now I am able to hear the picture click on the button click both on the emulator and the device. Now I want to save the picture as well. Any clue?