您可以使用 Glass 相机拍摄图片和视频,还可以针对各种不同的用例显示相机的预览流。
概览
拍摄图片或视频的方法有两种:
- 使用
startActivityForResult()
调用内置相机 activity。请尽可能使用此选项。 使用 Android Camera API 构建您自己的逻辑。如果您使用此方法,请遵循以下准则:
- 像 Glass 一样,在点击相机按钮时拍摄照片,在长按时拍摄视频。
- 告知用户是拍摄照片还是录制视频。
- 在拍摄期间让屏幕保持开启状态。
与 Glass 系统共享相机
如果您的 Glassware 使用 Android API 访问摄像头,如果用户按下硬件摄像头按钮,请尽可能暂时释放摄像头。
替换 activity 中的
onKeyDown()
方法,并拦截KEYCODE_CAMERA
以处理按下摄像头按钮的操作。松开相机并返回
false
,表示您未使用事件,这样内置 Glass 相机就会启动。
拍摄图片或视频后,Glass 会返回到您的 activity,您可以在其中在
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. }
拍摄图片或视频
图片
如需使用内置的 Camera Glassware 拍摄图片,请执行以下操作:
- 调用
startActivityForResult(Intent, int)
并将操作设置为ACTION_IMAGE_CAPTURE
。 - 在
onActivityResult(int, int, android.content.Intent)
中:- 确保
requestCode
与启动图片拍摄 intent 时使用的请求代码一致。 - 确保
resultCode
与RESULT_OK
匹配。 - 如有必要,使用
EXTRA_THUMBNAIL_FILE_PATH
键从Intent
extra 获取图片缩略图的路径。 - 可以使用
EXTRA_PICTURE_FILE_PATH
键的Intent
extra 提供完整映像的路径。当图片拍摄 intent 将控制权返还给 Glass 时,图片可能未完全写入文件。验证该映像文件是否存在,或使用FileObserver
监控其父目录。当完整图片可用时,加载该文件并在您的 Glassware 中使用它。
- 确保
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();
}
}
视频
如需使用内置的 Camera Glassware 拍摄视频,请执行以下操作:
- 调用
startActivityForResult(Intent, int)
并将操作设置为ACTION_VIDEO_CAPTURE
。 - 在
onActivityResult(int, int, android.content.Intent)
中:- 确保
requestCode
与启动视频拍摄 intent 时使用的请求代码一致。 - 确保
resultCode
与RESULT_OK
匹配。 - 根据需要使用
EXTRA_THUMBNAIL_FILE_PATH
键从Intent
extra 获取视频缩略图的路径,以便显示预览(如有必要)。 - 包含
EXTRA_VIDEO_FILE_PATH
键的Intent
extra 中提供了录制视频的路径。
- 确保