2017-11-07 7 views
0

cameraを開いて、ビデオを録画するためにSurfaceViewにプレビューを設定します。録画を完了し、別のアクティビティを意図して、キャプチャしたビデオURIをVideoViewに設定します。自動的にカメラのプレビューとは異なります。カメラによるビデオキャプチャの向きが自動的に変化するのはなぜですか?

これは、カメラのプレビュー(私はビデオキャプチャがまったく同じに表示されている期待して)です:

enter image description here

しかし、撮影した映像を終わるには、以下のように表示されます。

enter image description here

ご覧のように、ビデオは風景モードで表示され、プレビューはポートレートモードになります。

private boolean prepareRecorder(){ 

     //# Create a new instance of MediaRecorder 
     mRecorder = new MediaRecorder(); 
     mCamera.unlock(); 
     mRecorder.setCamera(mCamera); 
     mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 

     //# Video settings 
     mRecorder.setVideoSize(1920,1280); 
     mRecorder.setVideoFrameRate(30); 
     mRecorder.setVideoEncodingBitRate(3000000); 
     mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 

     //# Audio settings 
     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
     mRecorder.setAudioEncodingBitRate(16); 
     mRecorder.setAudioSamplingRate(44100); 

     //Max duration 30 seconds 
     mRecorder.setMaxDuration(30000); 

     //set preview display 
     mRecorder.setPreviewDisplay(surfaceHolder.getSurface()); 

     //set the ouput of the Mp4 file 
     MediaFileHelper mediaFileHelper = new MediaFileHelper(); 
     outputFile =mediaFileHelper.getOutputVideoFile(); 
     mRecorder.setOutputFile(outputFile.getAbsolutePath()); 

     try{ 
      mRecorder.prepare(); 
     } catch (IOException e) { 
      Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); 
      releaseMediaRecorder(); 
      return false; 
     }catch (IllegalStateException e){ 
      Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); 
      releaseMediaRecorder(); 
      return false; 
     } 
     return true; 
    } 

は、だから私の質問は次のとおりです:ここではオープンCamera

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    releaseCamera(); 
    cameraId = Camera.CameraInfo.CAMERA_FACING_BACK; 
    try{ 
     mCamera = Camera.open(cameraId); 
    }catch (RuntimeException ex){ 
     ex.printStackTrace(); 
    } 

    Camera.Parameters parameters; 
    parameters = mCamera.getParameters(); 

    List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes(); 
    List<Camera.Size> mSupportedVideoSizes = parameters.getSupportedVideoSizes(); 
    optimalSize = getOptimalVideoSize(mSupportedVideoSizes, 
      mSupportedPreviewSizes, videoSurface.getWidth(), videoSurface.getHeight()); 

    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
    profile.videoFrameWidth = optimalSize.width; 
    profile.videoFrameHeight = optimalSize.height; 

    parameters.setPreviewFrameRate(20); 
    parameters.setPreviewSize(profile.videoFrameWidth,profile.videoFrameHeight); 
    mCamera.setParameters(parameters); 
    mCamera.setDisplayOrientation(90); 

    try{ 
     mCamera.setPreviewDisplay(surfaceHolder); 
     mCamera.startPreview(); 
     inPreview = true; 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

は、私はビデオレコーダーを準備する方法であるとき、ここで

surfaceCreatedのための私のコードです

1)どのようなビデオキャプチャの向きが180度回転する原因になるのは問題ですか?

2)どのようにカメラのプレビューと同じ向きのビデオを表面ビューで撮ることができますか?

ありがとうございます。

答えて

1

一部のデバイスは、キャプチャ後にイメージとビデオを回転します。

public static int getRotation(Context context, Uri imageUri) { 
    String[] columns = {MediaStore.Images.Media.ORIENTATION}; 
    Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null); 
    if (cursor == null) return 0; 

    cursor.moveToFirst(); 

    int orientationColumnIndex = cursor.getColumnIndex(columns[0]); 
    return cursor.getInt(orientationColumnIndex); 
} 

結果が0であるならば、あなたは元にそれを回転させる必要がある誰回転がない:画像が回転しているかどうかを知るために、コードの下に使用します。コードの下に使用します。

public static Bitmap rotate(Bitmap bm, int rotation) { 
    if (rotation != 0) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(rotation); 
     Bitmap bmOut = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); 
     return bmOut; 
    } 
    return bm; 
} 
+0

私は 'getRotation'に' videoUri'を入れる必要がありますか? – ken

+0

はい、ビデオのカスタム最初の関数 –

+0

ですので、2番目の関数の 'rotation'は1番目の関数による戻り値です?? – ken

0

サムスンのようないくつかのデバイスは、我々が回転を取得し、それが必要な場合は、回転させることができ、コードをcapture.But後の画像を回転させます。

public static int getCameraPhotoOrientation(String imagePath) { 
      int rotate = 0; 
      try { 
       File imageFile = new File(imagePath); 
       ExifInterface exif = new ExifInterface(
         imageFile.getAbsolutePath()); 
       int orientation = exif.getAttributeInt(
         ExifInterface.TAG_ORIENTATION, 
         ExifInterface.ORIENTATION_NORMAL); 

       switch (orientation) { 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         rotate = 270; 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         rotate = 180; 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         rotate = 90; 
         break; 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return rotate; 
     } 


we can use this method as below 


    //Rotate if necessary 
int rotate=AppUtil.getCameraPhotoOrientation(imagePath); 
Matrix matrix = new Matrix(); 
matrix.postRotate(rotate); 

// create a file object from path 
File imageFile = new File(path); 

//handle Out of memory error 
Bitmap bmp=AppUtil.decodeFile(imageFile,100,100); 

//Rotate BMP 
Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
+0

兄弟ですが、これはビデオであり、イメージではありません、それはまだ同じですか?? – ken

関連する問題