Image in portrait orientation will be opened 90° ccw rotated

cmweb

Active Member
Licensed User
Longtime User
Hello,

I just noticed a strange behaviour of images that has been shot by my Samsung Galaxy Note camera:

Images made in portrait orientation will be shown in portrait mode in the Gallery (as well as in Quickpic and other image viewer apps), but are 90° counter clock wise rotated when loading the bitmap into a B4A Imageview (as they would be landscape oriented images).

Well, I could rotate the image by code. But how can I detect whether the image is intended to be viewed in portrait or landscape mode?

Or am I doing something wrong...!?

Best regards,

Carsten
 

cmweb

Active Member
Licensed User
Longtime User
If the images are Jpeg images then you can use JpegUtils library to read the orientation from the EXIF metadata.

Thanks, that sounds good. I'll give it a try...

Best regards,

Carsten

Gesendet von meinem GT-N7000 mit Tapatalk 2
 
Upvote 0

jur123

Member
Licensed User
Longtime User
Hi,
Images made in portrait orientation will be shown in portrait mode in the Gallery or PC
It is an old thread, but anyway I have a workaround for Samsung Galaxy Note2 (and others?)

The exif metadata [exif.getAttribute(exif.TAG_ORIENTATION)] shows a 6, but the picture is not rotated (showing in Landscape on PC or gallery)

The solution is to check the orientation of the phone in the activity create. Orientation=0 means Portrait.
In sub Camera1_PictureTaken check the height/width ratio. If ratio < 1 and orientation = 0 then rotate picture 90 degrees.

I have added the code involved.

Hope this helps someone. I took me an awfull long time to scrape these code/lines together.....

Regards, Jur


B4X:
Sub GetOrientation As Int
' 0 = portrait
' 1 = landscape left
' 3 = landscape right
  Dim R As Reflector
  R.Target = R.GetContext
  R.Target = R.RunMethod2("getSystemService", "window", "java.lang.String")
  R.Target = R.RunMethod("getDefaultDisplay")
  Return R.RunMethod("getOrientation")
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
  Dim In As InputStream
  Dim Out As OutputStream
  Dim drect As Rect
' make a bitmap of the data stream from the camera
  In.InitializeFromBytesArray(Data, 0, Data.Length)
  Dim bmp, bmp2 As Bitmap
  bmp.Initialize2(In)
  bmp2.Initialize3(bmp)
  In.Close
' check orientation and height/width ratio of the picture
  If bmp.Width > bmp.Height AND Orientation = 0 Then
  ' portrait oriention, but landscape photo
    RESU = RotateBitmap90(bmp)  'RESU met resultaat
    Out = File.OpenOutput(Imagefolder, bestand, False)
    RESU.WriteToStream(Out, 100, "JPEG")
  Else
    camera1.SavePictureToFile(Data, Imagefolder, bestand)
  End If
  camera1.StartPreviewEnd
Sub

Sub RotateBitmap90(SBMP As Bitmap) As Bitmap
  Dim bb As Bitmap
  Dim angle As Int : angle = 90
  'original size
  Dim pwo As Int
  Dim pho As Int
  'resized to ...
  Dim sqp As Int
  Dim Recto As Rect
  Dim c As Canvas

  pwo = SBMP.Width
  pho = SBMP.Height
  sqp = pwo 'for square imaqe

  If pwo <> pho Then
      'set longer side
      sqp = pho
      If pwo > pho Then sqp = pwo
      'first RESIZE to square
      bb = CreateScaledBitmap (SBMP, sqp, sqp, True)
  End If

  bb.InitializeMutable(sqp,sqp)
  Recto.Initialize(0, 0,sqp,sqp)

  'ROTATE masterPic into bb
  c.Initialize2(bb)
  c.DrawBitmapRotated(SBMP, Null, Recto, angle) 'Draw the new image
  SBMP.Initialize3(bb)

  'RESIZE back inverse, if pic was not square
  If pwo <> pho Then
      SBMP = CreateScaledBitmap (bb, pho, pwo, True)
  End If
  Return SBMP
End Sub

Sub CreateScaledBitmap(Original As Bitmap, Widths As Int, Heights As Int, Filters As Boolean) As Bitmap
Dim R As Reflector
Dim b As Bitmap
b = R.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
Array As Object(Original, Widths, Heights, Filters), _
Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
Return b
End Sub
 
Last edited:
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
Hi,
Images made in portrait orientation will be shown in portrait mode in the Gallery or PC
It is an old thread, but anyway I have a workaround for Samsung Galaxy Note2 (and others?)

The exif metadata [exif.getAttribute(exif.TAG_ORIENTATION)] shows a 6, but the picture is not rotated (showing in Landscape on PC or gallery)

The solution is to check the orientation of the phone in the activity create. Orientation=0 means Portrait.
In sub Camera1_PictureTaken check the height/width ratio. If ratio < 1 and orientation = 0 then rotate picture 90 degrees.

I have added the code involved.

Hope this helps someone. I took me an awfull long time to scrape these code/lines together.....

Regards, Jur


Code:
Sub GetOrientation AsInt' 0 = portrait
' 1 = landscape left
' 3 = landscape rightDim R AsReflector
R.Target = R.GetContext
R.Target = R.RunMethod2("getSystemService", "window", "java.lang.String")
R.Target = R.RunMethod("getDefaultDisplay")Return R.RunMethod("getOrientation")End SubSub Camera1_PictureTaken (Data() AsByte)DimInAsInputStreamDim Out AsOutputStreamDim drect AsRect' make a bitmap of the data stream from the cameraIn.InitializeFromBytesArray(Data, 0, Data.Length)Dim bmp, bmp2 AsBitmap
bmp.Initialize2(In)
bmp2.Initialize3(bmp)In.Close' check orientation and height/width ratio of the pictureIf bmp.Width > bmp.Height AND Orientation = 0Then' portrait oriention, but landscape photo RESU = RotateBitmap90(bmp) 'RESU met resultaat Out = File.OpenOutput(Imagefolder, bestand, False)
RESU.WriteToStream(Out, 100, "JPEG")Else
camera1.SavePictureToFile(Data, Imagefolder, bestand)EndIf
camera1.StartPreviewEndSub

this code works on portrait
but u know make this work on ladscape?

i m testing on galaxy s3, with camex galaxy s3 save exif = 6 ever
how can i know if the photo is
' 1 = landscape left
' 3 = landscape right
?
 
Upvote 0
Top