Android Question Lib\class for meta-info about any image file ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Maybe anyone touched this topic ?
To check\analyze the file if it is an image and return meta info map of the file, including EXIF, if available.

I have checked:

1. jMagicImage lib (works under Android too) https://www.b4x.com/android/forum/t...s-image-also-some-basic-image-filters.130857/
It's return null object error on .BMP files

2. Android ExifInterface: https://www.b4x.com/android/forum/threads/android-support-exifinterface.74629/
Works OK, only for files that have EXIF data. That is also empty for non-supporting files.

Any suggestions more?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I have prepared some class "imgmeta" and trying to collect images info:

B4X:
Private Sub Class_Globals
    #if B4A
    Private exif As ExifInterface   'lib
    #End If
    Private raf As RandomAccessFile
    Private MagicImage1 As jMagicImage   'lib
    Private bmplus As BitmapPlus   'lib
    Dim metadata As List
    Dim metadata_exif As Map
    
    Dim fullpath As String
    Dim dir As String
    Dim filename As String
    Dim size As Long
    Dim modified As Long
    Dim mime As String
    Dim format As String
    Dim width As Int
    Dim height As Int
    Dim bits_per_pixel As Int
    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(file_fullpath As String) As Boolean
    metadata.Initialize
    metadata_exif.Initialize
    MagicImage1.Initialize
    width = 0
    height = 0
    mime = "?"
    
    fullpath = file_fullpath
    dir = GetPath(fullpath)
    filename = GetFileName(fullpath)
    
    If File.Exists("", file_fullpath) = False Then
        Log("File does NOT EXIST !!!")
        Return False
    End If
    
    size = File.Size(dir, filename)
    modified = File.LastModified(dir, filename)
'    Log("size = " & size)

    If size < 2 Then
        Return False
    End If
    
    raf.Initialize(dir, filename, True)
    Dim buf(2) As Byte
    raf.ReadBytes(buf, 0, 2, 0)
    raf.Close
    Dim prefix As String = BytesToString(buf, 0, buf.Length, "UTF8")    '"BM" for BMP
    If prefix = "BM" Then
        mime = "image/bmp"
    End If

'    Dim dimensions As Map = bmplus.GetImageDimensions(fullpath)
'    If dimensions.IsInitialized Then
'        If dimensions.ContainsKey("Width") And dimensions.ContainsKey("Height") Then
'            width = dimensions.Get("Width")
'            height = dimensions.Get("Height")
'        Else
'            Log("Unknown format: Error of getting image dimensions !!!")
'            Return True
'        End If
'    Else
'        Return False
'    End If
    
    Dim extension As String = filename.SubString2(filename.Length - 4, filename.Length)
'    Log(extension)
    Dim isBMP As Boolean = extension.ToLowerCase = ".bmp"   
    If isBMP And prefix = "BM" Then
        mime = "image/bmp"
        Return True
    Else If isBMP And prefix <> "BM" Then
        Return False
    End If
    
    Dim JsonData As String = MagicImage1.ReadMetadataFromFile(fullpath)
    Dim js As JSONParser
    js.Initialize(JsonData)
    Dim whole As Map = js.NextObject
    Dim basic As Map = whole.Get("Basic")
    'magic basic: (MyMap) {Mime Type=image/png, Number of Images=1, Format=PNG, Format Details=Png, Width=3600, Color Type=RGB, Compression Algorithm=PNG Filter, Bits Per Pixel=32, Height=3600}
    'magic basic: (MyMap) {Mime Type=image/jpeg, Number of Images=1, Format=JPEG, Format Details=Jpeg/JFIF v.1.1, Width=1280, Color Type=YCbCr, Compression Algorithm=JPEG, Bits Per Pixel=24, Height=720}
    'magic basic: (MyMap) {Mime Type=image/jpeg, Number of Images=1, Format=JPEG, Format Details=Jpeg/DCM, Width=1280, Width DPI=72, Color Type=YCbCr, Height Inch=10.0, Height DPI=72, Compression Algorithm=JPEG, Width Inch=17.777779, Bits Per Pixel=24, Height=720}
    If basic.ContainsKey("Mime Type") Then
        mime = basic.Get("Mime Type")
    End If
    If basic.ContainsKey("Format") Then
        format = basic.Get("Format")
    End If
    If basic.ContainsKey("Bits Per Pixel") Then
        bits_per_pixel = basic.Get("Bits Per Pixel")
    End If
    If basic.ContainsKey("Width") Then
        width = basic.Get("Width")
    End If
    If basic.ContainsKey("Height") Then
        height = basic.Get("Height")
    End If
    
    
    metadata = whole.Get("Metadata")
    '(ArrayList) [{name=Orientation, type=Integer, id=274, value=1}, {name=XResolution, type=Real, id=282, value=72.0}, {name=YResolution, type=Real, id=283, value=72.0}, {name=ResolutionUnit, type=Integer, id=296, value=2}, {name=Software, type=String, id=305, value=Adobe Photoshop CC 2019 (Windows)}, {name=DateTime, type=String, id=306, value=2019:09:23 10:40:07}, {name=ExifOffset, type=Integer, id=34665, value=168}, {name=ColorSpace, type=Integer, id=40961, value=1}, {name=ExifImageWidth, type=Integer, id=40962, value=1280}, {name=ExifImageLength, type=Integer, id=40963, value=720}, {name=Compression, type=Integer, id=259, value=6}, {name=XResolution, type=Real, id=282, value=72.0}, {name=YResolution, type=Real, id=283, value=72.0}, {name=ResolutionUnit, type=Integer, id=296, value=2}, {name=JpgFromRawStart, type=Integer, id=513, value=306}, {name=JpgFromRawLength, type=Integer, id=514, value=3387}]
    If metadata.Size > 0 Then
        Log("Metadata exists !")
    End If
    

    exif.Initialize("exif", fullpath)
    metadata_exif = exif.GetAllAttributes2
    'exif attr: (MyMap) {DateTime=2019:09:23 10:40:07, 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=720, ImageWidth=1280, Make=null, Model=null, Orientation=1, WhiteBalance=null, Artist=null, BitsPerSample=null, BrightnessValue=null, CFAPattern=null, ColorSpace=1, ComponentsConfiguration=null, CompressedBitsPerPixel=null, Compression=6, 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=306, JPEGInterchangeFormatLength=3387, LightSource=0, MakerNote=null, MaxApertureValue=null, MeteringMode=null, OECF=null, PhotometricInterpretation=null, PixelXDimension=1280, PixelYDimension=720, PlanarConfiguration=null, PrimaryChromaticities=null, ReferenceBlackWhite=null, RelatedSoundFile=null, ResolutionUnit=2, RowsPerStrip=null, SamplesPerPixel=null, Saturation=null, SceneCaptureType=null, SceneType=null, SensingMethod=null, Sharpness=null, ShutterSpeedValue=null, Software=Adobe Photoshop CC 2019 (Windows), 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=720000/10000, YCbCrCoefficients=null, YCbCrPositioning=null, YCbCrSubSampling=null, YResolution=720000/10000}
    'exif attr: (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=720, ImageWidth=1280, 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}
    'exif attr: (MyMap) {DateTime=2019:09:23 10:40:26, 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=720, ImageWidth=1280, Make=null, Model=null, Orientation=1, WhiteBalance=null, Artist=null, BitsPerSample=null, BrightnessValue=null, CFAPattern=null, ColorSpace=1, ComponentsConfiguration=null, CompressedBitsPerPixel=null, Compression=6, 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=306, JPEGInterchangeFormatLength=4358, LightSource=0, MakerNote=null, MaxApertureValue=null, MeteringMode=null, OECF=null, PhotometricInterpretation=null, PixelXDimension=1280, PixelYDimension=720, PlanarConfiguration=null, PrimaryChromaticities=null, ReferenceBlackWhite=null, RelatedSoundFile=null, ResolutionUnit=2, RowsPerStrip=null, SamplesPerPixel=null, Saturation=null, SceneCaptureType=null, SceneType=null, SensingMethod=null, Sharpness=null, ShutterSpeedValue=null, Software=Adobe Photoshop CC 2019 (Windows), 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=720000/10000, YCbCrCoefficients=null, YCbCrPositioning=null, YCbCrSubSampling=null, YResolution=720000/10000}

    If metadata_exif.ContainsKey("ImageWidth") Then
        width = basic.Get("ImageWidth")
    End If
    If metadata_exif.ContainsKey("ImageLength") Then
        height = basic.Get("ImageLength")
    End If

'    Dim a As String = exif.getAttribute("DateTimeDigitized")
'    Dim d1 As Long
'    If a <> Null And a.Contains(":") = False Then
'        d1 = a
'    End If
'    If d1 > 0 Then
'        modified = d1
'        Log("modified = exif.DateTime")
'    End If
    
'    Dim d2 As Long = exif.GpsDateTime
'    If d2 > 0 Then
'        modified = d2
'        Log("modified = exif.GpsDateTime")
'    End If
    Return True
End Sub

Sub getAll As Map
    Dim all_metainfo As Map
    all_metainfo.Initialize
    all_metainfo.Put("filename", filename)
    all_metainfo.Put("mime", mime)
    all_metainfo.Put("modified", modified)
    all_metainfo.Put("size", size)
    all_metainfo.Put("format", format)
    all_metainfo.Put("width", width)
    all_metainfo.Put("height", height)
    all_metainfo.Put("bits_per_pixel", bits_per_pixel)
    all_metainfo.Put("fullpath", fullpath)
    all_metainfo.Put("dir", dir)
    
    If metadata.IsInitialized Then all_metainfo.Put("metadata", metadata)
    If metadata_exif.IsInitialized Then all_metainfo.Put("metadata_exif", metadata_exif)
    Return all_metainfo
End Sub

Sub GetFileName(fullpath1 As String) As String
    #if B4A
        Return fullpath1.SubString(fullpath1.LastIndexOf("/") + 1)
    #else if B4J
        Return File.GetName(fullpath1)
    #end if
End Sub

Sub GetPath(Path As String) As String
    #if B4A
        Dim Path1 As String
        If Path = "/" Then
            Return "/"
        End If
        Dim L As Int = Path.LastIndexOf("/")
        If L = Path.Length - 1 Then
            'Strip the last slash
            Path1 = Path.SubString2(0,L)
        Else
            Path1 = Path
        End If
        L = Path.LastIndexOf("/")
        If L = 0 Then
            L = 1
        End If
        Return Path1.SubString2(0,L)
    #else if B4J
        Return File.GetFileParent(Path)
    #end if
End Sub
 
Upvote 0
Top