你可以使用 Glass 相機拍攝圖片和影片 顯示相機的預覽串流畫面, 不同用途的對話
總覽
拍攝圖片或影片的方法有兩種:
- 使用以下應用程式呼叫內建攝影機活動:
startActivityForResult()
。 請盡可能使用這個選項。 使用以下程式碼建構自己的邏輯: Android Camera API。 如果您使用這個方法,請按照下列指南操作:
- 點擊相機按鈕就能拍照,長按影片,就像 Glass 一樣。
- 告知使用者是拍照或錄影。
- 拍攝期間保持螢幕開啟。
與 Glass 系統分享相機
如果您的 Glassware 使用 Android API 存取相機, 於使用者按下硬體設備時,暫時釋放相機 相機按鈕。
覆寫
onKeyDown()
敬上 方法與攔截KEYCODE_CAMERA
。 處理相機按鈕的按下動作。放開相機並傳回
false
,表示您並未使用 事件,讓內建 Glass 攝影機啟動。
該圖片或影片拍攝完成後,Glass 也會回到 例如在活動中回收攝影機
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
與 啟動圖片擷取意圖 - 確認
resultCode
與RESULT_OK
相符。 - 從
Intent
的 額外EXTRA_THUMBNAIL_FILE_PATH
。 。 - 完整映像檔的路徑
Intent
的 額外EXTRA_PICTURE_FILE_PATH
。 鍵。當圖片擷取意圖將控制項傳回 Glassware,圖片可能無法完整寫入檔案。驗證 圖片檔案存在,或使用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();
}
}
影片
如何使用內建相機玻璃軟體拍攝影片:
- 致電
startActivityForResult(Intent, int)
,並將動作設為ACTION_VIDEO_CAPTURE
。 - 在
onActivityResult(int, int, android.content.Intent)
中:- 確認
requestCode
與 當使用者啟動影片擷取意圖時 - 確認
resultCode
與RESULT_OK
相符。 - 從
Intent
的 額外EXTRA_THUMBNAIL_FILE_PATH
。 鍵,視需要顯示預覽畫面。 - 您可以從以下位置取得錄影內容的路徑:
Intent
的 額外EXTRA_VIDEO_FILE_PATH
。 鍵。
- 確認