Android Question (Solved) Camera2 - Preview frontal Camera - flip photo

f0raster0

Well-Known Member
Licensed User
Longtime User
Hey everyone,

When using frotal camare, where the image token changes in the preview.
Is there a way to maintain consistency with the image token throughout, without it flipping in the preview?

Camera2 lib:

Mediachooser works if I change the settings in the phone, but my app is already using camera2, and before start changing things,

Is it possible to prevent the preview image from flipping using Camera2?
 
Solution
You will need to flip the image yourself. You can do it with BitmapCreatorEffects.
ok, thanks

The app now works as expected with the following behavior:
  • The camera preview is mirrored by default.
  • Once an image is taken, it keeps the mirrored effect, just like in the phone's default camera app.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
.....
    frontCamera = True 'open defaul frontal camera
    OpenCamera(frontCamera)    
End Sub

B4X:
Private Sub TakePicture
    Try
        SetState(openstate, True, VideoMode)
        Wait For(cam.FocusAndTakePicture(MyTaskIndex)) Complete (Data() As Byte)
        SetState(openstate, False, VideoMode)
        cam.DataToFile(Data, VideoFileDir, "1.jpg")...

f0raster0

Well-Known Member
Licensed User
Longtime User
thanks

Adding this line doesn't flip the preview image:
B4X:
tv.As(JavaObject).RunMethod("setScaleX", Array((-1).As(Float)))
But it flips the view in the camare when taking the selfie.

When I try MediaChooser, it works as expected:
  • The camera view is not flipped when taking the selfie.
  • After the photo is taken, the preview remains unflipped.
I want to achieve the same behavior using the Camera2 library, if posible


Edit:
The front camera preview behaves like a mirror. This is the same as the default behavior of the camera app.
yes, i want like that. It works correctly there using the camera app or when using MediaChooser. However, when using the Camera2 library example, and if added that line above doens't flip the preview, but the image now gets flipped before taking the picture - it should behaves like a mirror and don't flip the preview
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
The standard behavior should be:
- Preview looks like a mirror (preview = not talking about the PreviewTaken event).
- Once the image is taken, the image is no longer mirrored.
Yes, it is like that when using camera2 lib.

The default camera of the phone save the selfie as they appear in the preview (it also happen like that when using Mediachooser lib)

But when using camera2 lib it save the selfie flipped.

Can camera2 lib take the selfie like a mirrow and save it like a mirrow, without flip it?

 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
You will need to flip the image yourself. You can do it with BitmapCreatorEffects.
ok, thanks

The app now works as expected with the following behavior:
  • The camera preview is mirrored by default.
  • Once an image is taken, it keeps the mirrored effect, just like in the phone's default camera app.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
.....
    frontCamera = True 'open defaul frontal camera
    OpenCamera(frontCamera)    
End Sub

B4X:
Private Sub TakePicture
    Try
        SetState(openstate, True, VideoMode)
        Wait For(cam.FocusAndTakePicture(MyTaskIndex)) Complete (Data() As Byte)
        SetState(openstate, False, VideoMode)
        cam.DataToFile(Data, VideoFileDir, "1.jpg")
        
        Dim bmp As Bitmap = cam.DataToBitmap(Data)
        Log("test Picture taken: " & bmp)

        Dim flippedBmp As Bitmap = FlipBitmapHorizontally(bmp) 'flip the image
        If flippedBmp.IsInitialized Then
            Log("test: Show the flipped image")
            pnlBackground.SetVisibleAnimated(100, True)
            B4XImageView1.Bitmap = flippedBmp

            Log("test: Refresh the image view ")
            Sleep(4000)
            pnlBackground.SetVisibleAnimated(500, False)
        Else
            Log("Flipped image was not generated successfully")
        End If

    Catch
        HandleError(LastException)
    End Try
End Sub


Private Sub FlipBitmapHorizontally(bmp As Bitmap) As Bitmap
    Dim bc As BitmapCreator
    bc.Initialize(bmp.Width, bmp.Height)
    For i = 0 To bmp.Height - 1
        For j = 0 To bmp.Width - 1
            Dim pixelColor As Int = bmp.GetPixel(j, i)
            bc.SetColor(bmp.Width - j - 1, i, pixelColor) ' Flip horizontally
        Next
    Next
    Return bc.Bitmap
End Sub
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…