private List<Camera.Size> getSupportedVideoSizes(Camera camera) {
    Camera.Parameters params = camera.getParameters();
    // defer to preview instead of params.getSupportedVideoSizes() http://bit.ly/1rxOsq0
    // but prefer SupportedVideoSizes!
    List<Camera.Size> sizes = params.getSupportedVideoSizes();
    if (sizes != null) {
      return sizes;
    }

    // Video sizes may be null, which indicates that all the supported
    // preview sizes are supported for video recording.
    return params.getSupportedPreviewSizes();
  }
示例#2
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private boolean prepareVideoRecorder() {
    mCamera = tbs.beamr.android.sid.CamHelper.getDefaultCameraInstance();
    final Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    if (sizes == null || sizes.size() < 1) {
      sizes = parameters.getSupportedVideoSizes();
    }

    Log.e("supported sizes > ", getSizeString(sizes));
    final Camera.Size size = getCameraSize(sizes);

    switch (hasAutoFocus(parameters)) {
      case 0:
        break;
      case 1:
        parameters.set("cam_mode", 1);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        break;
      case 2:
        parameters.set("cam_mode", 1);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        break;
    }

    mCamera.setParameters(parameters);
    try {
      mCamera.setPreviewDisplay(mPreview.getHolder());
    } catch (IOException e) {
      Log.e(TAG, "Surface View is unavailable or unsuitable" + e.getMessage());
      return false;
    }
    mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.setDisplayOrientation(90);
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mMediaRecorder.setOrientationHint(90);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    final CamcorderProfile profile =
        CamcorderProfile.get(
            CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)
                ? CamcorderProfile.QUALITY_480P
                : CamcorderProfile.QUALITY_HIGH);
    //        profile.videoFrameRate = 25;
    //            profile.videoBitRate = 7200000;
    mMediaRecorder.setProfile(profile);
    //        if (size == null) {
    //            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    //            final CamcorderProfile profile =
    // CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    ////            profile.videoFrameRate = 30;
    ////            profile.videoBitRate = 7200000;
    //            mMediaRecorder.setProfile(profile);
    //        } else {
    //            // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    //            if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)) {
    //                final CamcorderProfile profile =
    // CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
    //                Log.e("recordingSize > ", String.valueOf(profile.videoFrameWidth) + "x" +
    // String.valueOf(profile.videoFrameHeight));
    //                mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    //                mMediaRecorder.setProfile(profile);
    //            } else {
    //                mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    //                mMediaRecorder.setVideoEncodingBitRate(8200000);
    //                mMediaRecorder.setVideoSize(size.width, size.height);
    ////            mMediaRecorder.setVideoFrameRate(20);
    //                mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    //            }
    //        }

    // Step 4: Set output file
    // Todo
    //        final String name = Environment.getExternalStorageDirectory().getAbsolutePath() +
    // "/tbs_beamR/received_file_" + (new Date(System.currentTimeMillis())).toString() + ".mp4";
    final String name =
        Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/test.mp4";
    final File file = new File(name);
    if (!file.exists()) {
      try {
        file.getParentFile().mkdirs();
        //                file.createNewFile();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    mMediaRecorder.setOutputFile(name);

    // Step 5: Prepare configured MediaRecorder
    try {
      mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
      Log.e(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
      releaseMediaRecorder();
      return false;
    } catch (IOException e) {
      Log.e(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
      releaseMediaRecorder();
      return false;
    }
    return true;
  }
  @Override
  public void openCamera() {
    final Activity activity = getActivity();
    if (null == activity || activity.isFinishing()) return;
    try {
      final int mBackCameraId =
          mInterface.getBackCamera() != null ? (Integer) mInterface.getBackCamera() : -1;
      final int mFrontCameraId =
          mInterface.getFrontCamera() != null ? (Integer) mInterface.getFrontCamera() : -1;
      if (mBackCameraId == -1 || mFrontCameraId == -1) {
        int numberOfCameras = Camera.getNumberOfCameras();
        if (numberOfCameras == 0) {
          throwError(new Exception("No cameras are available on this device."));
          return;
        }

        for (int i = 0; i < numberOfCameras; i++) {
          //noinspection ConstantConditions
          if (mFrontCameraId != -1 && mBackCameraId != -1) break;
          Camera.CameraInfo info = new Camera.CameraInfo();
          Camera.getCameraInfo(i, info);
          if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT && mFrontCameraId == -1) {
            mInterface.setFrontCamera(i);
          } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK && mBackCameraId == -1) {
            mInterface.setBackCamera(i);
          }
        }
      }
      if (getCurrentCameraPosition() == CAMERA_POSITION_UNKNOWN) {
        if (getArguments().getBoolean(CameraIntentKey.DEFAULT_TO_FRONT_FACING, false)) {
          // Check front facing first
          if (mInterface.getFrontCamera() != null && (Integer) mInterface.getFrontCamera() != -1) {
            mButtonFacing.setImageDrawable(VC.get(this, R.drawable.mcam_camera_rear));
            mInterface.setCameraPosition(CAMERA_POSITION_FRONT);
          } else {
            mButtonFacing.setImageDrawable(VC.get(this, R.drawable.mcam_camera_front));
            if (mInterface.getBackCamera() != null && (Integer) mInterface.getBackCamera() != -1)
              mInterface.setCameraPosition(CAMERA_POSITION_BACK);
            else mInterface.setCameraPosition(CAMERA_POSITION_UNKNOWN);
          }
        } else {
          // Check back facing first
          if (mInterface.getBackCamera() != null && (Integer) mInterface.getBackCamera() != -1) {
            mButtonFacing.setImageDrawable(VC.get(this, R.drawable.mcam_camera_front));
            mInterface.setCameraPosition(CAMERA_POSITION_BACK);
          } else {
            mButtonFacing.setImageDrawable(VC.get(this, R.drawable.mcam_camera_rear));
            if (mInterface.getFrontCamera() != null && (Integer) mInterface.getFrontCamera() != -1)
              mInterface.setCameraPosition(CAMERA_POSITION_FRONT);
            else mInterface.setCameraPosition(CAMERA_POSITION_UNKNOWN);
          }
        }
      }

      if (mWindowSize == null) mWindowSize = new Point();
      activity.getWindowManager().getDefaultDisplay().getSize(mWindowSize);
      final int toOpen = getCurrentCameraId();
      mCamera = Camera.open(toOpen == -1 ? 0 : toOpen);
      Camera.Parameters parameters = mCamera.getParameters();
      List<Camera.Size> videoSizes = parameters.getSupportedVideoSizes();
      if (videoSizes == null || videoSizes.size() == 0)
        videoSizes = parameters.getSupportedPreviewSizes();
      mVideoSize = chooseVideoSize((BaseCaptureActivity) activity, videoSizes);
      Camera.Size previewSize =
          chooseOptimalSize(
              parameters.getSupportedPreviewSizes(), mWindowSize.x, mWindowSize.y, mVideoSize);
      parameters.setPreviewSize(previewSize.width, previewSize.height);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) parameters.setRecordingHint(true);
      setCameraDisplayOrientation(parameters);
      mCamera.setParameters(parameters);
      createPreview();
      mMediaRecorder = new MediaRecorder();
    } catch (IllegalStateException e) {
      throwError(new Exception("Cannot access the camera.", e));
    } catch (RuntimeException e2) {
      throwError(
          new Exception("Cannot access the camera, you may need to restart your device.", e2));
    }
  }