[关闭]
@chopsticks 2015-09-23T15:11:23.000000Z 字数 2954 阅读 2267

Android下Camera应用的开发

Android Development


Android下Camera应用的开发

tags:Android, Development


Android上的应用开发主要参考Google官方关于Camera的开发说明,其中包含了关于TakePicture和CaptureVideo的示例代码片段

要点:必须使用Preview来实现capture picture或video

1. Camera

2. 基本流程

2.1 创建一个实现了SurfaceHolderCallback的类CameraPreview

  1. 在该SurfaceHolder中处理所有有关Preivew和Camera相关的事情。需要定义:

    • Camera的SingleInstance:管理Camera对象,及其拍照等Camera的功能
    • SurfaceHolder的实例,管理当前的Preview的SurfaceHolder
    • Camera.PictureCallback的对象,用来作为Camera.takePicture时的参数,takePicture之后的处理都需要在PictureCallback中处理。
  2. 实现SurfaceHolder的回调--SurfaceHolderCallback,以便自动处理Holder发生变化的一些event。

具体代码如下:

  1. public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
  2. public final String TAG = "CameraPreview";
  3. private Camera mCamera = null;
  4. private CameraUtil mCameraUtil;
  5. private SurfaceHolder mSurfaceHolder;
  6. private Camera.PictureCallback mPicture;
  7. CameraPreview(Context context){
  8. super(context);
  9. mCameraUtil = new CameraUtil(context);
  10. mSurfaceHolder = getHolder();
  11. mSurfaceHolder.addCallback(this);
  12. mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  13. mPicture = new Camera.PictureCallback() {
  14. @Override
  15. public void onPictureTaken(byte[] data, Camera camera) {
  16. //process image data
  17. ...
  18. //start preview again
  19. camera.startPreview();
  20. }
  21. };
  22. }
  23. @Override
  24. public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h){
  25. if(mSurfaceHolder.getSurface()==null){
  26. //preview surface doesnot exist
  27. return;
  28. }
  29. try {
  30. mCamera.stopPreview();
  31. }catch (Exception e){
  32. Log.i(TAG, "STOP preview ERROR:"+e.getMessage());
  33. }
  34. // set preview size and make any resize, rotate or
  35. // reformatting changes here
  36. try {
  37. mCamera.setPreviewDisplay(mSurfaceHolder);
  38. mCamera.startPreview();
  39. }catch (Exception e){
  40. Log.i(TAG, "start preview error: "+e.getMessage());
  41. }
  42. }
  43. @Override
  44. public void surfaceCreated(SurfaceHolder surfaceHolder){
  45. try {
  46. //open camera
  47. mCamera = mCameraUtil.getCameraInstance();
  48. }catch (Exception e){
  49. Log.i(TAG, "Camera open failed!"+ e.getMessage());
  50. return;
  51. }finally {
  52. if(mCamera == null) {
  53. Log.i(TAG, "open camera failed");
  54. return;
  55. }
  56. }
  57. try {
  58. //set preview
  59. mCamera.setPreviewDisplay(mSurfaceHolder);
  60. //start preview
  61. mCamera.startPreview();
  62. }catch (Exception e){
  63. Log.i(TAG, "error setting camera preview:"+e.getMessage());
  64. }
  65. }
  66. @Override
  67. public void surfaceDestroyed(SurfaceHolder surfaceHolder){
  68. mCamera.stopPreview();
  69. mCamera.release();
  70. mCamera = null;
  71. Log.i(TAG, "preview destroyed");
  72. }
  73. public void takePicture(){
  74. mCamera.takePicture(null,null, mPicture);
  75. Log.i(TAG, "picture taked!");
  76. }
  77. public Camera getCamera(){
  78. return mCamera;
  79. }
  80. ...
  81. }
<需要注意>
  1. mPicture = new Camera.PictureCallback() {
  2. @Override
  3. public void onPictureTaken(byte[] data, Camera camera){
  4. //image data process
  5. ...
  6. //start preview again
  7. mCamera.startPreview();
  8. }
  9. };

2.2 设置界面

  1. 在需要显示Preview的Activity上创建一个Frame。
  2. 并将继承了SurfaceView的CameraView绑定到Frame上。

    1. mCameraPreview = new CameraPreview(this);
    2. cameraFrame.addView(mCameraPreview);
  3. 通过Button调用takePicture来拍照

    1. mCameraPreview.takePicture()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注