0

I want to open the camera and set the preview on the SurfaceView in order to record a video.Once done recording,intent to another activity and set the captured video URI in a VideoView.But my problem is,the video orientation is change automatically which is different to the camera preview.

This is the Camera preview(I expect the video capture is appear exactly the same):

enter image description here

But end up the video captured appear like below:

enter image description here

As you can see,the video is appear in Landscape mode,while the preview is in Portrait mode.

Here is my code for surfaceCreated when open 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();
    }
}

Here is how I prepare the video recorder:

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

So my question is:

1) What is the problem causing the Video captured's orientation rotate 180 degree?

2) How can I take the video which orientation is same as the Camera Preview in the surface view?

Thanks in advance.

2 Answers 2

1

Some devices rotates images and videos after capturing. Use below code to know if the image rotated:

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

if result is 0 then there is no rotation else you need to rotate it to the original. Use below code:

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;
}
4
  • So I need to put in the videoUri into the getRotation?
    – ken
    Nov 7, 2017 at 6:42
  • so the rotation in second function is the value return by 1st function??
    – ken
    Nov 7, 2017 at 7:31
  • The second function I just not understand..Cause now I working on video,video also have Bitmap??
    – ken
    Nov 7, 2017 at 7:33
  • yes rotation in second function is what returns in first funcion. No, for video there is no bitmap, custom it for video too. Nov 7, 2017 at 9:44
0

some devices like samsung rotates the image after capture.But through the code we can get the rotation and rotate if it is required.

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);
1
  • Bro but this is video,not image,is it still the same??
    – ken
    Nov 7, 2017 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.