As with most media capture capabilities available on Android, we can use an Intent to leverage the the built-in application. To use the built-in camera application, we create an Intent like this:
// Path to where we want the file and what to call it String imageFilePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfavoritepicture.jpg"; // Create a File object out of that File imageFile = new File(imageFilePath); // Create a Uri out of that android.net.Uri imageFileUri = android.net.Uri.fromFile(imageFile); // Create the Intent that triggers the camera android.content.Intent i = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); // Tell the camera application where we want the resulting image saved i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); // Start the Camera startActivityForResult(i, CAMERA_RESULT);
To get the resulting image, we need to implement an onActivityResult method in our sketch:
PImage cameraImage;
protected void onActivityResult(int requestCode, int resultCode, android.content.Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
// We know the location via the imageFilePath String so load it into a standard Processing PImage
cameraImage = loadImage(imageFilePath);
}
Here is a full example:
final int CAMERA_RESULT = 0;
String imageFilePath;
File imageFile;
android.net.Uri imageFileUri;
PImage cameraImage;
void setup() {
imageFilePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfavoritepicture.jpg";
imageFile = new File(imageFilePath);
imageFileUri = android.net.Uri.fromFile(imageFile);
}
void draw() {
if (cameraImage != null) {
image(cameraImage,0,0,width,height);
}
}
void mousePressed() {
android.content.Intent i = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, CAMERA_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, android.content.Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
cameraImage = loadImage(imageFilePath);
}
}