You can use the Glass camera to capture images and video and to also display the camera's preview stream for a variety of different use cases.
Overview
You have two options for capturing images or video:
- Calling the built-in camera activity with
startActivityForResult()
. Use this option when possible. Building your own logic with the Android Camera API. Follow these guidelines if you are using this method:
- Take a picture on a camera button click and a video on a long click, just like Glass does.
- Indicate to the user whether a picture was taken or a video was recorded.
- Keep the screen on during capture.
Sharing the camera with the Glass system
If your Glassware uses the Android APIs to access the camera, temporarily release the camera when possible if users press the hardware camera button.
Override the
onKeyDown()
method in your activity and interceptKEYCODE_CAMERA
to handle camera button presses.Release the camera and return
false
to indicate that you did not consume the event so that the built-in Glass camera starts.
After the image or video capture takes place, Glass returns to your activity, where you can reclaim the camera in
onResume()
.@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_CAMERA) { // Stop the preview and release the camera. // Execute your logic as quickly as possible // so the capture happens quickly. return false; } else { return super.onKeyDown(keyCode, event); } } @Override protected void onResume() { super.onResume(); // Re-acquire the camera and start the preview. }
Capturing images or video
Images
To capture an image using the built-in Camera Glassware:
- Call
startActivityForResult(Intent, int)
with the action set asACTION_IMAGE_CAPTURE
. - In
onActivityResult(int, int, android.content.Intent)
:- Ensure that the
requestCode
matches the request code used when starting the image capture intent. - Ensure that the
resultCode
matchesRESULT_OK
. - Obtain the path to the image's thumbnail from the
Intent
's extra with theEXTRA_THUMBNAIL_FILE_PATH
key, if necessary. - The path to the full image is available from the
Intent
's extra with theEXTRA_PICTURE_FILE_PATH
key. When the image capture intent returns control to your Glassware, the image might not be fully written to file. Verify that the image file exists or use aFileObserver
to monitor its parent directory. When the full image is available, load the file and use it in your Glassware.
- Ensure that the
private static final int TAKE_PICTURE_REQUEST = 1;
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
// TODO: Show the thumbnail to the user while the full picture is being
// processed.
}
super.onActivityResult(requestCode, resultCode, data);
}
private void processPictureWhenReady(final String picturePath) {
final File pictureFile = new File(picturePath);
if (pictureFile.exists()) {
// The picture is ready; process it.
} else {
// The file does not exist yet. Before starting the file observer, you
// can update your UI to let the user know that the application is
// waiting for the picture (for example, by displaying the thumbnail
// image and a progress indicator).
final File parentDirectory = pictureFile.getParentFile();
FileObserver observer = new FileObserver(parentDirectory.getPath(),
FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
// Protect against additional pending events after CLOSE_WRITE
// or MOVED_TO is handled.
private boolean isFileWritten;
@Override
public void onEvent(int event, String path) {
if (!isFileWritten) {
// For safety, make sure that the file that was created in
// the directory is actually the one that we're expecting.
File affectedFile = new File(parentDirectory, path);
isFileWritten = affectedFile.equals(pictureFile);
if (isFileWritten) {
stopWatching();
// Now that the file is ready, recursively call
// processPictureWhenReady again (on the UI thread).
runOnUiThread(new Runnable() {
@Override
public void run() {
processPictureWhenReady(picturePath);
}
});
}
}
}
};
observer.startWatching();
}
}
Videos
To capture a video using the built-in Camera Glassware:
- Call
startActivityForResult(Intent, int)
with the action set asACTION_VIDEO_CAPTURE
. - In
onActivityResult(int, int, android.content.Intent)
:- Ensure that the
requestCode
matches the request code used when starting the video capture intent. - Ensure that the
resultCode
matchesRESULT_OK
. - Obtain the path to the video's thumbnail from the
Intent
's extra with theEXTRA_THUMBNAIL_FILE_PATH
key to display a preview if necessary. - The path to the recorded video is available from the
Intent
's extra with theEXTRA_VIDEO_FILE_PATH
key.
- Ensure that the