Android Question CameraEx stretching preview

Discussion in 'Android Questions' started by yiankos1, Jan 17, 2016.

  1. yiankos1

    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: Jan 18, 2016
  2. Erel

    Erel Administrator Staff Member Licensed User

    There is no such method in Android.

    You can call CamEx.GetPreviewSize and set the panel size to be with the same ratio as the preview size.
     
    yiankos1 likes this.
  3. yiankos1

    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.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Try this code:
    Code:
    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 / 250%y - ph / 2, pw, ph)
    You might need to switch the width and height for portrait orientations.
     
    yiankos1 likes this.
  5. yiankos1

    yiankos1 Active Member Licensed User

    Just tried your code and it's working perfect. Thank you very much for your time.
     
  6. peacemaker

    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: Sep 24, 2016
  7. Erel

    Erel Administrator Staff Member Licensed User

    Are you using the code posted in post #4?
     
  8. peacemaker

    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
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Camera_Ready event.

    If you are unable to solve it then please upload a small project with the relevant code.
     
  10. peacemaker

    peacemaker Well-Known Member Licensed User

    Thanks, Erel, the full source project is emailed to you.
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Please try to reproduce it in a small project and upload it to the forum.
     
  12. peacemaker

    peacemaker Well-Known Member Licensed User

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

    Attached Files:

  13. Erel

    Erel Administrator Staff Member Licensed User

    Try it with this code:
    Code:
    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 / 250%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
     
  14. peacemaker

    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
     
  15. peacemaker

    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:

    Code:
    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 / 250%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?
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    There nothing that can be changed in the library code that is relevant to this issue.
     
  17. peacemaker

    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 ?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    Note that on the device I tested it (Nexus 5X) it worked properly with the code I posted. Anyway, changing the panel preview size is not part of the CameraEx functionality. Your solution looks good.
     
    peacemaker likes this.
  19. Urishev

    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
     
  20. strat

    strat Active Member Licensed User


    If you want to change preview size, you should use "camEx.SetpreviewSize(800, 600)" instead of "camEx.SetPictureSize(800, 600)".
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice