2019-05-22  1,462 views 评论

ASP.NET小程序开发上传接口获取图片GPS信息和其他EXFI信息

在近期的项目开发中 要求在小程序端实现照片 视频上传并校验照片和视频的真实性

要求拍摄的照片和视频与用户基本信息中填写的地址GPS信息相符(想近即可)并且拍摄时间为上传前的时间。

照片的解决方案比较简单,一般手机拍摄的照片都会包含EXFI信息(拍摄设备型号、拍摄时间、地址、光圈、ISO等信息),视频暂不知道如何校验,貌似大部分视频都不会包含GPS信息,如果有哪位知道如何校验视频的相关信息希望不吝赐教。

EXIF信息,是可交换图像文件的缩写,是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据。EXIF可以附加于JPEG、TIFF、RIFF等文件之中,为其增加有关数码相机拍摄信息的内容和索引图或图像处理软件的版本信息。

小程序端/公众号/APP端请求本地相机或直接上传图片,请求服务端API(前后端结合调用空了写个Demo直接上传,先提供服务端代码)

需要安装以下Nuget依赖:

MetadataExtractor  (当前最新版本为2.0)

具体实现代码如下:

    /// <summary>
    /// 获取图片EXIF信息
    /// </summary>
    /// <param name="imgPath">照片绝对路径</param>
    /// <returns></returns>
    public static Dictionary<string, string> GetImgExif(string imgPath)
    {
        var rmd = ImageMetadataReader.ReadMetadata(imgPath);
        var rt = new Dictionary<string, string>();
        foreach (var rd in rmd)
        {
            foreach (var tag in rd.Tags)
            {
                var temp = EngToChs(tag.Name);
                if (temp == "其他")
                {
                    continue;
                }
                if (!rt.ContainsKey(temp))
                {
                    rt.Add(temp, tag.Description);
                }

            }
        }
        return rt;
    }
    /// <summary>
    /// 筛选参数并将其名称转换为中文
    /// </summary>
    /// <param name="str">参数名称</param>
    /// <returns>参数中文名</returns>
    private static string EngToChs(string str)
    {
        var rt = "其他";
        switch (str)
        {
            case "Exif Version":
                rt = "Exif版本";
                break;
            case "Model":
                rt = "相机型号";
                break;
            case "Lens Model":
                rt = "镜头类型";
                break;
            case "File Name":
                rt = "文件名";
                break;
            case "File Size":
                rt = "文件大小";
                break;
            case "Date/Time":
                rt = "拍摄时间";
                break;
            case "File Modified Date":
                rt = "修改时间";
                break;
            case "Image Height":
                rt = "照片高度";
                break;
            case "Image Width":
                rt = "照片宽度";
                break;
            case "X Resolution":
                rt = "水平分辨率";
                break;
            case "Y Resolution":
                rt = "垂直分辨率";
                break;
            case "Color Space":
                rt = "色彩空间";
                break;

            case "Shutter Speed Value":
                rt = "快门速度";
                break;
            case "F-Number":
                rt = "光圈";
                break;
            case "ISO Speed Ratings":
                rt = "ISO";
                break;
            case "Exposure Bias Value":
                rt = "曝光补偿";
                break;
            case "Focal Length":
                rt = "焦距";
                break;

            case "Exposure Program":
                rt = "曝光程序";
                break;
            case "Metering Mode":
                rt = "测光模式";
                break;
            case "Flash Mode":
                rt = "闪光灯";
                break;
            case "White Balance Mode":
                rt = "白平衡";
                break;
            case "Exposure Mode":
                rt = "曝光模式";
                break;
            case "Continuous Drive Mode":
                rt = "驱动模式";
                break;
            case "Focus Mode":
                rt = "对焦模式";
                break;
            case "GPS Latitude Ref":
                rt = "纬线类型";
                break;
            case "GPS Latitude":
                rt = "纬度";
                break;
            case "GPS Longitude Ref":
                rt = "经线类型";
                break;
            case "GPS Longitude":
                rt = "经度";
                break;
            case "GPS Time-Stamp":
                rt = "GPS UTC时间";
                break;
            case "GPS Date Stamp":
                rt = "GPS拍摄日期";
                break;
        }
        return rt;
    }
}

具体调用:

public JsonResult upFile(Controller cxt)
{
    try
    {
        uploadpath = Server.MapPath("/Upload/");
        uploadFile = cxt.Request.Files[0];
        oldName = uploadFile.FileName;
        if (!System.IO.Directory.Exists(uploadpath))
        {
            System.IO.Directory.CreateDirectory(uploadpath);
        }
        fileName = Guid.NewGuid().ToString() + uploadFile.FileName.Substring((uploadFile.FileName.LastIndexOf('.')), (uploadFile.FileName.Length- uploadFile.FileName.LastIndexOf('.')));
        uploadFile.SaveAs(uploadpath + fileName);

        var exfi = GetImgExif(uploadpath + fileName);

        return Json(new {Code=0,Message="上传成功!" });
    }
    catch (Exception ex)
    {
        return Json(new { Code = 1001, Message = "上传失败!"+ex.Message });
    }
}

 

使用ImageMetadataReader.ReadMetadata 加载图片信息如下,可以加载到大几十个属性(只要照片信息中包含),当前已经整理30个属性,基本上已经满足日常使用,如果有额外需要可以自己在EngToChs方法中新增。

转成功后的字典信息如下:

 

给我留言

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: