Android Question [B4X] [B4XPages] CameraEx - Square preview is stretched

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, i know there are some posts about stretching of the preview of the cameraEx, (one of them is mine) but them are related to resize correctly the panel containing the preview.

Instead, i wish to know if there is a way to keep the preview panel size fixed (square), but have the camera feed with the correct aspect ratio (basically crop it to square)

now it looks like this (and it's a copy-paste of the virgin example project):

Screenshot_20210816_201112_biquadrolab.ecobi55.jpg


thanks in advance
 

Mike1970

Well-Known Member
Licensed User
Longtime User
1. Set the preview size to be a square.

i tried with:

B4X:
    camEx.Initialize(pnlQrPreview, False, Me, "Camera1")
    camEx.SetPreviewSize(pnlQrPreview.Width, pnlQrPreview.Height)
i also tried setting the previewsize ot 720x720 (that is a supported size).. nothing


but I get the following error:

B4X:
facing: 0, 0
Error occurred on line: 175 (CameraExClass)
java.lang.NoSuchMethodException: setPreviewSize [int, int]
    at java.lang.Class.getMethod(Class.java:2068)
    at java.lang.Class.getDeclaredMethod(Class.java:2047)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod3(Reflection.java:834)
    at biquadrolab.ecobi55.cameraexclass._setpreviewsize(cameraexclass.java:117)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**


on this line in the CameraEx class:

B4X:
Public Sub SetPreviewSize(Width As Int, Height As Int)
    r.target = parameters
    r.RunMethod3("setPreviewSize", Width, "java.lang.int", Height, "java.lang.int")
End Sub

it this the right way ? or have i misunderstood?
 
Last edited:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
1. You need to set it in Camera1_Ready event (see the example).
2. You can only set it to one of the sizes returned from GetSupportedPreviewSizes.

Thanks @Erel it worked.
I did like so:

B4X:
    camEx.Initialize(pnlQrPreview, False, Me, "Camera1")
    Wait For Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetContinuousAutoFocus
        Dim maxCameraSize As CameraSize = findLargestSquarePreviewSize
        camEx.SetPreviewSize(maxCameraSize.Width, maxCameraSize.Height)
        camEx.CommitParameters
        camEx.StartPreview
      
        prgQrLoading.Visible = False
    Else
        ToastMessageShow("Errore apertura camera", False)
        StopCamera
    End If


I made this function to find the largest square previewsize
B4X:
Private Sub findLargestSquarePreviewSize As CameraSize
    Dim result As CameraSize = camEx.GetPreviewSize
    
    For Each c As CameraSize In camEx.GetSupportedPreviewSizes
        If (c.Width == c.Height) And ((c.Width > result.Width) Or (result.Height <> result.Width)) Then
            result = c
        End If
    Next
    
    Return result
End Sub
 
Last edited:
Upvote 0
Top