Search

Atalasoft Knowledge Base

HOWTO: Parse (Exif) Metadata From HEIF/HEIC FIles

Tananda
DotImage

With the introduction of HEIF/HEIC codec support, we introduced HeifDecoder

HEIF has the ability to store EXIF metadata similar to jpeg and other formats.

However, our ExifParser class will not work on HEIF files. The HeifDecoder has a HeifDocument class which allows for parsing of metadata

Example

Here is example code to parse EXIF data as ExifCollection from a HEIF/HEIC file

/// usage:
/// string inFile = @"C:\FUllPath\To\your.heic";
//// ExifCollection = ParseExifFromHeif(inFile);
private static ExifCollection ParseExifFromHeif(string inHeifFile)
{
    ExifCollection exifMetadata = null;

    HeifDecoder heifDecoder = new HeifDecoder();

    using (FileStream inStream = new FileStream(inHeifFile, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        if (!heifDecoder.IsValidFormat(inStream))
        {
            // either throw an exception
            throw new Exception("Input file not a valid HEIF/HEIC file");
            // or return nothing
            // your call
            //return null;
        }
        else
        {
            try
            {
                inStream.Seek(0, SeekOrigin.Begin); // must rewind

                HeifDocument heifDocument = heifDecoder.GetHeifDocument(inStream, null);
                foreach (HeifImage heifImage in heifDocument.HeifImages)
                {
                    foreach (HeifMetadata hmd in heifImage.Metadata)
                    {
                        if (hmd.Type == "Exif")
                        {
                            exifMetadata = hmd.AsExifCollection();
                            return exifMetadata;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // you could log here or return null or throw
                throw ex;
            }
        }
    }
    return exifMetadata;
}

Details
Last Modified: 2 Months Ago
Last Modified By: Tananda
Type: HOWTO
Article not rated yet.
Article has been viewed 82 times.
Options
Also In This Category