Android Question CameraEx stretching preview

yiankos1

Well-Known Member
Licensed User
Longtime 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

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime 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:
Upvote 0

peacemaker

Expert
Licensed User
Longtime 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
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, Erel, the full source project is emailed to you.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Just CamersEx sample - preview video is stratched horizontally, round icons are oval.
But default camera app - the preview is "real", not distorted.
 

Attachments

  • 2.zip
    12.1 KB · Views: 586
  • IMG_20160925_152502.jpg
    IMG_20160925_152502.jpg
    92.6 KB · Views: 743
  • IMG_20160925_152525.jpg
    IMG_20160925_152525.jpg
    88.3 KB · Views: 725
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime 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
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime 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?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I meant maybe somehow to update the CameraEx class.
Or default camera apps also do such dual initialization to have "real" preview ?
 
Upvote 0

Urishev

Member
Licensed User
Longtime 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
 
Upvote 0

strat

Active Member
Licensed User
Longtime 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)".
 
Upvote 0
Top