public void TestUserCommentDescription_BlankAscii() { var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0 "); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal(string.Empty, descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUserCommentDescription_AsciiHeaderAsciiEncoding() { var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0This is a comment"); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("This is a comment", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUnicodeComment_Ascii() { var commentBytes = new byte[] { 65, 83, 67, 73, 73, 0, 0, 0, 73, 32, 97, 109, 32, 97, 32, 99, 111, 109, 109, 101, 110, 116, 46, 32, 89, 101, 121, 46, 0 }; var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("I am a comment. Yey.", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUserCommentDescription_ZeroLengthAscii1() { // the 10-byte encoding region is only partially full var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0"); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("ASCII", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUserCommentDescription_BlankAscii() { var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0 "); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal(string.Empty, descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUserCommentDescription_AsciiHeaderAsciiEncoding() { var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0This is a comment"); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("This is a comment", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUnicodeComment_Ascii() { var commentBytes = new byte[] { 65, 83, 67, 73, 73, 0, 0, 0, 73, 32, 97, 109, 32, 97, 32, 99, 111, 109, 109, 101, 110, 116, 46, 32, 89, 101, 121, 46, 0 }; var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("I am a comment. Yey.", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUserCommentDescription_ZeroLengthAscii2() { // fill the 10-byte encoding region var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0\x0\x0"); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal(string.Empty, descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUserCommentDescription_ZeroLengthAscii1() { // the 10-byte encoding region is only partially full var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0"); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("ASCII", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUnicodeComment_ActualBytes() { var commentBytes = new byte[] { 85, 78, 73, 67, 79, 68, 69, 0, 84, 0, 104, 0, 105, 0, 115, 0, 32, 0, 109, 0, 97, 0, 114, 0, 109, 0, 111, 0, 116, 0, 32, 0, 105, 0, 115, 0, 32, 0, 103, 0, 101, 0, 116, 0, 116, 0, 105, 0, 110, 0, 103, 0, 32 , 0, 99, 0, 108, 0, 111, 0, 115, 0, 101, 0, 46, 0, 46, 0, 46, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32 , 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0 }; var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("This marmot is getting close...", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
public void TestUnicodeComment_ActualBytes() { var commentBytes = new byte[] { 85, 78, 73, 67, 79, 68, 69, 0, 84, 0, 104, 0, 105, 0, 115, 0, 32, 0, 109, 0, 97, 0, 114, 0, 109, 0, 111, 0, 116, 0, 32, 0, 105, 0, 115, 0, 32, 0, 103, 0, 101, 0, 116, 0, 116, 0, 105, 0, 110, 0, 103, 0, 32 , 0, 99, 0, 108, 0, 111, 0, 115, 0, 101, 0, 46, 0, 46, 0, 46, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32 , 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0 }; var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal("This marmot is getting close...", descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }
static string?GetExposureProgramDescription(IEnumerable <Directory> directories) { // obtain a specific directory var directory = directories.OfType <ExifSubIfdDirectory>().FirstOrDefault(); if (directory == null) { return(null); } // create a descriptor var descriptor = new ExifSubIfdDescriptor(directory); // get tag description return(descriptor.GetExposureProgramDescription()); }
public PhotoMetadata GetMetadata(IFormFile file) { if (file == null) { throw new ArgumentException("Input file cannot be null!"); } var metadata = new PhotoMetadata(); using (var ms = new MemoryStream()) { file.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var result = ImageMetadataReader.ReadMetadata(ms); // obtain the Exif directories var exifIdDirectory = result.OfType <ExifIfd0Directory>().FirstOrDefault(); var exifSubIfDirectory = result.OfType <ExifSubIfdDirectory>().FirstOrDefault(); if (exifIdDirectory != null) { var camera = exifIdDirectory.GetDescription(ExifIfd0Directory.TagMake) ?? "Unknown"; var model = exifIdDirectory.GetDescription(ExifIfd0Directory.TagModel) ?? "Unknown"; metadata.Camera = $"{camera} - {model}"; var takenDate = exifIdDirectory.GetDescription(ExifIfd0Directory.TagDateTime); DateTime dateTime; if (DateTime.TryParse(takenDate, out dateTime)) { metadata.DateTaken = dateTime; } } if (exifSubIfDirectory != null) { var subIfdDescriptor = new ExifSubIfdDescriptor(exifSubIfDirectory); metadata.ExposureTime = subIfdDescriptor.GetExposureTimeDescription(); metadata.Aperture = subIfdDescriptor.GetApertureValueDescription(); metadata.Flash = subIfdDescriptor.GetFlashDescription(); metadata.Iso = subIfdDescriptor.GetIsoEquivalentDescription(); } } return(metadata); }
private static void Main() { const string filePath = "../MetadataExtractor.Tests/Data/withIptcExifGps.jpg"; Console.WriteLine($"Processing file: {filePath}"); // There are multiple ways to get a Metadata object for a file // // SCENARIO 1: UNKNOWN FILE TYPE // // This is the most generic approach. It will transparently determine the file type and invoke the appropriate // readers. In most cases, this is the most appropriate usage. This will handle JPEG, TIFF, GIF, BMP and RAW // (CRW/CR2/NEF/RW2/ORF) files and extract whatever metadata is available and understood. // try { var directories = ImageMetadataReader.ReadMetadata(filePath); Print(directories, "Using ImageMetadataReader"); } catch (ImageProcessingException e) { PrintError(e); } catch (IOException e) { PrintError(e); } // // SCENARIO 2: SPECIFIC FILE TYPE // // If you know the file to be a JPEG, you may invoke the JpegMetadataReader, rather than the generic reader // used in approach 1. Similarly, if you knew the file to be a TIFF/RAW image you might use TiffMetadataReader, // PngMetadataReader for PNG files, BmpMetadataReader for BMP files, or GifMetadataReader for GIF files. // // Using the specific reader offers a very, very slight performance improvement. // try { var directories = JpegMetadataReader.ReadMetadata(filePath); Print(directories, "Using JpegMetadataReader"); } catch (JpegProcessingException e) { PrintError(e); } catch (IOException e) { PrintError(e); } // // APPROACH 3: SPECIFIC METADATA TYPE // // If you only wish to read a subset of the supported metadata types, you can do this by // passing the set of readers to use. // // This currently only applies to JPEG file processing. // try { // Handle only Exif and IPTC from JPEG var readers = new IJpegSegmentMetadataReader[] { new ExifReader(), new IptcReader() }; var directories = JpegMetadataReader.ReadMetadata(filePath, readers); Print(directories, "Using JpegMetadataReader for Exif and IPTC only"); } catch (JpegProcessingException e) { PrintError(e); } catch (IOException e) { PrintError(e); } // Write all extracted values to stdout void Print(IEnumerable <Directory> directories, string method) { Console.WriteLine(); Console.WriteLine("-------------------------------------------------"); Console.Write(' '); Console.WriteLine(method); Console.WriteLine("-------------------------------------------------"); Console.WriteLine(); // Extraction gives us potentially many directories foreach (var directory in directories) { // Each directory stores values in tags foreach (var tag in directory.Tags) { Console.WriteLine(tag); } // Each directory may also contain error messages foreach (var error in directory.Errors) { Console.Error.WriteLine("ERROR: " + error); } } } DateTime?GetTakenDateTime(IEnumerable <Directory> directories) { // obtain the Exif SubIFD directory var directory = directories.OfType <ExifSubIfdDirectory>().FirstOrDefault(); if (directory == null) { return(null); } // query the tag's value if (directory.TryGetDateTime(ExifDirectoryBase.TagDateTimeOriginal, out var dateTime)) { return(dateTime); } return(null); } string GetExposureProgramDescription(IEnumerable <Directory> directories) { // obtain a specific directory var directory = directories.OfType <ExifSubIfdDirectory>().FirstOrDefault(); if (directory == null) { return(null); } // create a descriptor var descriptor = new ExifSubIfdDescriptor(directory); // get tag description return(descriptor.GetExposureProgramDescription()); } void PrintError(Exception exception) => Console.Error.WriteLine($"EXCEPTION: {exception}"); }
public void TestUserCommentDescription_ZeroLengthAscii2() { // fill the 10-byte encoding region var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0\x0\x0"); var directory = new ExifSubIfdDirectory(); directory.Set(ExifDirectoryBase.TagUserComment, commentBytes); var descriptor = new ExifSubIfdDescriptor(directory); Assert.Equal(string.Empty, descriptor.GetDescription(ExifDirectoryBase.TagUserComment)); }