13

I have tested new Camera2 API on Android Lollipop. I want to fetch supported preview size:

StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] sizes = streamConfigurationMap.getOutputSizes(SurfaceTexture.class);

and the maximum preview size is 1440x1080px on Samsung Galaxy Tab S that has 2560x1600px resolution. So my previewSize is 1440x1080px and TextureView surface size is 2560x1600px so image is distorted.

I tested old Camera API that is deprecated.

Camera.Parameters parameters =  camera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPictureSizes();

And the code above returns 32 varius combinations of preview size such as: 2560x1920, 1920x1080, 1080x1920, 2560x2560 etc. In that case I am able to choose optimal size and display correct image.

I do not know how to force new API to get optimal size. I know that the solution is to resize down view that displays preview, but built-in camera app works in fullscreen mode correctly. Thanks in advance for all suggestions!

Yasel
  • 2,920
  • 4
  • 40
  • 48
piotrpawlowski
  • 767
  • 2
  • 9
  • 19
  • "So my previewSize is 1440x1080px and TextureView surface size is 2560x1600px so image is distorted" -- that is because your aspect ratios are not the same. You need to make the `TextureView` have the same aspect ratio as the camera preview images, such as using negative margins to size it bigger than your screen on one axis. – CommonsWare Jul 11 '15 at 21:54
  • I know about that, but why preview size is so small? On Samsung Galaxy S4 preview size is 1920 x 1080 (the same as resolution) and there is no problem to display image. – piotrpawlowski Jul 11 '15 at 22:02
  • 1
    "I know about that, but why preview size is so small?" -- you would have to ask Samsung, I suspect. – CommonsWare Jul 11 '15 at 22:04
  • 4
    Samsung, like many other manufacturers today, have not (yet?) wholeheartedly embraced the Camera2 API. They did implement it because it is required on Lollipop, but except Nexus devices, you will today be better served if you use the deprecated Camera API. You will find that some of the advanced features of Camera2 are not working, while others are essentially implemented through the old HAL. – Alex Cohn Jul 12 '15 at 05:13
  • 1
    Thank you, it is important information. I will not waste my time on this issue. – piotrpawlowski Jul 12 '15 at 06:54
  • Interesting thread. After having spent a week on intensely testing Camera2 I have decided not to use it yet. My impression is that some features are just not yet mature. For example I didn't find any convincing example to get a high frequency preview image capture via the imageReader, nor was I able to implement it myself. Checking here in SO for "camera" I find 97'000 Posts, for Camera2 just 700. Thus I'm rather sure that for the near future the old Camera will still be supported by new devices. – Settembrini Mar 19 '16 at 00:01
  • @AlexCohn is this real about manufactures? my phone is Xiaomi. Is next issue real? https://stackoverflow.com/questions/52374888/camera2-1440x1080-is-maximum https://github.com/googlesamples/android-Camera2Basic/issues/123 first I had problems with MediaRecorer Video Surface Input (https://stackoverflow.com/questions/51332386/mediarecorder-and-videosource-surface-stop-failed-1007-a-serious-android-bug) and now Camera 2... It's much easier with iOS because there is only one manufacture - Apple – user25 Sep 17 '18 at 20:37
  • @CommonsWare see the comment from me to AlexCohn. And I was trying to update some code in my projects to use Camera2 instead of legacy camera... – user25 Sep 17 '18 at 20:38
  • parameters.getSupportedPictureSizes() returns the supported still image sizes, not the preview sizes. What does parameters.getSupportedPreviewSizes() return? – Eddy Talvala Oct 04 '18 at 18:02

2 Answers2

5

As @Alex Cohn said it depends on the manufacturers to embrace Camera API2.

You can check if the Camera API2 is supported with camCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); returning an int corresponding to the level of support for Camera API2. If the level you get is equal to CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACYbasically your phone manufacturer does not support Camera API2.

--> As a consequence the SCALER_STREAM_CONFIGURATION_MAP may return different resolutions than the ones accessible with Camera API1. This is the reason why you get

the maximum preview size is 1440x1080px

What really shocks me it that the question was asked 4 years ago... and there are still manufacturers that did not care to upgrade to Camera API2. While Camera API2 gives you much more features, maybe Camera API1 is not dead after all.

Would be nice to have some fragmentation information, at least to know how many devices you actually target if you use Camera API2 (it's not all the Android L and above because of the LEGACY support, developers deserve to know that)

dnhyde
  • 1,265
  • 2
  • 14
  • 24
4

replace

Size[] sizes = streamConfigurationMap.getOutputSizes(SurfaceTexture.class);

to

Size[] sizes = streamConfigurationMap.getOutputSizes(ImageFormat.JPEG)
  • 3
    I'm not 100% sure, but this will give you the supported size for still captures, not for the preview. – Tim Autin Jun 27 '17 at 07:21
  • `Size[] getOutputSizes (Class klass)` only returns sizes for [PRIVATE](https://developer.android.com/reference/android/graphics/ImageFormat.html#PRIVATE) image format. Some of the supported classes may support additional formats beyond PRIVATE. For example, ImageReader also supports YUV_420_888. SurfaceTexture may accept YUV_420_888 or NV21, too. – Alex Cohn Sep 03 '17 at 10:11
  • For future readers, don't use the recommended solution because the device may not support `ImageFormat.JPEG`. I have seen one of Samsung devices not supporting it, don't remember which one exactly tho. Documentation (https://developer.android.com/reference/android/hardware/camera2/params/StreamConfigurationMap#getOutputSizes(int)) reads "input of `StreamConfigurationMap.getOutputSizes(int format)` should be one of `StreamConfigurationMap.getOutputFormats()`" – Farid Mar 19 '21 at 08:40