From 15eabfdabf30973c1e99b015060b7da2190ccdf1 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期六, 06 一月 2024 15:47:26 +0800 Subject: [PATCH] 获取图片的GPS信息 --- JiangSu/cs/Tools.cs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 104 insertions(+), 0 deletions(-) diff --git a/JiangSu/cs/Tools.cs b/JiangSu/cs/Tools.cs index 69e3b2e..c4e9961 100644 --- a/JiangSu/cs/Tools.cs +++ b/JiangSu/cs/Tools.cs @@ -3,8 +3,12 @@ using System.Collections.Generic; using System.Data.Common; using System.Data.SQLite; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; using System.Linq; using System.Reflection; +using System.Security.Cryptography; using System.Web; namespace JiangSu.cs @@ -92,5 +96,105 @@ return list.ToArray(); } + + /// <summary> + /// 瀛楄妭鏍煎紡鍖� + /// </summary> + public static string FormatBytes(long bytes) + { + string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" }; + + int i = 0; + double dblSByte = bytes; + if (bytes > 1024) + for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024) + dblSByte = bytes / 1024.0; + + return String.Format("{0:0.##}{1}", dblSByte, Suffix[i]); + } + + /// <summary> + /// 璁$畻鏂囦欢鐨凪D5鐮� + /// </summary> + public static string CalcFileMD5(string filePath) + { + using (var md5 = new MD5CryptoServiceProvider()) + { + using (var stream = File.OpenRead(filePath)) + { + byte[] hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + } + } + + /// <summary> + /// 鑾峰彇鍥剧墖鐨凣PS淇℃伅 + /// </summary> + public static string GetImgGps(string path) + { + Image img = null; + try + { + img = Image.FromFile(path); + var items = img.PropertyItems.OrderByDescending(x => x.Id); + + List<string> list = new List<string> { "0", "0", "0" }; + foreach (PropertyItem item in items) + { + if (item.Id >= 0x0000 && item.Id <= 0x001e) // 鍙彇Id鑼冨洿涓�0x0000鍒�0x001e + { + switch (item.Id) + { + case 0x0002: // 璁剧疆绾害 + list[1] = GetGpsVal(item.Value); + break; + case 0x0004: // 璁剧疆缁忓害 + list[0] = GetGpsVal(item.Value); + break; + case 0x0006: + if (item.Value.Length == 8) + { + double altitude = BitConverter.ToUInt32(item.Value, 0) * 1.0d / BitConverter.ToUInt32(item.Value, 4); + list[2] = altitude.ToString("0.000"); + } + break; + } + } + } + + return string.Format("POINT Z ({0})", string.Join(" ", list.ToArray())); + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + return null; + } + finally + { + if (null != img) img.Dispose(); + } + } + + /// <summary> + /// 鑾峰彇GPS鍊� + /// </summary> + private static string GetGpsVal(byte[] bytes) + { + if (bytes.Length != 24) return "0"; + + //degrees(灏哹yte[0]~byte[3]杞垚uint, 闄や互byte[4]~byte[7]杞垚鐨剈int) + double d = BitConverter.ToUInt32(bytes, 0) * 1.0d / BitConverter.ToUInt32(bytes, 4); + + //minutes(灏哹yte[8]~byte[11]杞垚uint, 闄や互byte[12]~byte[15]杞垚鐨剈int) + double m = BitConverter.ToUInt32(bytes, 8) * 1.0d / BitConverter.ToUInt32(bytes, 12); + + //seconds(灏哹yte[16]~byte[19]杞垚uint, 闄や互byte[20]~byte[23]杞垚鐨剈int) + double s = BitConverter.ToUInt32(bytes, 16) * 1.0d / BitConverter.ToUInt32(bytes, 20); + + double val = (((s / 60 + m) / 60) + d); + + return val.ToString("0.00000000"); + } } } -- Gitblit v1.9.3