管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-01-06 15eabfdabf30973c1e99b015060b7da2190ccdf1
获取图片的GPS信息
已添加3个文件
已修改5个文件
251 ■■■■■ 文件已修改
JiangSu/App_Start/WebApiConfig.cs 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/Controllers/ImgController.cs 58 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/JiangSu.csproj 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/Models/FileDesc.cs 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/cs/CustomMultipartFormDataStreamProvider.cs 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/cs/NoBufferPolicySelector.cs 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/cs/Tools.cs 104 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/index.html 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiangSu/App_Start/WebApiConfig.cs
@@ -1,8 +1,10 @@
using Newtonsoft.Json.Converters;
using JiangSu.cs;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Hosting;
namespace JiangSu
{
@@ -17,6 +19,9 @@
                defaults: new { id = RouteParameter.Optional }
            );
            // æ³¨å†Œæ‰©å±•主机缓存Policy
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector());
            // å±è”½è¿”回XML格式
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
JiangSu/Controllers/ImgController.cs
@@ -2,15 +2,23 @@
using JiangSu.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web;
namespace JiangSu.Controllers
{
    public class ImgController : ApiController
    {
        public static string uploadPath = "Images";
        public static List<string> extension = new List<string> { ".jpg", "png" };
        [HttpGet]
        public List<Img> SelectByPage(string name, int pageSize = 10, int pageIndex = 1)
        {
@@ -86,5 +94,55 @@
                return 0;
            }
        }
        [HttpPost]
        public Task<IEnumerable<FileDesc>> Post()
        {
            string root = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, uploadPath);
            if (!Directory.Exists(root)) Directory.CreateDirectory(root);
            if (!Request.Content.IsMimeMultipartContent()) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            var provider = new CustomMultipartFormDataStreamProvider(root);
            var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith<IEnumerable<FileDesc>>(t =>
            {
                if (t.IsFaulted || t.IsCanceled) throw new HttpResponseException(HttpStatusCode.InternalServerError);
                List<FileDesc> infos = new List<FileDesc>();
                foreach (MultipartFileData f in provider.FileData)
                {
                    FileInfo info = new FileInfo(f.LocalFileName);
                    if (!extension.Contains(info.Extension.ToLower()))
                    {
                        File.Delete(f.LocalFileName);
                        continue;
                    }
                    string md5 = Tools.CalcFileMD5(f.LocalFileName);
                    string newName = md5 + info.Extension;
                    string newFile = Path.Combine(root, uploadPath, newName);
                    Img img = new Img();
                    img.name = info.Name;
                    img.path = uploadPath + "\\" + newName;
                    img.geom = Tools.GetImgGps(f.LocalFileName);
                    if (!File.Exists(newFile)) File.Move(f.LocalFileName, newFile);
                    int rows = ImgDAL.Insert(img);
                    infos.Add(new FileDesc(info.Name, info.Length));
                }
                return infos;
            });
            return task;
        }
        [HttpGet]
        public string GetGps()
        {
            return Tools.GetImgGps(@"D:\SEM\病害示例_pos\1.jpg");
        }
    }
}
JiangSu/JiangSu.csproj
@@ -142,17 +142,20 @@
    <Compile Include="Controllers\OpController.cs" />
    <Compile Include="Controllers\ValuesController.cs" />
    <Compile Include="Controllers\GridController.cs" />
    <Compile Include="cs\CustomMultipartFormDataStreamProvider.cs" />
    <Compile Include="cs\GridDAL.cs" />
    <Compile Include="cs\ImgDAL.cs" />
    <Compile Include="cs\LogOut.cs" />
    <Compile Include="cs\ModelDAL.cs" />
    <Compile Include="cs\ModelHandler.cs" />
    <Compile Include="cs\NoBufferPolicySelector.cs" />
    <Compile Include="cs\PostgreHelper.cs" />
    <Compile Include="cs\SQLiteHelper.cs" />
    <Compile Include="cs\Tools.cs" />
    <Compile Include="Global.asax.cs">
      <DependentUpon>Global.asax</DependentUpon>
    </Compile>
    <Compile Include="Models\FileDesc.cs" />
    <Compile Include="Models\Grid.cs" />
    <Compile Include="Models\Img.cs" />
    <Compile Include="Models\Model.cs" />
JiangSu/Models/FileDesc.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,24 @@
using JiangSu.cs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace JiangSu.Models
{
    /// <summary>
    /// æ–‡ä»¶æè¿°
    /// </summary>
    public class FileDesc
    {
        public FileDesc(string name, long length)
        {
            this.Name = name;
            this.Length = Tools.FormatBytes(length);
        }
        public string Name { set; get; }
        public string Length { set; get; }
    }
}
JiangSu/cs/CustomMultipartFormDataStreamProvider.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
namespace JiangSu.cs
{
    public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
    {
        public CustomMultipartFormDataStreamProvider(string path)
            : base(path)
        { }
        public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
        {
            //string name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
            //return name.Replace("\"", string.Empty);
            return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
        }
    }
}
JiangSu/cs/NoBufferPolicySelector.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http.WebHost;
namespace JiangSu.cs
{
    public class NoBufferPolicySelector : WebHostBufferPolicySelector
    {
        public override bool UseBufferedInputStream(object hostContext)
        {
            var context = hostContext as HttpContextBase;
            if (context != null)
            {
                if (context.Request.HttpMethod == HttpMethod.Post.ToString() && context.Request.ContentLength > 102400)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
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>
        /// è®¡ç®—文件的MD5码
        /// </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>
        /// èŽ·å–å›¾ç‰‡çš„GPS信息
        /// </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(将byte[0]~byte[3]转成uint, é™¤ä»¥byte[4]~byte[7]转成的uint)
            double d = BitConverter.ToUInt32(bytes, 0) * 1.0d / BitConverter.ToUInt32(bytes, 4);
            //minutes(将byte[8]~byte[11]转成uint, é™¤ä»¥byte[12]~byte[15]转成的uint)
            double m = BitConverter.ToUInt32(bytes, 8) * 1.0d / BitConverter.ToUInt32(bytes, 12);
            //seconds(将byte[16]~byte[19]转成uint, é™¤ä»¥byte[20]~byte[23]转成的uint)
            double s = BitConverter.ToUInt32(bytes, 16) * 1.0d / BitConverter.ToUInt32(bytes, 20);
            double val = (((s / 60 + m) / 60) + d);
            return val.ToString("0.00000000");
        }
    }
}
JiangSu/index.html
@@ -116,7 +116,10 @@
  </script>
</head>
<body>
  <a href="http://localhost/JiangSu/Values/Get">Test</a> <br /> <br />
  <a href="http://localhost/JiangSu/Values/Get">Test</a>
  <span style="width: 50px;">&nbsp;</span>
  <a href="http://localhost/JiangSu/Img/GetGps">GetGps</a>
  <br /> <br />
  <a href="http://localhost/JiangSu/Op/SelectByPage?pageSize=10&pageIndex=1&name=">SelectByPage?pageSize=10&pageIndex=1&name=</a> <br />