Пример #1
0
        public virtual void TestGetThumbnailData()
        {
            ExifThumbnailDirectory directory = ExifReaderTest.ProcessBytes <ExifThumbnailDirectory>("Tests/Data/withExif.jpg.app1");

            sbyte[] thumbData = directory.GetThumbnailData();
            NUnit.Framework.Assert.IsNotNull(thumbData);
            try
            {
                // attempt to read the thumbnail -- it should be a legal Jpeg file
                JpegSegmentReader.ReadSegments(new SequentialByteArrayReader(thumbData), null);
            }
            catch (JpegProcessingException)
            {
                NUnit.Framework.Assert.Fail("Unable to construct JpegSegmentReader from thumbnail data");
            }
        }
Пример #2
0
        public static IList <Directory> Read(Stream stream)
        {
            if (stream.Position != 0)
            {
                stream.Position = 0;
            }


            var segmentTypes = _exifReader.SegmentTypes;
            var segments     = JpegSegmentReader.ReadSegments(new SequentialStreamReader(stream), segmentTypes);

            var directories = new List <Directory>();

            var readerSegmentTypes = _exifReader.SegmentTypes;
            var readerSegments     = segments.Where(s => readerSegmentTypes.Contains(s.Type));

            directories.AddRange(_exifReader.ReadJpegSegments(readerSegments));

            return(directories);
        }
Пример #3
0
        public static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                PrintUsage();
                Environment.Exit(1);
            }
            var filePath = args[0];

            if (!File.Exists(filePath))
            {
                Console.Error.WriteLine("File does not exist");
                PrintUsage();
                Environment.Exit(1);
            }
            ICollection <JpegSegmentType> segmentTypes = new HashSet <JpegSegmentType>();

            for (var i = 1; i < args.Length; i++)
            {
                var segmentType = (JpegSegmentType)Enum.Parse(typeof(JpegSegmentType), args[i], ignoreCase: true);
                if (!segmentType.CanContainMetadata())
                {
                    Console.Error.WriteLine("WARNING: Segment type {0} cannot contain metadata so it may not be necessary to extract it", segmentType);
                }
                segmentTypes.Add(segmentType);
            }
            if (segmentTypes.Count == 0)
            {
                // If none specified, use all that could reasonably contain metadata
                foreach (var segmentType in JpegSegmentTypeExtensions.CanContainMetadataTypes)
                {
                    segmentTypes.Add(segmentType);
                }
            }
            Console.Out.WriteLine("Reading: {0}", filePath);
            using (var stream = File.OpenRead(filePath))
            {
                var segmentData = JpegSegmentReader.ReadSegments(new SequentialStreamReader(stream), segmentTypes);
                SaveSegmentFiles(filePath, segmentData);
            }
        }
        public void TestReadAllSegments()
        {
            var segmentData = JpegSegmentReader.ReadSegments("Tests/Data/withExifAndIptc.jpg", null);

            Assert.Equal(1, segmentData.GetSegmentCount(JpegSegmentType.App0));
            Assert.Equal(File.ReadAllBytes("Tests/Data/withExifAndIptc.jpg.app0"), segmentData.GetSegment(JpegSegmentType.App0));
            Assert.Null(segmentData.GetSegment(JpegSegmentType.App0, 1));
            Assert.Equal(2, segmentData.GetSegmentCount(JpegSegmentType.App1));
            Assert.Equal(File.ReadAllBytes("Tests/Data/withExifAndIptc.jpg.app1.0"), segmentData.GetSegment(JpegSegmentType.App1));
            Assert.Equal(File.ReadAllBytes("Tests/Data/withExifAndIptc.jpg.app1.1"), segmentData.GetSegment(JpegSegmentType.App1, occurrence: 1));
            Assert.Null(segmentData.GetSegment(JpegSegmentType.App1, 2));
            Assert.Equal(1, segmentData.GetSegmentCount(JpegSegmentType.App2));
            Assert.Equal(File.ReadAllBytes("Tests/Data/withExifAndIptc.jpg.app2"), segmentData.GetSegment(JpegSegmentType.App2));
            Assert.Null(segmentData.GetSegment(JpegSegmentType.App2, 1));
            Assert.Equal(1, segmentData.GetSegmentCount(JpegSegmentType.AppD));
            Assert.Equal(File.ReadAllBytes("Tests/Data/withExifAndIptc.jpg.appd"), segmentData.GetSegment(JpegSegmentType.AppD));
            Assert.Null(segmentData.GetSegment(JpegSegmentType.AppD, 1));
            Assert.Equal(1, segmentData.GetSegmentCount(JpegSegmentType.AppE));
            Assert.Equal(File.ReadAllBytes("Tests/Data/withExifAndIptc.jpg.appe"), segmentData.GetSegment(JpegSegmentType.AppE));
            Assert.Null(segmentData.GetSegment(JpegSegmentType.AppE, 1));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.App3));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.App4));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.App5));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.App6));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.App7));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.App8));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.App9));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.AppA));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.AppB));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.AppC));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.AppF));
            Assert.Equal(0, segmentData.GetSegmentCount(JpegSegmentType.Com));
            Assert.Equal(4, segmentData.GetSegmentCount(JpegSegmentType.Dht));
            Assert.Equal(2, segmentData.GetSegmentCount(JpegSegmentType.Dqt));
            Assert.Equal(1, segmentData.GetSegmentCount(JpegSegmentType.Sof0));
            Assert.Null(segmentData.GetSegment(JpegSegmentType.App3));
        }
Пример #5
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Com.Drew.Imaging.Jpeg.JpegProcessingException"/>
        public static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                PrintUsage();
                System.Environment.Exit(1);
            }
            string filePath = args[0];

            if (!new FilePath(filePath).Exists())
            {
                System.Console.Error.Println("File does not exist");
                PrintUsage();
                System.Environment.Exit(1);
            }
            ICollection <JpegSegmentType> segmentTypes = new HashSet <JpegSegmentType>();

            for (int i = 1; i < args.Length; i++)
            {
                JpegSegmentType segmentType = JpegSegmentType.ValueOf(args[i].ToUpper());
                if (!segmentType.canContainMetadata)
                {
                    System.Console.Error.Printf("WARNING: Segment type %s cannot contain metadata so it may not be necessary to extract it%n", segmentType);
                }
                segmentTypes.Add(segmentType);
            }
            if (segmentTypes.Count == 0)
            {
                // If none specified, use all that could reasonably contain metadata
                Sharpen.Collections.AddAll(segmentTypes, JpegSegmentType.canContainMetadataTypes);
            }
            System.Console.Out.Println("Reading: " + filePath);
            JpegSegmentData segmentData = JpegSegmentReader.ReadSegments(new FilePath(filePath), segmentTypes.AsIterable());

            SaveSegmentFiles(filePath, segmentData);
        }
Пример #6
0
 private static IReadOnlyList <JpegSegment> ReadSegments(string fileName, ICollection <JpegSegmentType>?segmentTypes = null)
 {
     using var stream = TestDataUtil.OpenRead(fileName);
     return(JpegSegmentReader.ReadSegments(new SequentialStreamReader(stream), segmentTypes).ToList());
 }
 private static IReadOnlyList <JpegSegment> ReadSegments(string fileName, ICollection <JpegSegmentType> segmentTypes = null)
 {
     using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
         return(JpegSegmentReader.ReadSegments(new SequentialStreamReader(stream), segmentTypes).ToList());
 }
Пример #8
0
        public override FileMetadata AnalyzeFile()
        {
            try
            {
                this.foundMetadata = new FileMetadata();
                IReadOnlyList <MetadataExtractor.Directory> directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(this.fileStream);

                foreach (MetadataExtractor.Directory currentDir in directories.Where(p => !IgnoredExifDirectories.Contains(p.Name)))
                {
                    if (currentDir is MetadataExtractor.Formats.Exif.ExifThumbnailDirectory)
                    {
                        try
                        {
                            uint offset = (uint)currentDir.GetObject(MetadataExtractor.Formats.Exif.ExifThumbnailDirectory.TagThumbnailOffset) + (uint)MetadataExtractor.Formats.Exif.ExifReader.JpegSegmentPreamble.Length;
                            uint length = (uint)currentDir.GetObject(MetadataExtractor.Formats.Exif.ExifThumbnailDirectory.TagThumbnailLength);

                            long currentPosition = this.fileStream.Position;
                            this.fileStream.Seek(0, SeekOrigin.Begin);
                            JpegSegment app1Segment = JpegSegmentReader.ReadSegments(new MetadataExtractor.IO.SequentialStreamReader(this.fileStream), new[] { JpegSegmentType.App1 }).FirstOrDefault();
                            if (app1Segment != null)
                            {
                                byte[] thumb = new byte[length];
                                Array.Copy(app1Segment.Bytes, offset, thumb, 0, length);
                                this.foundMetadata.Thumbnail = thumb;
                            }
                            this.fileStream.Seek(currentPosition, SeekOrigin.Begin);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else if (currentDir is MetadataExtractor.Formats.Exif.GpsDirectory gps)
                    {
                        if (this.foundMetadata.GPS == null)
                        {
                            MetadataExtractor.GeoLocation gpsLocation = gps.GetGeoLocation();
                            if (gpsLocation != null)
                            {
                                this.foundMetadata.GPS = new GeoLocation(gpsLocation.ToDmsString(), gpsLocation.Longitude, gpsLocation.Latitude);
                                if (gps.ContainsTag(MetadataExtractor.Formats.Exif.GpsDirectory.TagAltitude))
                                {
                                    this.foundMetadata.GPS.Altitude = $"{gps.GetDescription(MetadataExtractor.Formats.Exif.GpsDirectory.TagAltitude)} ({gps.GetDescription(MetadataExtractor.Formats.Exif.GpsDirectory.TagAltitudeRef)})";;
                                }
                            }
                        }
                    }
                    else
                    {
                        Dictionary <string, string> dicTags = new Dictionary <string, string>();
                        foreach (MetadataExtractor.Tag tag in currentDir.Tags)
                        {
                            string lcDescription = tag.Description?.Trim();
                            string lcName        = tag.Name?.Trim();

                            if (!String.IsNullOrWhiteSpace(lcName) && !String.IsNullOrWhiteSpace(lcDescription) &&
                                !lcName.StartsWith("unknown", StringComparison.OrdinalIgnoreCase) && !lcDescription.ToLower().StartsWith("unknown", StringComparison.OrdinalIgnoreCase))
                            {
                                lcName        = Functions.RemoveAccentsWithNormalization(lcName);
                                lcDescription = Functions.RemoveAccentsWithNormalization(lcDescription);

                                switch (tag.Type)
                                {
                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagWinAuthor:
                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagArtist:
                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagCameraOwnerName:
                                    this.foundMetadata.Add(new User(lcDescription, false, "EXIF"));
                                    break;

                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagSoftware:
                                    string strSoftware = Analysis.ApplicationAnalysis.GetApplicationsFromString(lcDescription);
                                    if (!String.IsNullOrEmpty(strSoftware))
                                    {
                                        this.foundMetadata.Add(new Application(strSoftware));
                                    }
                                    break;

                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTime:
                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeDigitized:
                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeOriginal:
                                    if (DateTime.TryParseExact(lcDescription, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date) &&
                                        date > DateTime.MinValue && (!foundMetadata.Dates.CreationDate.HasValue || this.foundMetadata.Dates.CreationDate > date))
                                    {
                                        this.foundMetadata.Dates.CreationDate = date;
                                    }
                                    break;

                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagModel:
                                    this.foundMetadata.Model = lcDescription;
                                    break;

                                case MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagHostComputer:
                                    this.foundMetadata.OperatingSystem = lcDescription;
                                    break;

                                default:
                                    break;
                                }

                                if (!dicTags.ContainsKey(lcName))
                                {
                                    dicTags.Add(lcName, lcDescription);
                                }
                            }
                        }

                        if (dicTags.Count > 0)
                        {
                            string makerKey = currentDir.Name;
                            int    i        = 1;
                            while (foundMetadata.Makernotes.ContainsKey(makerKey))
                            {
                                makerKey = $"{currentDir.Name} ({i})";
                                i++;
                            }

                            foundMetadata.Makernotes.Add(makerKey, dicTags);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine($"Error analizing EXIF metadata ({e.ToString()})");
            }

            return(this.foundMetadata);
        }
 public void TestWithNonJpegFile()
 {
     Assert.Throws <JpegProcessingException>(() => JpegSegmentReader.ReadSegments("MetadataExtractor.Tests.dll", null));
 }
        public void TestLoadJpegWithoutExifDataReturnsNull()
        {
            var segmentData = JpegSegmentReader.ReadSegments("Tests/Data/noExif.jpg", null);

            Assert.Null(segmentData.GetSegment(JpegSegmentType.App1));
        }