Android Question CameraEx stretching preview

yiankos1

Active Member
Licensed User
Hello my friends,
I use my front camera at my app using CameraEx class, but as i see at some devices camera preview stretches (i think camera preview vertically). As i read here, there are many developer have same problem as me. They use
getOptimalPreviewSize, in order to get ratio and use the optimal preview size. Any idea to use that code at b4a or in-line java?
Thank you for your time.
 
Last edited:

yiankos1

Active Member
Licensed User
Good morning Erel,
Thank you for the answer.
For example:
My camera preview is 1080x1920, so my ratio will 0.56.
How to find out the best dimensions for my panel?
Thank you for your time.
 

Erel

Administrator
Staff member
Licensed User
Try this code:
B4X:
Dim size As CameraSize = camEx.GetPreviewSize
Dim ratio As Double = size.Width / size.Height
Dim pw, ph As Int
If 100%y * ratio > 100%x Then
   pw = 100%x
   ph = pw / ratio
Else
   ph = 100%y
   pw = ph * ratio
End If
Panel1.SetLayout(50%x - pw / 2, 50%y - ph / 2, pw, ph)
You might need to switch the width and height for portrait orientations.
 

peacemaker

Well-Known Member
Licensed User
I found that if the preview is on full-screen panel - almost no stratching of the video.
If to change panel's size smaller than full-screen - the preview video is stratched and always stratched, at any panel size, if smaller than full-screen.

Tested on 2 phones.

How to make small panel with non-distorted preview ?
 
Last edited:

peacemaker

Well-Known Member
Licensed User
Yes, tried, it works like my panel size calculation also.
When this code should be used ? Right after camera init ?

Ah, i forgot to say - landscape phone orientation helps to see the preview fully correct, the issue is only in the portrait orientation
 

peacemaker

Well-Known Member
Licensed User
Thanks, Erel, the full source project is emailed to you.
 

Erel

Administrator
Staff member
Licensed User
Try it with this code:
B4X:
Sub Camera1_Ready (Success As Boolean)
   Dim size As CameraSize = camEx.GetPreviewSize
   If Activity.Width < Activity.Height Then
     Dim t As Int = size.Width
     size.Width = size.Height
     size.Height = t
   End If
   Dim ratio As Double = size.Width / size.Height
   Dim pw, ph As Int
   If 100%y * ratio > 100%x Then
    pw = 100%x
    ph = pw / ratio
   Else
    ph = 100%y
    pw = ph * ratio
   End If
   Panel1.SetLayout(50%x - pw / 2, 50%y - ph / 2, pw, ph)

   If Success Then
     camEx.SetJpegQuality(90)
     camEx.SetContinuousAutoFocus
     camEx.CommitParameters
     camEx.StartPreview
     Log(camEx.GetPreviewSize)
   Else
     ToastMessageShow("Cannot open camera.", True)
   End If
End Sub
 

peacemaker

Well-Known Member
Licensed User
The same:
1) Portrait: round objects on the preview are vertical ovals
2) Landscape: round objects on the preview are horizontal ovals
But panel size is adjusted.

Also i have found that panel's settings affect - horizontal and vertical anchors: if to set panel into the left-top anchors and make Panel not full-screen - the landscape mode is OK !
But all other variants - distort the preview, the portrait is always distorted
 

peacemaker

Well-Known Member
Licensed User
SOLVED, but by a trick with double init and preparing the panel size after first init, and then - re-init the camera:

B4X:
Sub Activity_Resume
    FirstInit = True
    InitializeCamera
End Sub

Private Sub ReInitializeCamera
    FirstInit = False
       camEx.Release
    InitializeCamera
End Sub

Sub Camera1_Ready (Success As Boolean)
If FirstInit = True Then
   Dim size As CameraSize = camEx.GetPreviewSize
   If Activity.Width < Activity.Height Then
     Dim t As Int = size.Width
     size.Width = size.Height
     size.Height = t
   End If
   Dim ratio As Double = size.Width / size.Height
   Dim pw, ph As Int
   If 100%y * ratio > 100%x Then
    pw = 100%x
    ph = pw / ratio
   Else
    ph = 100%y
    pw = ph * ratio
   End If
   Panel1.SetLayout(50%x - pw / 2, 50%y - ph / 2, pw, ph)

    CallSubDelayed(Me, "ReInitializeCamera")
    Return
End If

    If Success Then
        camEx.SetJpegQuality(90)
        camEx.SetContinuousAutoFocus
        camEx.CommitParameters
        camEx.StartPreview
        Log(camEx.GetPreviewSize)
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub
Is it possible to fix the lib anyhow to avoid such dual init?
 

peacemaker

Well-Known Member
Licensed User
I meant maybe somehow to update the CameraEx class.
Or default camera apps also do such dual initialization to have "real" preview ?
 

Urishev

Member
Licensed User
Hello! Is it possible to resize the camera preview?
My camera preview is 1080*1920, but I want to change to 600*800.
Why in this code the size does not change?
Sub Camera1_Ready (Success AsBoolean)
If Success Then
camEx.StartPreview
camEx.SetPictureSize(800, 600)
camEx.CommitParameters
Else
ToastMessageShow("Cannot open camera.", True)
EndIf
EndSub
 

strat

Active Member
Licensed User
Hello! Is it possible to resize the camera preview?
My camera preview is 1080*1920, but I want to change to 600*800.
Why in this code the size does not change?
Sub Camera1_Ready (Success AsBoolean)
If Success Then
camEx.StartPreview
camEx.SetPictureSize(800, 600)
camEx.CommitParameters
Else
ToastMessageShow("Cannot open camera.", True)
EndIf
EndSub

If you want to change preview size, you should use "camEx.SetpreviewSize(800, 600)" instead of "camEx.SetPictureSize(800, 600)".
 
Top