Android 使用CameraAPI自制相机应用程序

Android api之CameraAPI

    • 1.在AndroidManifest.xml中添加使用camera所需的权限。
    • 2.创建一个预览类——创建一个扩展SurfaceView并实现SurfaceHolder接口的camera预览类。这个类从摄像机预览实时图像。
    • 3.构建一个预览布局——一旦你有了camera Preview类,创建一个包含预览和用户界面控件的视图布局。
    • 4.设置监听器的捕捉-连接监听器的界面控件,以启动图像或视频捕捉响应用户的行动,如按按钮。
    • 5.设置相机拍照回调,捕获和保存文件。
    • 6.最后我们要在activity销毁时释放camera资源

写一个简单的相机应用学习如何使用Android系统提供给我们的CameraAPI.
Android Api中文学习地址
一共分为六步:

1.在AndroidManifest.xml中添加使用camera所需的权限。

Android 使用CameraAPI自制相机应用程序_第1张图片

2.创建一个预览类——创建一个扩展SurfaceView并实现SurfaceHolder接口的camera预览类。这个类从摄像机预览实时图像。

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
  private SurfaceHolder mHolder;
  private Camera mCamera;

  public CameraPreview(Context context, Camera camera) {
      super(context);
      mCamera = camera;
      mHolder = getHolder();
      mHolder.addCallback(this);
      // deprecated setting, but required on Android versions prior to 3.0
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }

  public void surfaceCreated(SurfaceHolder holder) {
      // The Surface has been created, now tell the camera where to draw the preview.
      try {
          mCamera.setPreviewDisplay(holder);
          mCamera.startPreview();
      } catch (IOException e) {
          Log.d(TAG, "Error setting camera preview: " + e.getMessage());
      }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
      // empty. Take care of releasing the Camera preview in your activity.
  }

  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
      // If your preview can change or rotate, take care of those events here.
      // Make sure to stop the preview before resizing or reformatting it.

      if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
      }

      // stop preview before making changes
      try {
          mCamera.stopPreview();
      } catch (Exception e){
          // ignore: tried to stop a non-existent preview
      }

      // set preview size and make any resize, rotate or
      // reformatting changes here

      // start preview with new settings
      try {
          mCamera.setPreviewDisplay(mHolder);
          mCamera.startPreview();

      } catch (Exception e){
          Log.d(TAG, "Error starting camera preview: " + e.getMessage());
      }
  }
}

3.构建一个预览布局——一旦你有了camera Preview类,创建一个包含预览和用户界面控件的视图布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />

    <Button
        android:id="@+id/button_capture"
        android:text="Capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout>

4.设置监听器的捕捉-连接监听器的界面控件,以启动图像或视频捕捉响应用户的行动,如按按钮。

 Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // get an image from the camera
                        mCamera.takePicture(null, null, mPicture);
                    }
                }
        );

5.设置相机拍照回调,捕获和保存文件。

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null){
                Log.d(TAG, "Error creating media file, check storage permissions: " );
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }
    };

6.最后我们要在activity销毁时释放camera资源

 if (mCamera != null){
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }

你可能感兴趣的:(api)