iOS Question pictures is rotated withouth reason

omarruben

Active Member
Licensed User
Longtime User
Hi guys, I am using low lever camera lib and is working excelent, I am saving the file wiht next code:

B4X:
Dim Out As OutputStream
Out = File.OpenOutput(XUI.DefaultFolder, "Test.png", False)
Bitmap1.WriteToStream(out, 100, "PNG")
Out.Close

no problem....
but when reading the file back and put on a BMP the image is rotated

B4X:
Dim bmp As B4XBitmap = LoadBitmapResize(File.DirDocuments, "Test.png", 150dip, 150dip, True)
ImageView.Bitmap = bmp

can anyone explain what I am doing wrong?
thank you
 

Semen Matusovskiy

Well-Known Member
Licensed User
When you take a photo, a bitmap has a correct orientation. But bmp.WriteToStream does not write 'orientation' tag in Exif.
I found three parameters only : ColorSpace, PixelXDimension, PixelYDimension). Means IOS will assume UIImageOrientationUp.

Workaround ...

1) Add to the bottom
B4X:
#If OBJC
- (UIImage*) removeOrientation: (UIImage*) image { return [UIImage imageWithCGImage: [image CGImage] scale: [image scale] orientation: UIImageOrientationUp]; }
#End If

2) In PictureTaken after receiving a bitmap (let's name it bmp):
a) get image orientation
B4X:
    Dim byteOrientation As Byte = bmp.As(NativeObject).GetField("imageOrientation").AsNumber
b) set image orientation to UIImageOrientationUp (means The original pixel data matches the image's intended display orientation).
B4X:
    Dim no As NativeObject = Me
    bmp = no.RunMethod ("removeOrientation:", Array (bmp))
c) rotate a bitmap
B4X:
    Select Case byteOrientation
        Case 0
            ' bmp = bmp.Rotate (0)
        Case 1
            bmp = bmp.Rotate (180)
        Case 2
             bmp = bmp.Rotate (270)
        Case 3
            bmp = bmp.Rotate (90)
    End Select

After this you can use bmp.WriteToStream.
 
Last edited:
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
Tip: better to be consistent with XUI.DefaultFolder (which is the same as File.DirDocuments).

What happens if you try to show the image before it is saved (Bitmap1)?
shows normal no rotation... looks like using Bitmap1.WriteToStream(out, 100, "PNG") is rotated
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
When you take a photo, a bitmap has a correct orientation. But bmp.WriteToStream does not write 'orientation' tag in Exif.
I found three parameters only : ColorSpace, PixelXDimension, PixelYDimension). Means IOS will assume UIImageOrientationUp.

Workaround ...

1) Add to the bottom
B4X:
#If OBJC
- (UIImage*) removeOrientation: (UIImage*) image { return [UIImage imageWithCGImage: [image CGImage] scale: [image scale] orientation: UIImageOrientationUp]; }
#End If

2) In PictureTaken after receiving a bitmap (let's name it bmp):
a) get image orientation
B4X:
    Dim byteOrientation As Byte = bmp.As(NativeObject).GetField("imageOrientation").AsNumber
b) set image orientation to UIImageOrientationUp (means The original pixel data matches the image's intended display orientation).
B4X:
    Dim no As NativeObject = Me
    bmp = no.RunMethod ("removeOrientation:", Array (bmp))
c) rotate a bitmap
B4X:
    Select Case byteOrientation
        Case 0
            ' bmp = bmp.Rotate (0)
        Case 1
            bmp = bmp.Rotate (180)
        Case 2
             bmp = bmp.Rotate (270)
        Case 3
            bmp = bmp.Rotate (90)
    End Select

After this you can use bmp.WriteToStream.
I will try today, thank you
 
Upvote 0
Top