public OffsetModel ParseOffsetData(ExifThumbnailDirectory exifThumbnailDir, string subPath)
        {
            if (exifThumbnailDir == null)
            {
                return new OffsetModel {
                           Success = false, Reason = "ExifThumbnailDirectory null"
                }
            }
            ;

            long thumbnailOffset = long.Parse(exifThumbnailDir.GetDescription(
                                                  ExifThumbnailDirectory.TagThumbnailOffset).Split(' ')[0]);
            const int maxIssue35Offset = 12;
            int       thumbnailLength  = int.Parse(exifThumbnailDir.GetDescription(
                                                       ExifThumbnailDirectory.TagThumbnailLength).Split(' ')[0]) + maxIssue35Offset;

            byte[] thumbnail = new byte[thumbnailLength];

            using (var imageStream = _iStorage.ReadStream(subPath))
            {
                imageStream.Seek(thumbnailOffset, SeekOrigin.Begin);

                imageStream.Read(thumbnail, 0, thumbnailLength);
            }

            // work around Metadata Extractor issue #35
            if (thumbnailLength <= maxIssue35Offset + 1)
            {
                _logger.LogInformation($"[ParseOffsetData] thumbnailLength : {thumbnailLength} {maxIssue35Offset + 1}");
                return(new OffsetModel {
                    Success = false, Reason = "offsetLength"
                });
            }

            int issue35Offset = 0;

            for (int offset = 0; offset <= maxIssue35Offset; ++offset)
            {
                // 0xffd8 is the JFIF start of image segment indicator
                if ((thumbnail[offset] == 0xff) && (thumbnail[offset + 1] == 0xd8))
                {
                    issue35Offset = offset;
                    break;
                }
            }
            return(new OffsetModel
            {
                Success = true,
                Index = issue35Offset,
                Count = thumbnailLength - issue35Offset,
                Data = thumbnail,                 // byte array
            });
        }
    }
Beispiel #2
0
        protected ImageSource ReadThumbnailFromExif(String filename, ExifThumbnailDirectory thumbnails)
        {
            if (thumbnails == null)
            {
                return(null);
            }

            //Assert.IsTrue(thumbnails.GetDescription(ExifThumbnailDirectory.TagCompression).StartsWith("JPEG"));
            long      thumbnailOffset  = Int64.Parse(thumbnails.GetDescription(ExifThumbnailDirectory.TagThumbnailOffset).Split(' ')[0]);
            const int maxIssue35Offset = 12;
            int       thumbnailLength  = Int32.Parse(thumbnails.GetDescription(ExifThumbnailDirectory.TagThumbnailLength).Split(' ')[0]) + maxIssue35Offset;

            byte[] thumbnail = new byte[thumbnailLength];
            using (FileStream imageStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                imageStream.Seek(thumbnailOffset, SeekOrigin.Begin);
                imageStream.Read(thumbnail, 0, thumbnailLength);
            }

            // work around Metadata Extractor issue #35
            //Assert.IsTrue(thumbnailLength > maxIssue35Offset + 1);
            int issue35Offset = 0;

            for (int offset = 0; offset <= maxIssue35Offset; ++offset)
            {
                // 0xffd8 is the JFIF start of image segment indicator
                if ((thumbnail[offset] == 0xff) && (thumbnail[offset + 1] == 0xd8))
                {
                    issue35Offset = offset;
                    break;
                }
            }

            using (MemoryStream thumbnailStream = new MemoryStream(thumbnail, issue35Offset, thumbnailLength - issue35Offset, false))
            {
                JpegBitmapDecoder jpegDecoder     = new JpegBitmapDecoder(thumbnailStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                WriteableBitmap   writeableBitmap = new WriteableBitmap(jpegDecoder.Frames[0]);
                writeableBitmap.Freeze();
                return(writeableBitmap);
            }
        }