Android Question Camera Orientation Problem using different phones

gezueb

Active Member
Licensed User
Longtime User
Hi everybody,

I know a lot of people have already posted the same question, but libraries have evolved and changed. Ok here we go:

I have an app that takes a still picture using CamEx2 Version 1.3 and Version 10.0 of b4a with libraries at the same level.

When I take a picture (in Portrait fashion) with an Nexus LG 5x, everything works fine. Preview and saved pics are in the same orientation which is logged by CamEx2 as 270
When I take the same picture with a Sony Xperia, the saved picture is turned 90 degress anti-clockwise compared to preview. Orientation is logged by Camex2 as 270. Both phone run android 8.1

What is the best method to distinguish the difference and then rotate the picture.

Thanks for help
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
and Version 10.0 of b4a with libraries at the same level.
Always use the latest version.

There is code in this example, that will rotate the image based on the exif header.

 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
as the proud owner of 2 nexus 5x's (although one has just fried itself) - and in an effort to ease your confusion - i can tell you that the 5x's orientations are handled differently from other models. this is a known issue. when i'm feeling weak, i provide a rotater button for the user and try to act like everything is normal. when i'm a little more determined, i test for the model the user has, and run different sets of camera initialization. wait until you deal with selfies. by the way, self-imolating 5x's is also a known issue. something else for you to look forward to.
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Thank you to both, will try Erels example. Strangely enough, its the Sony Xperia that makes problems, not the Nexus.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i was in denial too.
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
I tried to integrate Erel's Camera intent into my code. It fails with this message:

B4A Version: 10.0
Java Version: 14
Parsing code. Error
Error parsing program.
Error description: Unknown type: b4xmainpage
Are you missing a library reference?
Error occurred on line: 73 (B4XPages)
Public Sub MainPage As B4XMainPage


I cannot find any reference to B4XMainPage!?
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Thank you, Erel, your support to dummies like me is very much appreciated. As I was only interested in rotating a bitmap from a camera according to the exif value, I have now opted for another option, the BitMapExtended Library from XverhelstX which does this nicely and easiliy together with the jpegUtils Library. The code looks like this and can be adapted to any other orientation if needed. And yes, I will update ;)
rotate bitmap according to camera metadata:
    Dim TargetWidth,TargetHeight As Int

    Dim Exif As ExifData
    Dim Orientation As String
    Exif.Initialize(File.DirInternal, "Chess.jpg")
    Orientation = Exif.getAttribute("Orientation")
    Log("Exif Orientation: "&Orientation)
    If Orientation = "0" Then
        srcBitmap.InitializeResize(File.DirInternal,"Chess.jpg",TargetWidth,TargetHeight,False)        'load and adjusts the Bitmap to the format of the camera
    End If
    ' the Sony Xperia cam leaves a picture that is 90 degrees rotated when the bitmap is loaded and displayed
        
    'requires the BitMapExtended Library
    If Orientation = "6" Then
        Dim bmp As Bitmap
        Dim bmp2 As Bitmap
        bmp.InitializeMutable(TargetWidth,TargetWidth)       'must be a square to rotate it by 90 or 270 degrees!
        bmp.Initialize(File.DirInternal, "Chess.jpg")
        bmp = btmpex.rotateBitmap(bmp,90)
        bmp2 = btmpex.createScaledBitmap(bmp,TargetWidth,TargetHeight,False) 'now get it back into the original size
        srcBitmap = bmp2
    End If
    Log("File loaded")
 
Upvote 0
Top