Android Question Picture always in landscape mode

Ivan Aldaz

Member
Licensed User
Longtime User
Hi

I'm using CameraEx class in my app. When the picture is taken, it's shown properly (portrait when captured in portrait mode, landscape when captured in landscape mode) in three devices I've tested (chinese tablet (API 22), mobile phone LG G5 (API 24), tablet Lenovo A7600-F (API 19)), but when tested on an Asus Zenfone 2 (API 23) the image is shown always in landscape mode. I don't know if it's related with the API, with the processor (Asus Zenfone 2 has an Intel processor), or what.

Any ideas?
 

Ivan Aldaz

Member
Licensed User
Longtime User
Hi, Marco, and thanks

I've been reading that post, but think the same as @Eduard in post #73 of the linked thread: it should be fixed in Camera or CameraEx library (when writing exif values?). I am showing images with JSTouchImageView library, and any image I use here taken from any other camera app is presented properly, as well as pictures taken with the CameraEx library don't look good in other gallery apps either.

Anyway, I've tried the solution in post #79 , but no luck, because not always works. I'm trying now EZCamera lilbrary, so that my app uses another camera app to take pictures, but for the moment it's not totally controlled: it closes if not used in about 5 sec., I haven't found the way to take several pictures without having to press a button to show the cam before taking each picture, and some other issues I think that could be solved by using intent parameters (that I don't know how or where to find), but these are questions for another thread...
 
Upvote 0

Ivan Aldaz

Member
Licensed User
Longtime User
Mmmmmm... Now I'm thinking that perhaps the best solution for the moment is to rotate bitmap just after the capture of a picture. First, read the exif "Orientation" value, then rotate as indicated in post #79, and then write the new bitmap to a file and delete the original. Doing this, sliding images would be more fluid. It would be important to detect the property or reason that makes a device not to present pictures properly, so that this operation would only be done in these devices, but think it's not going to be easy.

I'll try and tell
 
Upvote 0

Ivan Aldaz

Member
Licensed User
Longtime User
Well, it's done , the way I said in my previous post.

After saving the picture from the camera with:

B4X:
Dim out As OutputStream = File.OpenOutput(dir, filename, False)
     out.WriteBytes(Data, 0, Data.Length)
     out.Close

...using the code in that 'famous' post #79, we analyze the exif "Orientation" value in the file saved, and then rotate it, if necessary:

B4X:
  Dim bm As Bitmap
        bm.Initialize(dir, filename) 'Saved picture
  
   Dim bm2 As BitmapExtended
   Dim exifdata1 As ExifData

   Try
     exifdata1.Initialize(dir, filename)

     Select Case exifdata1.getAttribute(exifdata1.TAG_ORIENTATION)
        
       Case exifdata1.ORIENTATION_ROTATE_180 '3.- transform = rotate 180
         bm=bm2.rotateBitmap(bm,180)
         saveRotatedPicture(bm, dir, filename)
        
       Case exifdata1.ORIENTATION_ROTATE_90  '6.- transform = rotate 90
         bm=bm2.rotateBitmap(bm,90)
         saveRotatedPicture(bm, dir, filename)
        
       Case exifdata1.ORIENTATION_ROTATE_270 '8.- transform = rotate 270"
         bm=bm2.rotateBitmap(bm,270)
         saveRotatedPicture(bm, dir, filename)
          
     End Select
      
   Catch
      
   End Try

...and now save the image.

We can modify (or not) exif "Orientation" value in the new file. In all the devices I've tested is always "1"

B4X:
Sub saveRotatedPicture(bm1 As Bitmap, dir1 As String, filename1 As String)
  
'Delete old picture is not necessary, it's overwritten when saving new:
'   File.Delete(dir1, filename1)
     
   Dim out As OutputStream
        out = File.OpenOutput(dir1, filename1, False)
        bm1.WriteToStream(out, 100, "JPEG")
        out.Close

'Modify exif "Orientation" data in new file:
   Dim exif1 As ExifData
        exif1.Initialize(dir1, filename1)
        exif1.setAttribute("Orientation", 1)
        exif1.saveAttributes

End Sub

Don't like very much this solution, but for the moment it works, and frees my mind. Hope some day there will be a more direct method, or better, no need for a method like this .

Hope this helps someone else.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…