谷歌眼鏡GDK開發指南之Camera

原文鏈接:http://bbs.seacat.cn/thread-903-1-1.html




你可以使用Glass Camera來拍照、錄像和相機預覽。



拍照或錄像你有兩個選擇:


1、通過 startActivityForResult()調用內置的相機activity。
2、使用 Android Camera API 構建你自己的邏輯。有如下一些原則:
在Glass上點擊相機按鈕是拍照,長按是錄像;拍照或錄像的時候要提示用戶。



如果你的Glassware用 Android APIs來訪問攝像頭,當用戶按下物理相機按鍵時臨時釋放Camera.




1、在activity中重寫Onkeydown()方法,並攔截 KEYCODE_CAMERA 來處理相機按下。
2、釋放camera 並返回false,來表明你沒有消耗掉這個事件,以便內置Glass Camera的啓動。




注意:如果你在Onkeydown()方法中返回true,你的activity會消耗了這個事件並且Glass Camera不能啓動。只有在你沒有辦法攔截Camera的使用時才能這樣做(例如你在拍攝連續視頻)。




1、在你拍照或攝像完後,Glass返回到你的activity,你可以在OnResume()中回收Camera。


@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.
}




拍照或攝像

當使用 ACTION_IMAGE_CAPTURE intent來調用拍照時,在onActivityResult回調的時候圖片文件可能還沒被準備好。爲了處理這種情況,你應當使用一個 FileObserver 來監視保存圖片文件的父目錄,並且要等到圖片寫入完成才能試圖去訪問文件。




注意:這些步驟在拍攝視頻時不是必須的。


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();
    }
}



可以查看CameraManager 的java文檔來了解更多。




發佈了24 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章