Android Question Camera orientations?

gehrlekrona

Member
Licensed User
Longtime User
Has anybody got the camera orientations to work? I am using the ACL and whenever I take a picture in portrait it saves it rotated. I have search the forum and tried almost everything but I find it impossible to get it to work. The last thing I tried was to use TakePicture2(90) but then the program stops with an error message "Unfortunately the program has stopped". I am checking if the screen Activity.Width > Activity.Height to set the right orientation but it still saves it rotated. It seems like ti is the portrait that is causing all the errors/problems, so has anybody got a fix for it? Does someone has an actual code that works?
 

gehrlekrona

Member
Licensed User
Longtime User
Thanks, but it didn't really help since my problem is (I think) when I save it. It looks right in the preview. You would think that someone here solved the problem. I can't figure it out and like I said, I have tried all the solutions people have posted here but to no avail. I am not sure why it is such a problem and it seems like a lot of people have/had the same problem. Maybe mine is different since none of the solutions work.
 
Upvote 0

strat

Active Member
Licensed User
Longtime User
You can use this subs to find a picture's exif data. Then, rotate the picture needed angle.
You need jpegutils library to run this.


B4X:
Sub button1_click
    masterpic.Initialize(dirname,filename)
    angle=findangle(dirname,filename)
    rotatepicture(angle)
    ImageView.Bitmap=masterpic
End Sub


Sub findangle(dir As String, Filename As String) As Int
  Dim retValue As Int = 0
  Dim filepath As String
  Dim exif As ExifData
  filepath = dir & "/" & Filename
  exif.Initialize("", filepath)
  Dim w As Int = exif.getAttributeInt (exif.TAG_ORIENTATION, 0)
  If w = exif.ORIENTATION_NORMAL Then retValue = 0
  If w = exif.ORIENTATION_ROTATE_90  Then retValue = 90
  If w = exif.ORIENTATION_ROTATE_180  Then retValue = 180
  If w = exif.ORIENTATION_ROTATE_270  Then retValue = 270
  Return retValue
End Sub

Sub rotatepicture( angle As Int)
  Dim bb As Bitmap
  Dim pwo As Int
  Dim sqp As Int
  Dim pho As Int
  Dim recto As Rect
  Dim c As Canvas

  pwo = masterpic.Width
  pho = masterpic.Height
  'pwo=100
  'pho=133
  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 (masterpic, sqp, sqp, True)
  End If

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

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

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

End Sub

Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
  Dim rf As Reflector
  Dim bm As Bitmap
  bm = rf.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
  Array As Object(Original, Width, Height, Filter), _
  Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
  Return bm
End Sub
 
Upvote 0

gehrlekrona

Member
Licensed User
Longtime User
CameraEx is for newer version of B4A than I have. I will try the subs. can snyone explain why we are having problems?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…