Search code examples
javaandroidandroid-camerax

Recording a 1280x720 video holding the phone in portrait orientation


I am struggling to record a video in a landscape resolution (1280x720) holding my phone in portrait orientation using the cameraX API (I also wasn't able to do it with any of the APIs, neither camera1 nor camera2). I was able to make it work for the ImageCapture use case and the preview though. The video keeps getting recorded in the nearest possible portrait resolution. Any help is appreciated!

CameraSelector cameraSelector = new CameraSelector.Builder()
   .requireLensFacing(CameraSelector.LENS_FACING_BACK)
   .build();

Preview preview = new Preview.Builder()
   .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
   .setTargetResolution(new Size(1280, 720))
   .build();
preview.setSurfaceProvider(mVideoView.createSurfaceProvider());

ImageCapture.Builder imageCaptureBuilder = new ImageCapture.Builder();
mImageCapture = imageCaptureBuilder
   .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
   .setTargetResolution(new Size(1280, 720))
   .build();

VideoCapture.Builder videoCaptureBuilder = new VideoCapture.Builder();
mVideoCapture = videoCaptureBuilder
   .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
   .setTargetResolution(new Size(1280, 720))
   .build();

ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
Camera camera = cameraProvider.bindToLifecycle(
   RecordMediaCameraXActivity.this,
   cameraSelector,
   preview,
   mImageCapture,
   mVideoCapture
 );

Solution

  • It's unlikely that the device supports the resolution you're looking for.

    Basically, you want a resolution that looks like this:

    4:3 full sensor
       |---------+========+---------|
       |         : 9:16   :         |
       |         : 720p   :         |
       |         : crop   :         |
       |         :        :         |
       |         :        :         |
       |         :        :         |
       |         :        :         |
       |         :        :         |
       |---------+========+---------|
    

    and that's just not something most devices support. They only support crops that are in the same alignment as the image sensor:

    4:3 full sensor
       |----------------------------|
       |                            |
       +============================+
       : 16:9 720p crop             :
       :                            :
       :                            :
       :                            :
       +============================+
       |                            |
       |----------------------------|
    
    

    If you want this, you'll likely need to crop the frames from the camera yourself. That'd require using something like ImageAnalysis or Preview to the GPU, and directly feeding a MediaRecorder or MediaCodec/MediaMuxer with the results.

    Also, CameraX doesn't yet officially support video recording, so it's possible that it'll support this kind of use case when it's officially available, but I wouldn't count on that.