4

I've been trying to use CameraX in my android application and wants both preview and output image to be a quality 1:1 Aspect Ratio image. I tried the following code for setting the image and preview config

PreviewConfig previewConfig = new PreviewConfig.Builder()
                .setTargetAspectRatio(new Rational(1, 1))
                .build();

--

        ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder()
                .setTargetAspectRatio(new Rational(1, 1))
                .setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
                .build();

According to the developer's guide this code would choose the best resolution the hardware could provide in that aspect ratio. Though this works well on some devices, on some devices the output image resolution is as low as 280X280 pixels. Seeming like that is not the valid aspect ratio for the hardware on that device.

What can I try as a workaround? What I had in mind was that I think every device would support 4:3 Aspect Ratio.

I can crop the output image but what about the preview. Can we crop the textureview?

Or can I put a simple black rectangle-shaped view on top of the viewfinder seeming like it is 1:1 image?

karatuno
  • 365
  • 5
  • 18
  • 1
    you cannot achieve 1:1 camera picture or video with camera API you have to use library like FFMPEG or if you are capturing just picture you have to overlay a view on camera view to pretend like 1:1 and then crop the 4:3 image in 1:1 – Salman Aziz Sep 26 '19 at 19:26
  • how did you solve this problem? @karatuno – Cafer Mert Ceyhan Jun 20 '20 at 22:35
  • I did exactly the same hack I proposed in the question. I used a dynamic black rectangle-shaped view on top of the viewfinder seeming like it is 1:1 image and cropped the image on output. I know it's not a good way and there should be a better way to approach this. But I could not found one so I did it the hack way. If you do find a better technique or better hack please do put an answer to this question. – karatuno Jun 21 '20 at 15:51

1 Answers1

5

You are not the only one to complain. 1:1 support is still not very good.

The mitigation you propose looks reasonable. 4:3 delivers high quality on all devices I have ever met. You can always overlay the preview with some non-transparent views to provide a look-and-feel of square preview.

There is an example on GitHub how you can use MediaCodec with cropped frames to produce a square video.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307