示例#1
0
  /** Turns the LED on or off if phone has one. */
  public synchronized void setFlashState(boolean state) {
    // If the camera has already been opened, we apply the change immediately
    if (mCamera != null) {

      if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
        lockCamera();
      }

      Parameters parameters = mCamera.getParameters();

      // We test if the phone has a flash
      if (parameters.getFlashMode() == null) {
        // The phone has no flash or the choosen camera can not toggle the flash
        throw new RuntimeException("Can't turn the flash on !");
      } else {
        parameters.setFlashMode(state ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
        try {
          mCamera.setParameters(parameters);
          mFlashEnabled = state;
        } catch (RuntimeException e) {
          mFlashEnabled = false;
          throw new RuntimeException("Can't turn the flash on !");
        } finally {
          if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
            unlockCamera();
          }
        }
      }
    } else {
      mFlashEnabled = state;
    }
  }
示例#2
0
  protected synchronized void createCamera() throws RuntimeException {
    if (mSurfaceView == null) throw new InvalidSurfaceException("Invalid surface !");
    if (mSurfaceView.getHolder() == null || !mSurfaceReady)
      throw new InvalidSurfaceException("Invalid surface !");

    if (mCamera == null) {
      openCamera();
      mUpdated = false;
      mUnlocked = false;
      mCamera.setErrorCallback(
          new Camera.ErrorCallback() {
            @Override
            public void onError(int error, Camera camera) {
              // On some phones when trying to use the camera facing front the media server will die
              // Whether or not this callback may be called really depends on the phone
              if (error == Camera.CAMERA_ERROR_SERVER_DIED) {
                // In this case the application must release the camera and instantiate a new one
                Log.e(TAG, "Media server died !");
                // We don't know in what thread we are so stop needs to be synchronized
                mCameraOpenedManually = false;
                stop();
              } else {
                Log.e(TAG, "Error unknown with the camera: " + error);
              }
            }
          });

      try {

        // If the phone has a flash, we turn it on/off according to mFlashEnabled
        // setRecordingHint(true) is a very nice optimization if you plane to only use the Camera
        // for recording
        Parameters parameters = mCamera.getParameters();
        if (parameters.getFlashMode() != null) {
          parameters.setFlashMode(
              mFlashEnabled ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
        }
        parameters.setRecordingHint(true);
        mCamera.setParameters(parameters);
        mCamera.setDisplayOrientation(mOrientation);

        try {
          if (mMode == MODE_MEDIACODEC_API_2) {
            mSurfaceView.startGLThread();
            mCamera.setPreviewTexture(mSurfaceView.getSurfaceTexture());
          } else {
            mCamera.setPreviewDisplay(mSurfaceView.getHolder());
          }
        } catch (IOException e) {
          throw new InvalidSurfaceException("Invalid surface !");
        }

      } catch (RuntimeException e) {
        destroyCamera();
        throw e;
      }
    }
  }
  private void showFlashButton(Parameters params) {
    boolean showFlash =
        (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
                && params.getFlashMode() != null)
            && params.getSupportedFlashModes() != null
            && params.getSupportedFocusModes().size() > 1;

    flashCameraButton.setVisibility(showFlash ? View.VISIBLE : View.INVISIBLE);
  }
示例#4
0
  public void switchFlashLight() {
    if (camera != null) {
      Parameters parameter = camera.getParameters();
      if (parameter.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)) {
        parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
      } else {
        parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
      }

      camera.setParameters(parameter);
    }
  }