Ottenere Metadati Exif da un'immagine JPEG con le risorse del API di libexif

Da Gambas-it.org - Wikipedia.

Il formato di file JPEG consente di incorporare informazioni aggiuntive chiamate "metadati" nell'intestazione del file. Lo scopo di questi metadati è di fornire informazioni aggiuntive e utili insieme all'immagine.

E' possibile estrarre tali Metadati, eventualmente contenuti in un file immagine JPEG usando le risorse della libreria Libexif.

E' necessario avere installata nel sistemae richiamare in gambas la seguente libreria condivisa: "libexif.so.12.3.3"


Mostriamo un semplice esempio:

Private content_count As Integer
Private ifd As Integer
Private entry_count As Integer


Library "libexif:12.3.3"

Public Struct ExifData
  ifd[5] As Pointer
  data As Pointer
  size As Integer
  priv As Pointer
End Struct

Public Struct ExifEntry
  tag As Integer
  format_ As Integer
  components As Long
  data As Pointer
  size As Integer
  parent As Pointer
  priv As Pointer
End Struct

' ExifData *exif_data_new_from_file (const char *path)
' Allocate a new #Pointer and load EXIF data from a JPEG file.
Private Extern exif_data_new_from_file(path As String) As ExifData

' ExifByteOrder exif_data_get_byte_order  (ExifData *data)
' Return the byte order in use by this EXIF structure.
Private Extern exif_data_get_byte_order(data As ExifData) As Integer

' const char *exif_byte_order_get_name (ExifByteOrder order)
' Return a short, localized, textual name for the given byte order.
Private Extern exif_byte_order_get_name(order As Integer) As String

' void exif_data_foreach_content (ExifData *data, PointerForeachContentFunc func, void *user_data)
' Execute a function on each IFD in turn.
Private Extern exif_data_foreach_content(data As ExifData, func As Pointer, user_data As Pointer)

' void exif_content_foreach_entry (ExifContent *content, ExifContentForeachEntryFunc func, void *user_data)
' Executes function on each EXIF tag in this IFD in turn.
Private Extern exif_content_foreach_entry(content As Pointer, func As Pointer, user_data As Pointer)

' ExifIfd exif_content_get_ifd (ExifContent *c)
' Return the IFD number in which the given #ExifContent is found.
Private Extern exif_content_get_ifd(c As Pointer) As Integer

' const char *exif_entry_get_value (ExifEntry *entry, char *val, unsigned int maxlen)
' Return a localized textual representation of the value of the EXIF entry.
Private Extern exif_entry_get_value(entry As ExifEntry, _val As Pointer, maxlen As Integer) As String

' const char *exif_tag_get_name_in_ifd(ExifTag tag, ExifIfd ifd)
' Return a textual name of the given tag when found in the given IFD.
Private Extern exif_tag_get_name_in_ifd(tag As Integer, ifd As Integer) As String

' const char *exif_format_get_name (ExifFormat format)
' Return a textual representation of the given EXIF data type.
Private Extern exif_format_get_name(format_ As Integer) As String

' void exif_data_unref (ExifData *data)
Private Extern exif_data_unref(data As ExifData)


Public Sub Main()
 
 Dim ed As ExifData
 Dim jpg As String
 Dim callback_data As Pointer
 
 jpg = "/percorso/del/file.jpg"
 Print "File immagine:   "; jpg
 
 ed = exif_data_new_from_file(jpg)
 If IsNull(ed) Then Error.Raise("Errore !")
 
 Print "Ordine dei byte: "; exif_byte_order_get_name(exif_data_get_byte_order(ed))
 
 exif_data_foreach_content(ed, data_foreach_func, callback_data)
 
 exif_data_unref(ed)
  
End


Private Function data_foreach_func(content As Pointer, callback_data As Pointer)
 
 ifd = exif_content_get_ifd(content)
 Print "  Content :"; content_count, "ifd= "; ifd
 
 exif_content_foreach_entry(content, content_foreach_func, callback_data)
 
 Inc content_count
  

End


Private Function content_foreach_func(entry As Pointer, callback_data As Pointer)
 
 Dim buf As New Byte[2000]
 Dim ent As ExifEntry
 
 ent = entry
 
 exif_entry_get_value(entry, buf.Data, buf.Count)
 
 Print "   Entry"; entry_count; ": "; exif_tag_get_name_in_ifd(ent.tag, ifd); "("; exif_format_get_name(ent.format_); ")"
 Print "      Size, Comps: "; ent.size; ", "; ent.components
 Print "      Value: "; exif_entry_get_value(ent, buf.Data, buf.Count)
 Inc entry_count
 
End


Riferimenti