Android Question Camera2 - get optimal sizes

Alexander Stolte

Expert
Licensed User
Longtime User
i have a panel with the full height of the activity, the preview image looks stretched if i set this static to 1920x1080, how can i calculate the optimal sizes?

My code is not working, because he think 1080x1080 is the best size...
The panel is 1080x2200 on my device.

B4X:
Private Sub GetOptimal As CameraSize   
    Dim last_sizes As CameraSize
    last_sizes.Initialize(0,0)
    For Each prev_sizes As CameraSize In cam.SupportedPreviewSizes
        Log("Width: " & prev_sizes.Width & " Height: " & prev_sizes.Height)
        If prev_sizes.Height <= xpnl_camera_background.Height And  prev_sizes.Width <= xpnl_camera_background.Width And prev_sizes.Height > last_sizes.Height And prev_sizes.Width > last_sizes.Width Then
            last_sizes.Initialize(prev_sizes.Width,prev_sizes.Height)
            Return last_sizes
        End If
    Next
    Return last_sizes
End Sub

all available sizes:
Width: 4608 Height: 2304
Width: 4608 Height: 2592
Width: 4608 Height: 2176
Width: 4608 Height: 2112
Width: 4096 Height: 2160
Width: 4096 Height: 1940
Width: 4000 Height: 3000
Width: 3840 Height: 2160
Width: 3456 Height: 3456
Width: 3264 Height: 2448
Width: 3200 Height: 2400
Width: 2976 Height: 2976
Width: 2688 Height: 1512
Width: 2592 Height: 1944
Width: 2592 Height: 1940
Width: 2340 Height: 1080
Width: 2304 Height: 1728
Width: 2280 Height: 1080
Width: 2160 Height: 1080
Width: 2048 Height: 1536
Width: 1920 Height: 1440
Width: 1920 Height: 1080
Width: 1440 Height: 1080
Width: 1280 Height: 960
Width: 1280 Height: 768
Width: 1280 Height: 720
Width: 1080 Height: 1080
Width: 1024 Height: 738
Width: 1024 Height: 768
Width: 800 Height: 600
Width: 800 Height: 480
Width: 720 Height: 480
Width: 640 Height: 480
Width: 352 Height: 288
Width: 320 Height: 240
Width: 176 Height: 144
 

JordiCP

Expert
Licensed User
Longtime User
You should take into account that the camera preview sizes will be according to the camera default orientation, which uses to be landscape for mobile phones (not for tablets if I remember correctly) --> so you should find the default camera orientation of the device, and switch width<-->height in the comparison accordingly.

(Inner comparison for a camera that has default orientation as landscape.)
B4X:
If prev_sizes.Height <= xpnl_camera_background.Width And  prev_sizes.Width <= xpnl_camera_background.Height And prev_sizes.Height >= last_sizes.Height And prev_sizes.Width >= last_sizes.Width Then
            last_sizes.Initialize(prev_sizes.Width,prev_sizes.Height)
            Return last_sizes
        End If
  • Note that width - height have been switched in the first 2 terms of the comparison.
  • Also note that the inequalities of the 2 last terms are now >= instead of >

In fact, once the optimal preview size is found, you should resize the panel fit to the chosen preview form factor (in this case 2160x1080), since it is not guaranteed that there will always be an exact one that totally fits the screen.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Thank you!
It works, but unfortunately with bars on top and bottom, because the panel has to be adjusted. I'll have to live with that. ?
B4X:
Private Sub GetOptimal As CameraSize
    Dim last_sizes As CameraSize
    last_sizes.Initialize(0,0)
    For Each prev_sizes As CameraSize In cam.SupportedPreviewSizes
        If prev_sizes.Height <= xpnl_camera_background.Width And  prev_sizes.Width <= xpnl_camera_background.Height And prev_sizes.Height >= last_sizes.Height And prev_sizes.Width >= last_sizes.Width Then
            last_sizes.Initialize(prev_sizes.Width,prev_sizes.Height)
            Return last_sizes
        End If
    Next
    Return last_sizes
End Sub

Sub PrepareSurface As ResumableSub  
    Dim last_sizes As CameraSize = GetOptimal
    xpnl_camera_background.SetLayoutAnimated(0,(Root.Width - last_sizes.Height)/2,(Root.Height - last_sizes.Width)/2,last_sizes.Height,last_sizes.Width)
    cam.PreviewSize.Initialize(last_sizes.Width,last_sizes.Height)
    Wait For (cam.PrepareSurface(MyTaskIndex)) Complete (Success As Boolean)
    If Success Then cam.StartPreview(MyTaskIndex, False)
    Return Success
End Sub
 
Upvote 0
Top