public string SerialNumber() { string return_value = StringValue(Exif_Photo_BodySerialNumber); if (string.IsNullOrEmpty(return_value) == false) { return(return_value); } // See if we can pull the data from a makernote. string make = Make().ToLowerInvariant(); if (make.Contains("nikon") == true && ExifIFD != null) { // Find the maker note... InteroperabilityField maker_note_field = ExifIFD.GetTag(MakerNote); if (maker_note_field != null) { byte[] maker_note_buffer = new byte[maker_note_field.Count - 10]; Array.Copy(TIFFData, maker_note_field.OffsetToValue + 10, maker_note_buffer, 0, maker_note_field.Count - 10); TIFFHeader th = new TIFFHeader(); if (th.FromBytes(maker_note_buffer, 0) == true) { ImageFileDirectory maker_note = new ImageFileDirectory(); if (maker_note.FromBytes(maker_note_buffer, th.OffsetToZerothIFD, th.LittleEndian) == true) { // Now read from the Maker Note Buffer! return_value = StringValue(maker_note, Exif_MakerNote_Nikon3_SerialNumber, maker_note_buffer); if (string.IsNullOrEmpty(return_value) == true) { return_value = StringValue(maker_note, Exif_MakerNote_Nikon3_SerialNO, maker_note_buffer); if (return_value.StartsWith("NO=") == true) { return(return_value.Substring(3, return_value.Length - 3).Trim()); } } } } return(return_value); } } return(string.Empty); }
public double CompassDirection() { if (GPSIFD == null) { return(0.0D); } InteroperabilityField field = GPSIFD.GetTag(Exif_GPS_Image_Direction); if (field == null || field.Type != 5) { return(0.0D); } if (field.Count == 3) { double degrees = 0.0D; double minutes = 0.0D; double seconds = 0.0D; GPSTime(GPSIFD, Exif_GPS_Image_Direction, out degrees, out minutes, out seconds); double return_value = ConvertDegreesMinutesSecondsCoordinateToDecimalDegrees(degrees, minutes, seconds); if (return_value >= 360.0D) { return_value = 0.0D; } return(return_value); } return(DecimalValue(GPSIFD, Exif_GPS_Image_Direction)); }
public int IntegerValue(ImageFileDirectory ifd, int tag) { if (TIFFData == null || ifd == null) { return(0); } var field = ifd.GetTag(tag); if (field == null || field.Count < 1) { return(0); } if (field.Type == 3) { return(field.AsShort()); } if (field.Type == 4) { return(LongValue(ifd, tag)); } return(0); }
public double DecimalValue(ImageFileDirectory ifd, int tag, byte[] buffer) { if (buffer == null || ifd == null) { return(0.0D); } var field = ifd.GetTag(tag); if (field == null || field.Type != 5) { return(0.0D); } int numerator = TIFFHeader.ToInt32(buffer, field.OffsetToValue, Header.LittleEndian); int denominator = TIFFHeader.ToInt32(buffer, field.OffsetToValue + 4, Header.LittleEndian); double return_value = Rational(numerator, denominator); return(return_value); }
public bool GPSTime(ImageFileDirectory ifd, int tag, out double hours, out double minutes, out double seconds) { hours = 0.0D; minutes = 0.0D; seconds = 0.0D; if (TIFFData == null || ifd == null) { return(false); } var field = ifd.GetTag(tag); if (field == null || field.Type != 5) { return(false); } if (field.Count < 3) { return(false); } int[] integer_array = new int[field.Count * 2]; // Rationals are two integers each int array_index = 0; while (array_index < integer_array.Length) { integer_array[array_index] = TIFFHeader.ToInt32(TIFFData, field.OffsetToValue + (array_index * 4), Header.LittleEndian); array_index++; } hours = Rational(integer_array[0], integer_array[1]); minutes = Rational(integer_array[2], integer_array[3]); seconds = Rational(integer_array[4], integer_array[5]); return(true); }
public int LongValue(ImageFileDirectory ifd, int tag) { if (TIFFData == null || ifd == null) { return(0); } var field = ifd.GetTag(tag); if (field == null || field.Type != 4 || field.Count < 1) { return(0); } // Lookout for values less than 4... if (field.Count <= 1) { return(field.OffsetToValue); } return(0); }
public double DegreesValue(ImageFileDirectory ifd, int tag) { if (TIFFData == null || ifd == null) { return(0.0D); } InteroperabilityField field = ifd.GetTag(tag); if (field == null || (field.Type != 5 && field.Type != 10)) { return(0.0D); } if (field.Count < 3) { return(0.0D); } int[] integer_array = new int[field.Count * 2]; // Rationals are two integers each int array_index = 0; while (array_index < integer_array.Length) { integer_array[array_index] = TIFFHeader.ToInt32(TIFFData, field.OffsetToValue + (array_index * 4), Header.LittleEndian); array_index++; } double degrees = Rational(integer_array[0], integer_array[1]); double minutes = Rational(integer_array[2], integer_array[3]); double seconds = Rational(integer_array[4], integer_array[5]); double return_value = ConvertDegreesMinutesSecondsCoordinateToDecimalDegrees(degrees, minutes, seconds); return(return_value); }
/// <summary> /// This parses the given bytes as EXIF information. /// </summary> /// <param name="bytes">The bytes to parse.</param> /// <returns>True on success, false otherwise.</returns> public bool FromBytes(byte[] bytes) { if (bytes == null || bytes.Length < 16) { return(false); } // Find the Exif signature... int array_index = 0; int exif_signature_offset = -1; int tiff_offset = -1; while (array_index < (bytes.Length - 6)) { if (bytes[array_index] == 'E' && bytes[array_index + 1] == 'x' && bytes[array_index + 2] == 'i' && bytes[array_index + 3] == 'f' && bytes[array_index + 4] == 0 && bytes[array_index + 5] == 0) { exif_signature_offset = array_index; array_index = bytes.Length; // Exit the loop } array_index++; } if (exif_signature_offset < 0) { // Let's try to find the TIFF Header array_index = 0; while (array_index < (bytes.Length - 4)) { if (((bytes[array_index] == 'M' && bytes[array_index + 1] == 'M') || (bytes[array_index] == 'I' && bytes[array_index + 1] == 'I')) && bytes[array_index + 2] == 0x00 && bytes[array_index + 3] == 0x2A) { exif_signature_offset = array_index; array_index = bytes.Length; // Exit the loop } array_index++; } if (exif_signature_offset < 0) { // Can't find the TIFF signature either return(false); } tiff_offset = exif_signature_offset; } else { tiff_offset = exif_signature_offset + 6; } if (Header.FromBytes(bytes, tiff_offset) == false) { return(false); } int tiff_data_length = bytes.Length - tiff_offset; if (tiff_data_length > Program.OneMegabyte) { tiff_data_length = Program.OneMegabyte; } TIFFData = new byte[tiff_data_length]; Array.Copy(bytes, tiff_offset, TIFFData, 0, tiff_data_length); IFD0.FromBytes(TIFFData, Header.OffsetToZerothIFD, Header.LittleEndian); var exif_field = IFD0.GetTag(ExifIFDPointer); if (exif_field != null) { ExifIFD = new ImageFileDirectory(); ExifIFD.InitializeExifTagNames(); ExifIFD.FromBytes(TIFFData, exif_field.OffsetToValue, Header.LittleEndian); } var gps_field = IFD0.GetTag(GpsIFDPointer); if (gps_field != null) { GPSIFD = new ImageFileDirectory(); GPSIFD.InitializeGPSTagNames(); GPSIFD.FromBytes(TIFFData, gps_field.OffsetToValue, Header.LittleEndian); } return(true); }
public string StringValue(ImageFileDirectory ifd, int tag, byte[] buffer) { if (buffer == null || ifd == null) { return(string.Empty); } var field = ifd.GetTag(tag); if (field == null || (field.Type != 2 && field.Type != 1) || field.Count < 2) { return(string.Empty); } // Lookout for values less than 4... if (field.Count <= 4) { byte[] bytes = new byte[4]; bytes[0] = (field.Count > 0) ? field.Byte0 : (byte)0; bytes[1] = (field.Count > 1) ? field.Byte1 : (byte)0; bytes[2] = (field.Count > 2) ? field.Byte2 : (byte)0; bytes[3] = (field.Count > 3) ? field.Byte3 : (byte)0; if (field.Count >= 1) { return(System.Text.Encoding.ASCII.GetString(bytes, 0, field.Count).Trim("\0".ToCharArray())); } else { return(string.Empty); } } if (field.OffsetToValue > 0 && field.Count > 1 && buffer.Length > (field.OffsetToValue + field.Count)) { // last check... if (field.Type == 2) { return(System.Text.Encoding.ASCII.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray())); } else if (field.Type == 1) { // Could be ASCII or Unicode if (buffer[field.OffsetToValue] != 0x00 && buffer[field.OffsetToValue + 1] == 0x00) { // Little Endian Unicode return(System.Text.Encoding.Unicode.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray())); } else if (buffer[field.OffsetToValue] == 0x00 && buffer[field.OffsetToValue + 1] != 0x00) { // Big Endian Unicode return(System.Text.Encoding.BigEndianUnicode.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray())); } else if (buffer[field.OffsetToValue] != 0x00 && buffer[field.OffsetToValue + 1] != 0x00) { return(System.Text.Encoding.UTF8.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray())); } } } return(string.Empty); }