Пример #1
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;
      }
    }
  }
Пример #2
0
  public static void configureCameraParameters(Context context, int rotation) {
    Parameters cameraParams = mCamera.getParameters();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) { // for 2.1 and
      // before
      if (isPortrait(context)) {
        cameraParams.set(CAMERA_PARAM_ORIENTATION, CAMERA_PARAM_PORTRAIT);
        mCameraAngle = 90;
      } else {
        cameraParams.set(CAMERA_PARAM_ORIENTATION, CAMERA_PARAM_LANDSCAPE);
        mCameraAngle = 0;
      }
    } else { // for 2.2 and later
      switch (rotation) {
        case Surface.ROTATION_0: // This is display orientation
          mCameraAngle = 90; // This is camera orientation
          break;
        case Surface.ROTATION_90:
          mCameraAngle = 0;
          break;
        case Surface.ROTATION_180:
          mCameraAngle = 270;
          break;
        case Surface.ROTATION_270:
          mCameraAngle = 180;
          // image
          break;
        default:
          mCameraAngle = 90;
          break;
      }
      Log.d(LogUtil.TAG, "angle: " + mCameraAngle);
      mCamera.setDisplayOrientation(mCameraAngle);
    }

    cameraParams.setRecordingHint(true);
    mCamera.setParameters(cameraParams);
  }