设置拍摄的肖像,但在风景中播放

时间:2013-04-30 13:26:45

标签: android camera orientation

我在下面有这个代码。问题是我将相机的方向设置为纵向,甚至预览是纵向。但是,当我播放该视频时,我会以横向模式看到它。你们知道我为什么以及如何解决这个问题?有没有办法在播放时旋转VideoView或在录制后自动旋转视频..? 求求你,帮助我,在任何地方我都不知道为什么以及如何解决它...

以下是代码:

public class BetaRecordActivity extends Activity {

private Preview preview;
private Camera camera;
private MediaRecorder recorder;

private int defaultCameraId;
private int videoCount;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.beta_record, menu);
    return true;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_beta_record);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    initCameraAndPreview();
    videoCount = 0;

    preview.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                releaseCamera();
                getThatCamReady();
                recorder.start();
                videoCount ++;
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                stopRecording();
                break;
            }
            return true;
        }
    });

}

@Override
protected void onResume() {
    super.onResume();

}

@Override
protected void onPause() {
    super.onPause();

}

private boolean getThatCamReady() {
    camera = getCameraInstance();
    setCameraDisplayOrientation(this, defaultCameraId, camera);
    camera.unlock();

    recorder = new MediaRecorder();
    recorder.setCamera(camera);

    recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    File tempFile = new File(Environment.getExternalStorageDirectory(), "/rec/temp/video_" + String.valueOf(videoCount) + ".mp4");
    recorder.setOutputFile(tempFile.getPath());
    recorder.setMaxDuration(12500);
    recorder.setMaxFileSize(7000000);

    recorder.setPreviewDisplay(preview.getHolder().getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }

    return true;
}

private void stopRecording() {
    //timer.pause();

     try {
         recorder.stop();
     } catch(RuntimeException stopException) {

         // Handle the cleanup here
         //badList.add(videoCount);
         //videoCount--;
         //RecordActivity.this.timeLeft.setText(String.valueOf(videoCount));
     }

     releaseMediaRecorder();
}

private void initCameraAndPreview() {
     // Find the total number of cameras available
    int numberOfCameras = Camera.getNumberOfCameras();

    // Find the ID of the default camera
    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
            defaultCameraId = i;
        }
    } 

    camera = getCameraInstance();
    setCameraDisplayOrientation(this, defaultCameraId, camera);
    preview = new Preview(this, camera);

    FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
    myCameraPreview.addView(preview);
}

private void releaseMediaRecorder(){
    if (recorder != null) {
        recorder.reset();
        recorder.release();
        recorder = null;
        camera.lock();
    }
}

private void releaseCamera(){
    if (camera != null){
        camera.release();
        camera = null;
    }
}

private Camera getCameraInstance(){
    // TODO Auto-generated method stub
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

public static void setCameraDisplayOrientation(Activity activity,
        int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

}

1 个答案:

答案 0 :(得分:1)

您是否尝试过recorder.setOrientationHint(90); ?