Android Question Front camera 100%x and 100%y

Douglas Farias

Expert
Licensed User
Longtime User
Good morning everyone.
I am trying to do a job that requires the use of the camera, but in this work I need the front camera to be 100% on the screen. (camex)

this is what i need
APP_POCKETTO_VERSAO1-04.jpg





is a 100%x and 100%y panel and the image is correct, the panel is not stretching the image.

i tryed make a example like this and here is the results
B4X:
Sub Camera1_Ready (Success As Boolean)
    If Success Then
        Dim csz() As CameraSize
        csz = camEx.GetSupportedPreviewSizes
        Dim Maxr As Int = 0
        Dim mp As Map
        mp.Initialize
        For Each cs As CameraSize In csz
            mp.Put(cs.width, cs.height)
            Maxr = Max(Maxr, cs.width)
        Next
      
        If mp.ContainsKey(641) Then
            camEx.SetPreviewSize(640,mp.Get(640))
            camEx.SetPictureSize(640,mp.Get(640))
        Else if mp.ContainsKey(721) Then
            camEx.SetPreviewSize(720,mp.Get(720))
            camEx.SetPictureSize(720,mp.Get(720))
        Else
            camEx.SetPreviewSize(Maxr, mp.Get(Maxr))
            camEx.SetPictureSize(Maxr, mp.Get(Maxr))
        End If
          

          
        camEx.SetJpegQuality(100)
        camEx.SetContinuousAutoFocus
        camEx.CommitParameters
        camEx.StartPreview
      
    Else
        ToastMessageShow("Cannot open camera, please try close and open the app again", True)
    End If
  
  
End Sub

RESULT 1
Screenshot_2017-01-23-12-18-19.jpg



the panel is 100% but the image is stretching.


here is another try, with erel example posted here
https://www.b4x.com/android/forum/threads/cameraex-stretching-preview.62462/#post-394473

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)


RESULT 2
Screenshot_2017-01-23-12-12-28.png



here the image is correct, is not stretching the panel and image, but is not 100% of screen.

how can i make this panel 100% on the screen and not stretching the image?
i m not good with Calculations(math)

Could someone help me with this?
thank you all.

Ps: the guns on the wall are airsoft and pellet, not real guns.
 
Last edited:

sorex

Expert
Licensed User
Longtime User
you'll have to compensate things somewhere since the dimension ratio of the picture don't match the ratio of the screen.

if you go full screen you'll lose some parts of the picture that goes outside of the screen.

if you don't mind having that you just calculate the width to 100%x and height ratio based to the width.
if the height is still smaller than 100%y you just calculate the opposite for both which should give you a height of 100%y and a width >100%x so you need to center it.
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
B4X:
        cs = camEx.GetPreviewSize
        Dim w As Int = cs.Height
        Dim h As Int = cs.Width
        pCamera.SetLayout((Activity.Width - w) / 2, (Activity.Height - h) / 2, w, h)
      
        Private ph,pw As Float
        ph = 100%y
        pw = ph * ratio
        pCamera.SetLayout(50%x - pw / 2, 50%y - ph / 2, pw, ph)
      
      
        Dim v As View = pCamera.GetView(0)
        v.SetLayout(0, 0, pCamera.Width, pCamera.Height)
        camEx.SetJpegQuality(100)

what is the ratio?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the ratio is the relation between width and height. so w/h

you need it to keep it proportially resized (non stretched)

ratio is double/float. pw/ph just plain int.
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
thx, works fine, donated 10 EUR for your help.
thx again.

B4X:
        cs = camEx.GetPreviewSize
        Dim w As Int = cs.Height
        Dim h As Int = cs.Width
        Private ratio As Float = w/h
        pCamera.Height = 100%y
        pCamera.Width = pCamera.Height * ratio
        pCamera.SetLayout(50%x - pCamera.Width / 2, 50%y - pCamera.Height / 2, pCamera.Width, pCamera.Height)
        Dim v As View = pCamera.GetView(0)
        v.SetLayout(0, 0, pCamera.Width, pCamera.Height)
        camEx.SetJpegQuality(100)
 
Upvote 0
Top