Android Question ~CameraEx : bug in preview

aeropic

Active Member
Licensed User
Longtime User
Hi All,

I'm developping a small app based on cameraEx and my intention was to adapt the size of the screen to the resolution of the camera.
It is working fine when using the back camera, but when switching to the front camera I get weird results.

For example on my Galaxy S3, simply launching the app, selecting the front camera then switching back the camera generates the bug;
bug.png

- the preview seems to keep the size it had with the front camera and this generates a grey band at the bottom of the screen.

I spent a whole afternoon to check everywhere to see what happens when the camera is changed. There is just this:
B4X:
camEx.Release
frontCamera = Not(frontCamera)
InitializeCamera

and in the code of camera1_ready, I just reinitialize all parameters. (Basically the same code is for the button which handles the picture size and this one is working.)

B4X:
Sub Camera1_Ready (Success As Boolean)
    If Success Then
   
        ClearLvs
       
        camEx.SetJpegQuality(90)
        camEx.CommitParameters
        'camEx.StartPreview
        'Log(camEx.GetPreviewSize)
   
       
        ' set the maximum picture size
        Dim pictureSizes() As CameraSize = camEx.GetSupportedPicturesSizes
        Dim imax As Int
        Dim psmax As Int = 0
        For i = 0 To pictureSizes.Length-1  ' boucle recherche taille max
            If pictureSizes(i).Width + pictureSizes(i).Height > psmax Then
                psmax = pictureSizes(i).Width + pictureSizes(i).Height
                imax = i
            End If
        Next
       
        camEx.StopPreview
        camEx.CommitParameters
        camEx.SetPictureSize(pictureSizes(imax).Width , pictureSizes(imax).Height)
        camEx.SetPreviewSize(pictureSizes(imax).Width , pictureSizes(imax).Height)
        camEx.CommitParameters
       
       
        ' set the button EV initial value
        btnEV.Text = "+/-" & CRLF & "0"
       
        ' adapt the display to the image resolution
        Dim ratio As Float
        ratio = pictureSizes(imax).Height /pictureSizes(imax).Width
        btnPictureSize.Text = pictureSizes(imax).width&"x"& pictureSizes(imax).Height
        If ratio < (100%x / 100%y) Then 'crop largeur de panel  (ratio = 3/4 et y = 5 x =3 env)
        Panel1.Width =  100%y / ratio
        Panel1.Height = 100%y
        Panel1.left = Abs((100%x - Panel1.width)/2)
        Else
        Panel1.Width = 100%x
        Panel1.Height = 100%x / ratio
        Panel1.top = Abs((100%y - Panel1.height)/2)
        End If
       
        camEx.StartPreview
        camEx.CommitParameters
        Log(camEx.GetPreviewSize)
       
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub


In other words, I am a bit lost... I would be gratefull if somebody could help me to find my bug. (the full project is joined)

Thanks
Alain
 

Attachments

  • CameraUnlockbug.zip
    55.1 KB · Views: 372

aeropic

Active Member
Licensed User
Longtime User
Hi Erel,
You're right, this set of commit parameter was an attempt to fix the bug I forgot to remove. But unfortunately I don't think it fixes the problem.

The purpose of this code is to make the camera look like the samsung one:
- frame what the final picture will be according to the resolution
- indeed avoid distortions

The problem is clearly when switching to face camera and then back. Once in the bug, when changing the resolution for the back camera, the grey bands appear. Switching OFF then ON the power button reinitializes everything ...
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
yes, the panel values seem to be correct. When switching the camera I get those following values

panel1 wxh 720x960
1024 768
[Height=768, IsInitialized=false, Width=1024
]
panel1 wxh 720x1280
3264 1836
[Height=1836, IsInitialized=false, Width=3264
]
panel1 wxh 720x1079
3264 2176
[Height=2176, IsInitialized=false, Width=3264
]
panel1 wxh 720x1280
3264 1836
[Height=1836, IsInitialized=false, Width=3264
]
<====================================== here I change the camera
activity resume
back_camera_90
panel1 wxh 720x960
3264 2448
[Height=2448, IsInitialized=false, Width=3264
]
activity resume <======================= I switch again to face camera
front camera
panel1 wxh 720x720 <==================== it has a square aspect ratio ==> figures are correct
1392 1392
[Height=1392, IsInitialized=false, Width=1392
]
panel1 wxh 720x1280 <================== here correct values but wrong display
1280 720
[Height=720, IsInitialized=false, Width=1280
]

In fact it seems that the actual preview size does not fit the set and get preview values ...

edit : and yes I call release and reinitialize camera each time I switch front/back cameras.
I even tried with this:
B4X:
Sub ChangeCamera_Click
'    camEx.Release
    frontCamera = Not(frontCamera)
'    InitializeCamera
  
    camEx.CloseNow
    Activity_Resume
  
End Sub

without success :(
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm still getting an error.

You can use this code to find the supported preview sizes:
B4X:
Public Sub GetSupportedPreviewSizes As CameraSize()
   r.target = parameters
   Dim list1 As List = r.RunMethod("getSupportedPreviewSizes")
   Dim cs(list1.Size) As CameraSize
   For i = 0 To list1.Size - 1
     r.target = list1.Get(i)
     cs(i).Width = r.GetField("width")
     cs(i).Height = r.GetField("height")
   Next
   Return cs
End Sub
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
I'm still getting an error.

I didn't know ther was a list of supported preview sizes ...
After adding the code in cameraex class and loging the answers I get:


back_camera_90
Picture sizes________
640x480
960x720
1024x768
1280x720
1600x1200
2560x1920
3264x2448
2048x1536
3264x1836
2048x1152
3264x2176
Preview sizes________
960x720
1280x720
640x480
352x288
320x240

front camera

Picture sizes________
1280x960
1392x1392
640x480
1280x720
720x480
320x240
Preview sizes________
960x720
1280x720
704x704
352x288
640x480
320x320
320x240

In my code I set the preview size to the same value that picture size.
This maybe causes the error you get. What is strange is that I don't get any error ...

If I understand well, I have to select the preview size wrt the aspect ratio of the image to display ?
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
If I understand well, I have to select the preview size wrt the aspect ratio of the image to display ?
I have adapted the log to write the aspect ratio of pictures and of supported previews and this gives interesting results with some picture sizes not supported as preview and some preview size not fitting any picture size ...

I really do not clearly understand how to manage those preview sizes ? Maybe restricting a priori the picture sizes to what is supported in preview ?

back_camera_90

Picture sizes________
640x480 1.3333333333333333
960x720 1.3333333333333333
1024x768 1.3333333333333333
1280x720 1.7777777777777777
1600x1200 1.3333333333333333
2560x1920 1.3333333333333333
3264x2448 1.3333333333333333
2048x1536 1.3333333333333333
3264x1836 1.7777777777777777
2048x1152 1.7777777777777777
3264x2176 1.5 <<<<<<<<<<<<<<<<<<< NOT SUPPORTED in preview?
Preview sizes________
960x720 1.3333333333333333
1280x720 1.7777777777777777
640x480 1.3333333333333333
352x288 1.2222222222222223 <<<<<<<<<< NOT FITTING any picture siize
320x240 1.3333333333333333

front camera
Picture sizes________
1280x960 1.3333333333333333
1392x1392 1
640x480 1.3333333333333333
1280x720 1.7777777777777777
720x480 1.5 <<<<<<<<<<<<<<<<<<<< NOT SUPPORTED in preview
320x240 1.3333333333333333
Preview sizes________
960x720 1.3333333333333333
1280x720 1.7777777777777777
704x704 1
352x288 1.2222222222222223 <<<<<<<<<< NOT FITTING any picture siize
640x480 1.3333333333333333
320x320 1
320x240 1.3333333333333333
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
Hi Erel,
I've fixed the preview sizes to authorized ones according to the aspect ratio of the picture to display.
You should at least not get anymore the error with your Nexus 4 ?

But on my GS3, I still get the same strange behavior with grey bands after having switched to front camera then back again ... my feeling was right it is due to something else... but what ?

I join the project.
 

Attachments

  • CameraUnlockbug.zip
    345.3 KB · Views: 377
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
Seems to work fine here. Note that you should use File - Export as zip when uploading projects.

1- if it works fine for you, this might be a signature of another problem elsewhere...
I had problem to initialize the camera just after a wake up from the service starting the activity with a "SET-SHOW-WHEN-LOCKED" flag.
One try over three it failed to initialize correctly the camera:
- java lang exception directly at start
- java lang exception when using set resolution buton or flash button ...
- ...
I avoided the problem using a double initialization of the camera but I know it is not a clean way to fix it:
B4X:
Sub Activity_Resume
    SetShowWhenLocked
    WS.KeepAlive(True)
    Try
        InitializeCamera

        camEx.Release        ' <<< 2 lines added to avoid the initialization problem
        InitializeCamera
    Catch
        Msgbox("fail init camera", "")
    End Try

I had even inserted a try catch to check if the initializeCamera might fail, but it never triggers...
I've then the feeling that the "Camera1_Ready" event flag is raised with a success flag set to true but that the camera is not properly initialized ? Could I be right and might this be the root cause of the grey band I get on my Galaxy S3 after switching the camera ? The problem is that I'm not skilled enough to perform investigations in CameraEX class ...

2- Do you mean I must use a B4A menu "file export" to generate a zip ? I did not know this menu, I'll check this evening when back home !
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
1- I tired initializing the camera with a timer and as it seemed to fix the initialization bug, I reduced the timer down to 1 tick... Finally I suppressed the timer to discover that the initialization bug is gone... I probably introduced it with the wrong management of the preview size...

So today, I get a good initialization but still this grey band when switching to front/back camera on my S3

2- cool functionnality !!! I try it here and post the latest version of my GS3 buggy test app ...

thanks for all you help, Erel, I've never seen such a good customer service !
 

Attachments

  • CameraUnlockbug.zip
    51.4 KB · Views: 371
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
I think, I've eventually found my bug concerning the preview size... It is not possible to change the size of the panel to which is allocated the camera once the camera is initialized ...

camEx.Initialize(Panel1, frontCamera, Me, "Camera1")

Panel1 size cannot be modified any more !!!

SO in order to compute the size of the required panel coping with the available resolutions of the camera I think I have to do:
- 1rst initialisation of the camera and a loop to discover the available picture sizes and preview sizes
- then a computation of the associated panel sizes
- then everytime the image size is changed, reinitialize the camera with the correct panel size ...

Do I miss something ?
 
Upvote 0
Top