Android Question how to get Image orientation?

FrankBerra

Active Member
Licensed User
Longtime User
Hello forum
I am using ExifInterface4a (https://www.b4x.com/android/forum/threads/android-support-exifinterface.74629/) for getting the image orientation.
While i am getting right exif data with the stock gallery app i can't get the same exif data with the ExifInterface library.

Here is my code:
B4X:
Dim input As InputStream = File.OpenInput(dir, FileName) 'https://www.b4x.com/android/forum/threads/get-the-path-to-media-files-returned-from-contentchooser.39313/page-2
           
Dim Out As OutputStream
Out = File.OpenOutput(File.DirInternal, "Original.jpg", False)
File.Copy2(input, Out)
           
Out.Close
input.Close
           
          
Dim filePath As String
filePath = File.Combine(File.DirInternal, "Original.jpg")
exif.Initialize("",filePath)
           
Log("Exif Data: " & exif.GetAllAttributes)

What's wrong?


This is what i get:
B4X:
Exif Data: (MyMap) {DateTime=null, ExposureTime=null, Flash=null, FocalLength=null, GPSAltitude=null, GPSAltitudeRef=null, GPSDateStamp=null, GPSLatitude=null, GPSLatitudeRef=null, GPSLongitude=null, GPSLongitudeRef=null, GPSProcessingMethod=null, GPSTimeStamp=null, ImageLength=0, ImageWidth=0, Make=null, Model=null, Orientation=0, WhiteBalance=null, Artist=null, BitsPerSample=null, BrightnessValue=null, CFAPattern=null, ColorSpace=null, ComponentsConfiguration=null, CompressedBitsPerPixel=null, Compression=null, Contrast=null, Copyright=null, CustomRendered=null, DateTimeDigitized=null, DateTimeOriginal=null, DeviceSettingDescription=null, DigitalZoomRatio=null, ExifVersion=null, ExposureBiasValue=null, ExposureIndex=null, ExposureMode=null, ExposureProgram=null, FileSource=null, FlashpixVersion=null, FlashEnergy=null, FocalLengthIn35mmFilm=null, FocalPlaneResolutionUnit=null, FocalPlaneXResolution=null, FocalPlaneYResolution=null, FNumber=null, GainControl=null, GPSAreaInformation=null, GPSDestBearing=null, GPSDestBearingRef=null, GPSDestDistance=null, GPSDestDistanceRef=null, GPSDestLatitude=null, GPSDestLatitudeRef=null, GPSDestLongitude=null, GPSDestLongitudeRef=null, GPSDifferential=null, GPSDOP=null, GPSImgDirection=null, GPSImgDirectionRef=null, GPSMapDatum=null, GPSMeasureMode=null, GPSSatellites=null, GPSSpeed=null, GPSSpeedRef=null, GPSStatus=null, GPSTrack=null, GPSTrackRef=null, GPSVersionID=null, ImageDescription=null, ImageUniqueID=null, InteroperabilityIndex=null, ISOSpeedRatings=null, JPEGInterchangeFormat=null, JPEGInterchangeFormatLength=null, LightSource=0, MakerNote=null, MaxApertureValue=null, MeteringMode=null, OECF=null, PhotometricInterpretation=null, PixelXDimension=null, PixelYDimension=null, PlanarConfiguration=null, PrimaryChromaticities=null, ReferenceBlackWhite=null, RelatedSoundFile=null, ResolutionUnit=null, RowsPerStrip=null, SamplesPerPixel=null, Saturation=null, SceneCaptureType=null, SceneType=null, SensingMethod=null, Sharpness=null, ShutterSpeedValue=null, Software=null, SpatialFrequencyResponse=null, SpectralSensitivity=null, StripByteCounts=null, StripOffsets=null, SubjectArea=null, SubjectDistance=null, SubjectDistanceRange=null, SubjectLocation=null, ApertureValue=null, SubSecTimeDigitized=null, SubSecTimeOriginal=null, ThumbnailImageLength=null, ThumbnailImageWidth=null, TransferFunction=null, UserComment=null, WhitePoint=null, XResolution=null, YCbCrCoefficients=null, YCbCrPositioning=null, YCbCrSubSampling=null, YResolution=null}
[Code]
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
This can serve you:
B4X:
Sub LoadbitmapSample2(Dir As String,Filename As String,AutoRotate As Boolean) As Bitmap 'ignore
    Dim bm As Bitmap
    bm=LoadBitmapSample(Dir,Filename,100%x,100%y)
    If AutoRotate Then
        Dim bm2 As BitmapExtended
        bm2.Initialize("")
        Dim exifdata1 As ExifData
        Try
            exifdata1.Initialize(Dir,Filename)
            'http://sylvana.net/jpegcrop/exif_orientation.html
            ' 1) transform="";;
            ' 2) transform="-flip horizontal";;
            ' 3) transform="-rotate 180";;
            ' 4) transform="-flip vertical";;
            ' 5) transform="-transpose";;
            ' 6) transform="-rotate 90";;
            ' 7) transform="-transverse";;
            ' 8) transform="-rotate 270";;
            Select Case exifdata1.getAttribute(exifdata1.TAG_ORIENTATION)
                Case exifdata1.ORIENTATION_ROTATE_180 '3
                    bm=bm2.rotateBitmap(bm,180)
                Case exifdata1.ORIENTATION_ROTATE_90 '6
                    bm=bm2.rotateBitmap(bm,90)
                Case exifdata1.ORIENTATION_ROTATE_270 '8
                    bm=bm2.rotateBitmap(bm,270)
            End Select
        Catch
            Log(LastException)
        End Try
    End If
    Return bm
End Sub
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Your suggested code still gives me "Orientation: 0" instead of value 6.

B4X:
Dim input As InputStream = File.OpenInput(dir, FileName) 'https://www.b4x.com/android/forum/threads/get-the-path-to-media-files-returned-from-contentchooser.39313/page-2
           
Dim Out As OutputStream
Out = File.OpenOutput(File.DirInternal, "Copied.jpg", False)
File.Copy2(input, Out)
           
Out.Close
input.Close
  

Dim exifdata1 As ExifData
               Try
                   exifdata1.Initialize(File.DirInternal, "Copied.jpg")
               
                   'http://sylvana.net/jpegcrop/exif_orientation.html
                   ' 1) transform="";;
                   ' 2) transform="-flip horizontal";;
                   ' 3) transform="-rotate 180";;
                   ' 4) transform="-flip vertical";;"
                   ' 5) transform="-transpose";;
                   ' 6) transform="-rotate 90";;
                   ' 7) transform="-transverse";;
                   ' 8) transform="-rotate 270";;
               Log("Orientation: " & exifdata1.getAttribute(exifdata1.TAG_ORIENTATION))
                  
               Select Case exifdata1.getAttribute(exifdata1.TAG_ORIENTATION)
                       Case exifdata1.ORIENTATION_ROTATE_180 '3
                       Log("ORIENTATION_ROTATE_180")
                           'bm=bm2.rotateBitmap(bm,180)
                       Case exifdata1.ORIENTATION_ROTATE_90 '6
                       Log("ORIENTATION_ROTATE_90")
                           'bm=bm2.rotateBitmap(bm,90)
                       Case exifdata1.ORIENTATION_ROTATE_270 '8
                       Log("ORIENTATION_ROTATE_270")
                           'bm=bm2.rotateBitmap(bm,270)
                   End Select
               Catch
                   Log(LastException)
               End Try

But if i add the picture to assets folder and then i read the exif data everything works correctly:
B4X:
File.Copy(File.DirAssets, "test.jpg", File.DirInternal, "test.jpg")
exifdata1.Initialize(File.DirInternal, "test.jpg")

Is it possible that copied image (Inputstream to OutputStream) has lost Exif information during copy?
 
Upvote 0

mjcoon

Well-Known Member
Licensed User
Your suggested code still gives me "Orientation: 0" instead of value 6.

B4X:
Dim input As InputStream = File.OpenInput(dir, FileName) 'https://www.b4x.com/android/forum/threads/get-the-path-to-media-files-returned-from-contentchooser.39313/page-2
          
Dim Out As OutputStream
Out = File.OpenOutput(File.DirInternal, "Copied.jpg", False)
File.Copy2(input, Out)
          
Out.Close
input.Close
 

Dim exifdata1 As ExifData
               Try
                   exifdata1.Initialize(File.DirInternal, "Copied.jpg")
              
                   'http://sylvana.net/jpegcrop/exif_orientation.html
                   ' 1) transform="";;
                   ' 2) transform="-flip horizontal";;
                   ' 3) transform="-rotate 180";;
                   ' 4) transform="-flip vertical";;"
                   ' 5) transform="-transpose";;
                   ' 6) transform="-rotate 90";;
                   ' 7) transform="-transverse";;
                   ' 8) transform="-rotate 270";;
               Log("Orientation: " & exifdata1.getAttribute(exifdata1.TAG_ORIENTATION))
                 
               Select Case exifdata1.getAttribute(exifdata1.TAG_ORIENTATION)
                       Case exifdata1.ORIENTATION_ROTATE_180 '3
                       Log("ORIENTATION_ROTATE_180")
                           'bm=bm2.rotateBitmap(bm,180)
                       Case exifdata1.ORIENTATION_ROTATE_90 '6
                       Log("ORIENTATION_ROTATE_90")
                           'bm=bm2.rotateBitmap(bm,90)
                       Case exifdata1.ORIENTATION_ROTATE_270 '8
                       Log("ORIENTATION_ROTATE_270")
                           'bm=bm2.rotateBitmap(bm,270)
                   End Select
               Catch
                   Log(LastException)
               End Try

But if i add the picture to assets folder and then i read the exif data everything works correctly:
B4X:
File.Copy(File.DirAssets, "test.jpg", File.DirInternal, "test.jpg")
exifdata1.Initialize(File.DirInternal, "test.jpg")

Is it possible that copied image (Inputstream to OutputStream) has lost Exif information during copy?
What no-one has determined an answer in three years?

Unfortunately my use of this splendid library crashes at the .Initialize() method call although I can use the similar ExifData objrect within the JpegUtils library
 
Upvote 0
Top