Camera

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.

  1. Override the onKeyDown() method in your activity and intercept KEYCODE_CAMERA to handle camera button presses.

  2. Release the camera and return false to indicate that you did not consume the event so that the built-in Glass camera starts.

  1. 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

When using an ACTION_IMAGE_CAPTURE intent to take a picture, the image file may not be ready by the time onActivityResult is called. To handle this, you should use a FileObserver to monitor the parent directory where the image file will be saved and wait until the file has been completely written before trying to access it.

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 picturePath = data.getStringExtra(
                CameraManager.EXTRA_PICTURE_FILE_PATH);
        processPictureWhenReady(picturePath);
    }

    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()) {
            // Protect against additional pending events after CLOSE_WRITE 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 = (event == FileObserver.CLOSE_WRITE
                            && 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();
    }
}

See the CameraManager Javadoc for information on capturing media.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.