/// <summary>
 /// Set exif information.
 /// </summary>
 /// <param name="filePath">File path</param>
 public void SetExif(string filePath)
 {
     Application.Current.Dispatcher.Invoke(() =>
     {
         ExifDataList.Clear();
         ExifDataList.AddRange(ExifParser.GetExifDataFromFile(filePath));
     });
 }
Esempio n. 2
0
        public static Position TryExtractPositionFromFile(string file)
        {
            Console.WriteLine(file);

            IReadOnlyList <Directory> directories;

            try
            {
                directories = ImageMetadataReader.ReadMetadata(file);
            }
            catch (ImageProcessingException)
            {
                Console.WriteLine("Unable to process {0}", file);
                return(null);
            }
            if (!directories.Any())
            {
                return(null);
            }

            var latitude            = directories.SelectMany(x => x.Tags).FirstOrDefault(t => t.Name == "GPS Latitude");
            var longitude           = directories.SelectMany(x => x.Tags).FirstOrDefault(t => t.Name == "GPS Longitude");
            var date                = directories.SelectMany(x => x.Tags).FirstOrDefault(t => t.Name == "GPS Date Stamp");
            var time                = directories.SelectMany(x => x.Tags).FirstOrDefault(t => t.Name == "GPS Time-Stamp");
            var dilutionOfPrecision = directories.SelectMany(x => x.Tags).Where(t => t.Name.StartsWith("GPS DOP")).FirstOrDefault();

            // sometimes DOP is represented as rational value (numerator, denominator), sometimes as double.

            if (latitude != null && longitude != null && date != null && time != null)
            {
                var dateTime = $"{date.Description} {time.Description}";
                Console.WriteLine(dateTime);
                Console.WriteLine(gpsFormat);
                var dateTimeUtc = DateTimeOffset.ParseExact(dateTime, gpsFormat, null, System.Globalization.DateTimeStyles.AssumeUniversal);

                Console.WriteLine("[{0}]: {1}, {2}", dateTimeUtc, latitude.Description, longitude.Description);
                return(new Position(dateTimeUtc,
                                    LatLongParser.ParseString(latitude.Description),
                                    LatLongParser.ParseString(longitude.Description),
                                    dilutionOfPrecision == null ? 0.0 : ExifParser.ParseRationalOrDouble(dilutionOfPrecision.Description)));
            }

            if (latitude != null && longitude != null)
            {
                var filesystemTime = directories.SelectMany(x => x.Tags).FirstOrDefault(t => t.Name == "File Modified Date");
                if (filesystemTime != null)
                {
                    var dateTime = DateTimeOffset.ParseExact(filesystemTime.Description, fileModifiedDateFormat, CultureInfo.InvariantCulture);
                    Console.WriteLine("[{0}]: {1}, {2}", dateTime, latitude.Description, longitude.Description);
                    return(new Position(dateTime, LatLongParser.ParseString(latitude.Description), LatLongParser.ParseString(longitude.Description), PositionUnit.WGS84));
                }
            }


            return(null);
        }
Esempio n. 3
0
        public async Task <IEnumerable <Tag> > GetTagsAsync(string srcPath)
        {
            VerifySourceFile(srcPath);

            var reader   = new ExifReader(_opts);
            var parser   = new ExifParser();
            var exifJson = await reader.ReadExifAsync(srcPath).ConfigureAwait(false);

            return(parser.ParseTags(exifJson));
        }
Esempio n. 4
0
        public async Task <IEnumerable <Tag> > GetTagsAsync(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var reader   = new ExifReader(_opts);
            var parser   = new ExifParser();
            var exifJson = await reader.ReadExifAsync(stream).ConfigureAwait(false);

            return(parser.ParseTags(exifJson));
        }
Esempio n. 5
0
        private static Surface GetOrientedSurface(byte[] bytes, out ExifValueCollection exifMetadata)
        {
            exifMetadata = null;

            Surface surface = WebPFile.Load(bytes);

            byte[] exifBytes = WebPFile.GetExifBytes(bytes);
            if (exifBytes != null)
            {
                exifMetadata = ExifParser.Parse(exifBytes);

                if (exifMetadata != null)
                {
                    ExifValue orientationProperty = exifMetadata.GetAndRemoveValue(ExifPropertyKeys.Image.Orientation.Path);
                    if (orientationProperty != null)
                    {
                        MetadataHelpers.ApplyOrientationTransform(orientationProperty, ref surface);
                    }
                }
            }

            return(surface);
        }
Esempio n. 6
0
        private static Document GetOrientedDocument(byte[] bytes, out ExifValueCollection exifMetadata)
        {
            exifMetadata = null;

            Document doc = null;

            // Load the image into a Bitmap so the EXIF orientation transform can be applied.

            using (Bitmap image = WebPFile.Load(bytes))
            {
                byte[] exifBytes = WebPFile.GetExifBytes(bytes);
                if (exifBytes != null)
                {
                    exifMetadata = ExifParser.Parse(exifBytes);

                    if (exifMetadata.Count > 0)
                    {
                        MetadataEntry orientationProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.Orientation);
                        if (orientationProperty != null)
                        {
                            RotateFlipType transform = MetadataHelpers.GetOrientationTransform(orientationProperty);
                            if (transform != RotateFlipType.RotateNoneFlipNone)
                            {
                                image.RotateFlip(transform);
                            }
                        }

                        MetadataEntry xResProperty    = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.XResolution);
                        MetadataEntry yResProperty    = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.YResolution);
                        MetadataEntry resUnitProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.ResolutionUnit);
                        if (xResProperty != null && yResProperty != null && resUnitProperty != null)
                        {
                            if (MetadataHelpers.TryDecodeRational(xResProperty, out double xRes) &&
                                MetadataHelpers.TryDecodeRational(yResProperty, out double yRes) &&
                                MetadataHelpers.TryDecodeShort(resUnitProperty, out ushort resUnit))
                            {
                                if (xRes > 0.0 && yRes > 0.0)
                                {
                                    double dpiX, dpiY;

                                    switch ((MeasurementUnit)resUnit)
                                    {
                                    case MeasurementUnit.Centimeter:
                                        dpiX = Document.DotsPerCmToDotsPerInch(xRes);
                                        dpiY = Document.DotsPerCmToDotsPerInch(yRes);
                                        break;

                                    case MeasurementUnit.Inch:
                                        dpiX = xRes;
                                        dpiY = yRes;
                                        break;

                                    default:
                                        // Unknown ResolutionUnit value.
                                        dpiX = 0.0;
                                        dpiY = 0.0;
                                        break;
                                    }

                                    if (dpiX > 0.0 && dpiY > 0.0)
                                    {
                                        try
                                        {
                                            image.SetResolution((float)dpiX, (float)dpiY);
                                        }
                                        catch
                                        {
                                            // Ignore any errors when setting the resolution.
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                doc = Document.FromGdipImage(image);
            }

            return(doc);
        }
Esempio n. 7
0
        private static void AddAvifMetadataToDocument(Document doc, AvifReader reader, IByteArrayPool arrayPool)
        {
            byte[] exifBytes = reader.GetExifData();

            if (exifBytes != null)
            {
                ExifValueCollection exifValues = ExifParser.Parse(exifBytes, arrayPool);

                if (exifValues != null)
                {
                    exifValues.Remove(MetadataKeys.Image.InterColorProfile);
                    // The HEIF specification states that the EXIF orientation tag is only
                    // informational and should not be used to rotate the image.
                    // See https://github.com/strukturag/libheif/issues/227#issuecomment-642165942
                    exifValues.Remove(MetadataKeys.Image.Orientation);

                    foreach (MetadataEntry entry in exifValues)
                    {
                        doc.Metadata.AddExifPropertyItem(entry.CreateExifPropertyItem());
                    }
                }
            }

            CICPColorData?imageColorData = reader.ImageColorData;

            if (imageColorData.HasValue)
            {
                string serializedValue = CICPSerializer.TrySerialize(imageColorData.Value);

                if (serializedValue != null)
                {
                    doc.Metadata.SetUserValue(CICPMetadataName, serializedValue);
                }
            }

            ImageGridMetadata imageGridMetadata = reader.ImageGridMetadata;

            if (imageGridMetadata != null)
            {
                string serializedValue = imageGridMetadata.SerializeToString();

                if (serializedValue != null)
                {
                    doc.Metadata.SetUserValue(ImageGridName, serializedValue);
                }
            }

            byte[] iccProfileBytes = reader.GetICCProfile();

            if (iccProfileBytes != null)
            {
                doc.Metadata.AddExifPropertyItem(ExifSection.Image,
                                                 unchecked ((ushort)ExifTagID.IccProfileData),
                                                 new ExifValue(ExifValueType.Undefined,
                                                               iccProfileBytes));
            }

            byte[] xmpBytes = reader.GetXmpData();

            if (xmpBytes != null)
            {
                XmpPacket xmpPacket = XmpPacket.TryParse(xmpBytes);
                if (xmpPacket != null)
                {
                    doc.Metadata.SetXmpPacket(xmpPacket);
                }
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Exif情報を設定する
 /// </summary>
 /// <param name="filePath">画像ファイルパス</param>
 public void SetExif(string filePath)
 {
     ExifParser.SetExifDataFromFile(filePath, ExifDataList);
 }
Esempio n. 9
0
 /// <summary>
 /// コンストラクタ
 /// </summary>
 public ExifInfoViewModel()
 {
     // Exif情報の基礎部分を作っておく。
     // 各Exif情報の値は、画像読み込み時に設定する
     ExifParser.CreateExifDefaultList(ExifDataList);
 }
 protected ExifImageInfoMediaTransformerBase()
 {
     _exifParser = new ExifParser();
 }
Esempio n. 11
0
 public void TestParseDouble()
 {
     Assertions.AreApproximatelyEqual(3.946, ExifParser.ParseRationalOrDouble("3.946"));
 }