Bug? Bitmap.Rotate()

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, yesterday I published a post in the "iOS Question" topic, but it seems to be a bug, to me.
this is the post where a explained and discussed with other members:

 

Mike1970

Well-Known Member
Licensed User
Longtime User
Please summarize the bug report here and if possible upload a small example.

From today the instruction .Rotate for a bitmap is behaving in a strange way.
I think the only thing changed is that I update to iOS 14.3.

My tests
The code
B4X:
Sub Cam_Complete(Success As Boolean, Image as Bitmap, VideoPath As String)
    If Success Then
        If Image.IsInitialized Then
            btnDelete_Click 'delete previous chached images'
       
            imgCL.ContentMode = imgCL.MODE_FIT
            imgCL.Bitmap = Image

            Dim out as OutputStream = File.OpenOutput(path, fname, False)
            Image.Rotate(90).WriteToStream(out, 50, "JPEG")
       
            Dim p as Phone
            p.AddImageToAlbum(LoadBitmap(path, fname))
       
        End If
    End If
End Sub

This code ☝result is this image:
View attachment 104523

I changed the line 10 to:
B4X:
Image.WriteToStream(out, 50, "JPEG")
then I recompiled the app, and take another shot.

and the result was this:
View attachment 104524
this is the right real-life aspect

I Attach a small video: https://mega.nz/file/HZ0CQLRS#YlBIQvItD4Aqjo54z4o70veIk14yD4KH990c3BXCVSk
and an example project
 

Attachments

  • TestBitmap.zip
    2.9 KB · Views: 292

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is related to the original image actually being rotated.

You are rotating it so the image will appear correctly in the gallery, right?

You can see that it is rotated with:
B4X:
Dim no As NativeObject = image
Log(no.GetField("imageOrientation").AsNumber)
Orientation values: https://developer.apple.com/documentation/uikit/uiimageorientation?language=objc (3 = right).
You can reset the orientation with:
B4X:
im = image.Crop(0, 0, image.Width, image.Height)
 

Mike1970

Well-Known Member
Licensed User
Longtime User
You are rotating it so the image will appear correctly in the gallery, right?
that code like some month ago was working.

now, In this days where I noticed this behavior I never saw it correctly, in the gallery neither.
always stretched.

my goal:
IMG_4719-min.JPG


my result:
IMG_4717-min.JPG

(stretched)
 
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User
Nothing has changed in Bitmap implementation. The change is probably in the way that the camera handles the orientation.

Have you tried the solution with crop instead of rotate. Seems to work fine here.


You mean like so?

B4X:
Sub Cam_Complete(Success As Boolean, Image as Bitmap, VideoPath As String)
    If Success Then
        If Image.IsInitialized Then
            btnDelete_Click 'delete previous chached images'
      
            imgCL.ContentMode = imgCL.MODE_FIT
            imgCL.Bitmap = Image

            Dim out as OutputStream = File.OpenOutput(path, fname, False)
            dim im as Bitmap = Image.Crop(0, 0, Image.Width, Image.Height)
            im.WriteToStream(out, 50, "JPEG")
      
            Dim p as Phone
            p.AddImageToAlbum(LoadBitmap(path, fname))
      
        End If
    End If
End Sub
 

Mike1970

Well-Known Member
Licensed User
Longtime User
Does it work?

I tried with this function, there is no difference between switch ON and switch OFF, the image is the same
B4X:
Sub Cam_Complete (Success As Boolean, image As Bitmap, VideoPath As String)
    If Success Then
        If image.IsInitialized Then
           
            imageview.ContentMode = imageview.MODE_FIT
           
            Dim out As OutputStream = File.OpenOutput(File.DirLibrary & "/" & "TestBitmap", "bitmap.jpeg", False)
            If Switch1.Value Then
                Dim im As Bitmap
                'im = image.Rotate(90)
                im = image.Crop(0,0,image.Width, image.Height)
                imageview.Bitmap = im
                im.WriteToStream(out, 50, "JPEG")
            Else
                imageview.Bitmap = image
                image.WriteToStream(out, 50, "JPEG")
            End If
            out.Close
           
            Dim p As Phone
            p.AddImageToAlbum(LoadBitmap(File.DirLibrary & "/" & "TestBitmap", "bitmap.jpeg"))
        End If
    End If
End Sub
The first was with the swtich off, and the second was with the switch on. aspect ration correct on both, but the second should be cropped/rotated (at least different ?)
IMG_4841.jpg


i tried to keep also .Rotate(90) in combo with .Crop(...) no change.
then i tried again with only .Rotate(90), and it squeeze like at the begging
 

Mike1970

Well-Known Member
Licensed User
Longtime User
The two images in this screenshot look fine. What is the problem?
The problem is that I wish the second (at the bottom left of the screen) to be rotated ? (and obviously with correct aspect ratio)

I photoshopped the image, to show what is my goal:
IMG_4841-2.jpg
 
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User
Have you tried to first crop it and then rotate?
B4X:
Sub Cam_Complete (Success As Boolean, image As Bitmap, VideoPath As String)
    If Success Then
        If image.IsInitialized Then
         
            imageview.ContentMode = imageview.MODE_FIT
         
            Dim out As OutputStream = File.OpenOutput(File.DirLibrary & "/" & "TestBitmap", "bitmap.jpeg", False)
            If Switch1.Value Then
                Dim im As Bitmap
                im = image.Crop(0,0,image.Width, image.Height)
                im = im.Rotate(90)
                imageview.Bitmap = im
                im.WriteToStream(out, 50, "JPEG")
            Else
                imageview.Bitmap = image
                image.WriteToStream(out, 50, "JPEG")
            End If
            out.Close
         
            Dim p As Phone
            p.AddImageToAlbum(LoadBitmap(File.DirLibrary & "/" & "TestBitmap", "bitmap.jpeg"))
        End If
    End If
End Sub

OK, like this I get the image rotate and with the correct aspect ratio!!
BUT, I did not understand why I've to use the ".Crop" function, before ".Rotate" to just rotate the image.
Is the ".Rotate" broken?
 
Top