Orienting the camera to a webcam and phone

I made an application where I need to record video. When I launch it in my emulator using the cameraโ€™s camera orientation, this is normal, but when I run the same on the phone, the orientation becomes 90 degrees changed. Kant realizes that this is happening. Can someone help me? here is my code ---

private boolean prepareMediaRecorder(){
    myCamera = getCameraInstance();         
    mediaRecorder = new MediaRecorder();
    myCamera.unlock();
    mediaRecorder.setCamera(myCamera);

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

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

    mediaRecorder.setOutputFile("/sdcard/video.mp4");

    mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M

    mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());

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

}
0
source share
1 answer

The orientation of the preview depends on the orientation of the device and the orientation of the camera.

Basically, you need to calculate the orientation of the cameraโ€™s preview based on these conditions.

We need two help methods:

1 - Calculate the orientation of the device:

public int getDeviceOrientation(Context context) {

    int degrees = 0;
    WindowManager windowManager =
            (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int rotation = windowManager.getDefaultDisplay().getRotation();

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

    return degrees;
}  

2 - :

public static int getPreviewOrientation(Context context, int cameraId) {

   int temp = 0;
   int previewOrientation = 0;

   Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
   Camera.getCameraInfo(cameraId, cameraInfo);

   int deviceOrientation = getDeviceOrientation(context);
   temp = cameraInfo.orientation - deviceOrientation + 360;
   previewOrientation = temp % 360;

    return previewOrientation;
}

mediaRecorder.prepare();

int rotation = getPreviewOrientation(context, cameraId);
mediaRecorder.setOrientationHint(rotation);  

.

+2

All Articles