Android Question image orientation from CameraIntent

peacemaker

Expert
Licensed User
Longtime User
HI, All

Any fresh solution for correct photo orientation got from the camera intent ?
CameraIntent sample gives the overturned photo on ImageView, if it was shot in the landscape mode.

I guess, it must be JPG EXIF info is checked firstly, but no sample was found on the forum.
 

Semen Matusovskiy

Well-Known Member
Licensed User
I used Andrew Graham's jpegutils https://www.b4x.com/android/forum/threads/jpegutils-library-gives-access-to-exif-data.11629/#content

There some global variables in code bellow, but I think, in general should be clear.

One subrotine reads a file and returns rotated bitmap (can be used in imageview)
B4X:
Sub Activity_subGetRotatedBitmap As Bitmap

    Dim bitmapOriginal                                                          As Bitmap
    Dim bitmapRotated                                                           As Bitmap
    Dim exifdataInstance                                                        As ExifData

    Try
        booleanNeedToWriteRotatedBitmap = False
        exifdataInstance.Initialize (moduleCommon.stringTaskDirectory, "Photo_" & AnswerQuestions.intPhotoOrdinal & ".jpg")
        bitmapOriginal.Initialize   (moduleCommon.stringTaskDirectory, "Photo_" & AnswerQuestions.intPhotoOrdinal & ".jpg")
        Select Case exifdataInstance.getAttribute (exifdataInstance.TAG_ORIENTATION)
            Case exifdataInstance.ORIENTATION_ROTATE_90
                bitmapRotated = bitmapOriginal.rotate (90)
            Case exifdataInstance.ORIENTATION_ROTATE_180
                bitmapRotated = bitmapOriginal.rotate (180)
            Case exifdataInstance.ORIENTATION_ROTATE_270
                bitmapRotated = bitmapOriginal.rotate (270)
            Case Else
                Return bitmapOriginal
        End Select
        booleanNeedToWriteRotatedBitmap = True
        Return bitmapRotated
    Catch
        booleanError = True
        Return Null
    End Try

End Sub

Another subrotine updates jpg-file and sets "normal" orientation
I did so, because stupid webserver was lazy and did not want to rotate, using EXIF.

B4X:
Sub Activity_WriteRotatedBitmap (bitmapRotated As Bitmap)

    Dim exifdataInstance                                                        As ExifData
    Dim outputstreamInstance                                                    As OutputStream

    Try
        outputstreamInstance = File.OpenOutput (moduleCommon.stringTaskDirectory, "Photo_" & AnswerQuestions.intPhotoOrdinal & ".jpg", False)
        bitmapRotated.WriteToStream (outputstreamInstance, 100, "JPEG")
        outputstreamInstance.Close

        exifdataInstance.Initialize (moduleCommon.stringTaskDirectory, "Photo_" & AnswerQuestions.intPhotoOrdinal & ".jpg")
        exifdataInstance.setAttribute (exifdataInstance.TAG_ORIENTATION, exifdataInstance.ORIENTATION_NORMAL)
        exifdataInstance.saveAttributes
    Catch
        booleanError = True
    End Try

End Sub
 
Upvote 0
Top