Пример #1
0
  /**
   * Opens the camera and starts sending preview frames to the underlying detector. The preview
   * frames are not displayed.
   *
   * @throws IOException if the camera's preview texture or display could not be initialized
   */
  @RequiresPermission(Manifest.permission.CAMERA)
  public CameraSource start() throws IOException {
    synchronized (mCameraLock) {
      if (mCamera != null) {
        return this;
      }

      mCamera = createCamera();

      // SurfaceTexture was introduced in Honeycomb (11), so if we are running and
      // old version of Android. fall back to use SurfaceView.
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mDummySurfaceTexture = new SurfaceTexture(DUMMY_TEXTURE_NAME);
        mCamera.setPreviewTexture(mDummySurfaceTexture);
      } else {
        mDummySurfaceView = new SurfaceView(mContext);
        mCamera.setPreviewDisplay(mDummySurfaceView.getHolder());
      }
      mCamera.startPreview();

      mProcessingThread = new Thread(mFrameProcessor);
      mFrameProcessor.setActive(true);
      mProcessingThread.start();
    }
    return this;
  }
Пример #2
0
  /**
   * Opens the camera and starts sending preview frames to the underlying detector. The supplied
   * surface holder is used for the preview so frames can be displayed to the user.
   *
   * @param surfaceHolder the surface holder to use for the preview frames
   * @throws IOException if the supplied surface holder could not be used as the preview display
   */
  @RequiresPermission(Manifest.permission.CAMERA)
  public CameraSource start(SurfaceHolder surfaceHolder) throws IOException {
    synchronized (mCameraLock) {
      if (mCamera != null) {
        return this;
      }

      mCamera = createCamera();
      mCamera.setPreviewDisplay(surfaceHolder);
      mCamera.startPreview();

      mProcessingThread = new Thread(mFrameProcessor);
      mFrameProcessor.setActive(true);
      mProcessingThread.start();
    }
    return this;
  }
Пример #3
0
  /**
   * Closes the camera and stops sending frames to the underlying frame detector.
   *
   * <p>This camera source may be restarted again by calling {@link #start()} or {@link
   * #start(SurfaceHolder)}.
   *
   * <p>Call {@link #release()} instead to completely shut down this camera source and release the
   * resources of the underlying detector.
   */
  public void stop() {
    synchronized (mCameraLock) {
      mFrameProcessor.setActive(false);
      if (mProcessingThread != null) {
        try {
          // Wait for the thread to complete to ensure that we can't have multiple threads
          // executing at the same time (i.e., which would happen if we called start too
          // quickly after stop).
          mProcessingThread.join();
        } catch (InterruptedException e) {
          Log.d(TAG, mContext.getString(R.string.processing_interrupted));
        }
        mProcessingThread = null;
      }

      // clear the buffer to prevent oom exceptions
      mBytesToByteBuffer.clear();

      if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.setPreviewCallbackWithBuffer(null);
        try {
          // We want to be compatible back to Gingerbread, but SurfaceTexture
          // wasn't introduced until Honeycomb.  Since the interface cannot use a SurfaceTexture, if
          // the
          // developer wants to display a preview we must use a SurfaceHolder.  If the developer
          // doesn't
          // want to display a preview we use a SurfaceTexture if we are running at least Honeycomb.

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mCamera.setPreviewTexture(null);

          } else {
            mCamera.setPreviewDisplay(null);
          }
        } catch (Exception e) {
          Log.e(TAG, mContext.getString(R.string.failed_to_clear_preview) + e);
        }
        mCamera.release();
        mCamera = null;
      }
    }
  }
Пример #4
0
 /** Stops the camera and releases the resources of the camera and underlying detector. */
 public void release() {
   synchronized (mCameraLock) {
     stop();
     mFrameProcessor.release();
   }
 }