2

我在网上搜索过,但找不到具体的答案。我在手机中使用内置的相机应用程序,在我记录了方向后,方向是 90 度错误。我认为它在相机中,因为当我录制时它始终处于横向模式,但我希望它随着用户旋转手机而旋转。但是当我正常使用相机应用程序时,我会按应有的方式旋转。我尝试了该setCameraDisplayOrientation方法,但我不知道如何使用它。

提前致谢

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Surface;
import android.widget.Toast;

public class CaptureVideo extends Activity {

    final static int REQUEST_VIDEO_CAPTURED = 1;
    static Uri uriVideo = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);// Nu kommer man till main.xml två gånger. Låter det ligga så så att vi connectar till en annan knapp senare. 
        Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra("android.intent.extra.durationLimit", 8);
        intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        intent.putExtra("android.intent.extra.videoQuality", 1);
        startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){
            if(requestCode == REQUEST_VIDEO_CAPTURED){
                uriVideo = data.getData();
                Toast.makeText(CaptureVideo.this,uriVideo.getPath(),Toast.LENGTH_LONG).show();
                Intent intent = new Intent(CaptureVideo.this, ShowVideo.class);
                startActivity(intent);
            }
        }
        else if(resultCode == RESULT_CANCELED){
            uriVideo = null;
            Toast.makeText(CaptureVideo.this,"Cancelled!",Toast.LENGTH_LONG).show();
        }

    }
    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);
    }
}
4

0 回答 0