Camera Orientation - Preview vs PictureTaken

Wembly

Member
Licensed User
Longtime User
Hi

I hope someone can make sense of this please I'm proper head scratching now...

Firstly the phone I'm using is a Galaxy Note 2 just in case this is hardware specific.

I've used various camera libraries Camera v2.20 & ACL 4.60 both same result.

The app presents the camera preview to an image view without issue correct orientation (portrait) however when the user takes a picture, the captured imaged recieved from the PictureTaken event is now rotated 90 degress clockwise.

It would seem the issue is I'm not using the natural orientation of the phone which is landscape so even though the preview orientation can be changed, when the PictureTaken event is raised it provides the taken image in the natural orientation regardless.

Is this correct or am I missing something???

Thanks.
 
Last edited:

gadgetmonster

Active Member
Licensed User
Longtime User
Hi,

Using the demo app that comes with the camera lib I did the following. First added an imageview to the main display just to test the results.

I also added the JPegUtils and RSImage Processing libraries to my app.

Added the following function to my code:

B4X:
Sub rotateBy (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)
         
   ' // Get orientation
   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

And then in the Camera1_PictureTaken function after the line

B4X:
camEx.SavePictureToFile(Data, dir, filename)

I added:

B4X:
   Dim RSIE As RSImageEffects
   Dim rotate As Int = rotateBy(dir, filename)   
   Dim tmpBitmap As Bitmap 
   
   tmpBitmap = LoadBitmap(dir, filename )   
   ImageView1.Gravity = Gravity.NO_GRAVITY
   ImageView1.Bitmap = RSIE.rotate(tmpBitmap, rotate)

This simply uses the exif data contained in the saved image and works out the number of degrees to rotate the image by. I then load the image into a temporary bitmap field and rotate it using RSImage library. Finally I put the resulting bitmap into the imageview to test the results.

There may well be a better way of doing this without having to save the file first.
 
Upvote 0

Wembly

Member
Licensed User
Longtime User
Thanks Paul.

The above only works with CameraEx Class or if only using the Camera lib 2.20 then must use initialize2 not initialize.
 
Upvote 0
Top