谷歌眼镜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。



[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Override

  2. publicboolean onKeyDown(int keyCode, KeyEvent event) {  

  3. if (keyCode == KeyEvent.KEYCODE_CAMERA) {  

  4. // Stop the preview and release the camera.

  5. // Execute your logic as quickly as possible

  6. // so the capture happens quickly.

  7. returnfalse;  

  8.    } else {  

  9. returnsuper.onKeyDown(keyCode, event);  

  10.    }  

  11. }  

  12. @Override

  13. protectedvoid onResume() {  

  14. super.onResume();  

  15. // Re-acquire the camera and start the preview.

  16. }  





拍照或摄像

当使用 ACTION_IMAGE_CAPTURE intent来调用拍照时,在onActivityResult回调的时候图片文件可能还没被准备好。为了处理这种情况,你应当使用一个 FileObserver 来监视保存图片文件的父目录,并且要等到图片写入完成才能试图去访问文件。




注意:这些步骤在拍摄视频时不是必须的。



[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. privatestaticfinalint TAKE_PICTURE_REQUEST = 1;  

  2. privatevoid takePicture() {  

  3.    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  

  4.    startActivityForResult(intent, TAKE_PICTURE_REQUEST);  

  5. }  

  6. @Override

  7. protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {  

  8. if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {  

  9.        String picturePath = data.getStringExtra(  

  10.                CameraManager.EXTRA_PICTURE_FILE_PATH);  

  11.        processPictureWhenReady(picturePath);  

  12.    }  

  13. super.onActivityResult(requestCode, resultCode, data);  

  14. }  

  15. privatevoid processPictureWhenReady(final String picturePath) {  

  16. final File pictureFile = new File(picturePath);  

  17. if (pictureFile.exists()) {  

  18. // The picture is ready; process it.

  19.    } else {  

  20. // The file does not exist yet. Before starting the file observer, you

  21. // can update your UI to let the user know that the application is

  22. // waiting for the picture (for example, by displaying the thumbnail

  23. // image and a progress indicator).

  24. final File parentDirectory = pictureFile.getParentFile();  

  25.        FileObserver observer = new FileObserver(parentDirectory.getPath()) {  

  26. // Protect against additional pending events after CLOSE_WRITE is

  27. // handled.

  28. privateboolean isFileWritten;  

  29. @Override

  30. publicvoid onEvent(int event, String path) {  

  31. if (!isFileWritten) {  

  32. // For safety, make sure that the file that was created in

  33. // the directory is actually the one that we're expecting.

  34.                    File affectedFile = new File(parentDirectory, path);  

  35.                    isFileWritten = (event == FileObserver.CLOSE_WRITE  

  36.                            && affectedFile.equals(pictureFile));  

  37. if (isFileWritten) {  

  38.                        stopWatching();  

  39. // Now that the file is ready, recursively call

  40. // processPictureWhenReady again (on the UI thread).

  41.                        runOnUiThread(new Runnable() {  

  42. @Override

  43. publicvoid run() {  

  44.                                processPictureWhenReady(picturePath);  

  45.                            }  

  46.                        });  

  47.                    }  

  48.                }  

  49.            }  

  50.        };  

  51.        observer.startWatching();  

  52.    }  

  53. }  





可以查看CameraManager 的java文档来了解更多。


你可能感兴趣的:(android,Activity,谷歌,Camera,摄像头)