谷歌眼镜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文档来了解更多。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值