public async Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token) { var resolved = await _resolver.Resolve(identifier, parameters, token); if (resolved.Stream == null && resolved.Decoded == null) { throw new ArgumentNullException($"{nameof(resolved.Stream)} and {nameof(resolved.Decoded)}"); } if (resolved.Stream != null && !resolved.Stream.CanSeek) { using (resolved.Stream) { var memoryStream = new MemoryStream(); await resolved.Stream.CopyToAsync(memoryStream); memoryStream.Position = 0; resolved = new DataResolverResult(memoryStream, resolved.LoadingResult, resolved.ImageInformation); } } if (resolved.Stream != null) { if (resolved.Stream.Length == 0) { throw new InvalidDataException("Zero length stream"); } if (resolved.Stream.Length < 32) { throw new InvalidDataException("Invalid stream"); } if (resolved.ImageInformation.Type == ImageInformation.ImageType.Unknown) { //READ HEADER const int headerLength = 4; byte[] header = new byte[headerLength]; int offset = 0; while (offset < headerLength) { int read = await resolved.Stream.ReadAsync(header, offset, headerLength - offset); offset += read; } resolved.Stream.Position = 0; resolved.ImageInformation.SetType(FileHeader.GetImageType(header)); } if (resolved.ImageInformation.Type == ImageInformation.ImageType.JPEG) { var exif = ExifHelper.Read(resolved.Stream); resolved.Stream.Position = 0; resolved.ImageInformation.SetExif(exif); } } return(resolved); }
public void GetExifMetaDataTest() { ExifHelper _exifHelper = new ExifHelper(); string _path = string.Format(@"{0}\TestSource\WP_20150912_18_11_56_Pro.jpg", AppDomain.CurrentDomain.BaseDirectory); ExifMetadata _mateData = _exifHelper.GetMetaData(_path); Assert.AreEqual("2015:09:12 18:11:56\0", _mateData.DatePictureTaken.DisplayValue); }
const string Unknown = "Un";//未知型号相机 /// <summary> /// 创建照片信息模型 /// </summary> /// <param name="file"></param> /// <returns></returns> public static Pictures CreateModel(string file) { try { DateTime createTime = File.GetCreationTime(file); DateTime writeTime = File.GetLastWriteTime(file); FileCodeTool codetool = new FileCodeTool(); string md5 = codetool.GetMD5(file); string sha1 = codetool.GetSHA1(file); Pictures picture = new Pictures() { Id = GuidTool.Short(), ExtName = Path.GetExtension(file).ToUpper(), MD5 = md5, SHA1 = sha1, OrigTime = createTime < writeTime ? createTime : writeTime, }; using (ExifHelper ex = new ExifHelper(file)) { string maker = ex.GetPropertyString((int)ExifTagNames.EquipMake).Trim().Replace(" ", "_"); string model = ex.GetPropertyString((int)ExifTagNames.EquipModel).Trim().Replace(" ", "_"); picture.Model = (Str.Ok(maker) ? maker : Unknown) + "@" + (Str.Ok(model) ? model : Unknown); char GpsLongitudeRef = ex.GetPropertyChar((int)ExifTagNames.GpsLongitudeRef); char GpsLatitudeRef = ex.GetPropertyChar((int)ExifTagNames.GpsLatitudeRef); picture.GpsLongitude = ex.GetPropertyDouble((int)ExifTagNames.GpsLongitude) * (GpsLongitudeRef.Equals('E') ? 1 : -1); picture.GpsLatitude = ex.GetPropertyDouble((int)ExifTagNames.GpsLatitude) * (GpsLatitudeRef.Equals('N') ? 1 : -1); string[] exifDTOrig = ex.GetPropertyString((int)ExifTagNames.ExifDTOrig).Trim().Split(' '); if (exifDTOrig != null && exifDTOrig.Count() == 2) { if (DateTime.TryParse(string.Format("{0} {1}", exifDTOrig[0].Replace(':', '-'), exifDTOrig[1]), out DateTime dt)) { picture.OrigTime = dt; } } picture.Name = string.Format("{0}-{1}{2}", picture.OrigTime.ToString("yyyyMMddhhmmss"), picture.MD5, picture.ExtName); } return(picture); } catch { return(null); } }
public async Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token) { var resolved = await _resolver.Resolve(identifier, parameters, token).ConfigureAwait(false); if (resolved.Stream == null && resolved.Decoded == null) { throw new ArgumentNullException($"{nameof(resolved.Stream)} and {nameof(resolved.Decoded)}"); } resolved.Stream = await resolved.Stream.AsSeekableStreamAsync().ConfigureAwait(false); if (resolved.Stream != null) { if (resolved.Stream.Length == 0) { throw new InvalidDataException("Zero length stream"); } if (resolved.Stream.Length < 32) { throw new InvalidDataException("Invalid stream"); } if (resolved.ImageInformation.Type == ImageInformation.ImageType.Unknown) { //READ HEADER const int headerLength = 4; var header = new byte[headerLength]; var offset = 0; while (offset < headerLength) { offset += await resolved.Stream.ReadAsync(header, offset, headerLength - offset).ConfigureAwait(false); } resolved.Stream.Position = 0; resolved.ImageInformation.SetType(FileHeader.GetImageType(header)); } if (resolved.ImageInformation.Type == ImageInformation.ImageType.JPEG) { var exif = ExifHelper.Read(resolved.Stream); resolved.Stream.Position = 0; resolved.ImageInformation.SetExif(exif); } } return(resolved); }
public ImageProperties(BitmapSource image, Stream fileStream = null) { PixelWidth = image.PixelWidth; PixelHeight = image.PixelHeight; DpiX = image.DpiX; DpiY = image.DpiY; if (fileStream == null) { return; } try { ExifInfos = ExifHelper.GetExifInfos(fileStream); } catch { ExifInfos = new Dictionary <string, string>(); } }