Windows CE 中向JPG文件写入经纬度 时间等信息

最近在一个项目中  需要向JPG文件中写入时间  GPS位置等信息 如果是在PC上很好实现,但是在WinCE上由于.NET CF好多库都没有 所以实现起来还是挺困难,于是就在网上google看有什么开源的东西可以用,于是就找到了 ExifLib,在CodeProject的网址为http://www.codeproject.com/Articles/43665/ExifLibrary-for-NET,不过该源码为PC版的,要改改改才能在WinCE上运行,下面是示例代码

 

  /// 
        /// 
        /// 
        /// JPG文件路径
        /// GPSLatitudeRef枚举值
        /// 纬度  弧度值
        /// GPSLongitudeRef 枚举值
        /// 经度  弧度值
        /// DateTime 类型值  获取的GPS时间
        private void SetLatituLongitudeAndGPSTime(string filename, GPSLatitudeRef latituderef, double latitude, GPSLongitudeRef longituderef, double longitude, DateTime time)
        {
            ImageFile file=null;
            try
            {
                file = ImageFile.FromFile(filename);
                CDms latitudedms = new CDms();
                latitudedms.Radian = Math.Abs(latitude);
                CDms longitudedms = new CDms();
                longitudedms.Radian = Math.Abs(longitude);
                uint latituded = (uint)Math.Round(latitudedms.d, 2) * 100;
                uint latitudem = (uint)Math.Round(latitudedms.m, 2) * 100;
                uint latitudes = (uint)Math.Round(latitudedms.s, 2) * 100;
                uint longituded = (uint)Math.Round(longitudedms.d, 2) * 100;
                uint longitudem = (uint)Math.Round(longitudedms.m, 2) * 100;
                uint longitudes = (uint)Math.Round(longitudedms.s, 2) * 100;
                ExifEnumProperty gpslatituderef = new ExifEnumProperty(ExifTag.GPSLatitudeRef, latituderef);
                GPSLatitudeLongitude gpsatitudeite = new GPSLatitudeLongitude(ExifTag.GPSLatitude, new MathEx.UFraction32[] { new MathEx.UFraction32(latituded, 100), new MathEx.UFraction32(latitudem, 100), new MathEx.UFraction32(latitudes, 100) });
                ExifEnumProperty gpslongituderef = new ExifEnumProperty(ExifTag.GPSLongitudeRef, longituderef);
                GPSLatitudeLongitude gpslongitude = new GPSLatitudeLongitude(ExifTag.GPSLongitude, new MathEx.UFraction32[] { new MathEx.UFraction32(longituded, 100), new MathEx.UFraction32(longitudem, 100), new MathEx.UFraction32(longitudes, 100) });
                //uint hour = (uint)time.Hour;
                //uint miniute = (uint)time.Minute;
                //uint second = (uint)time.Second;
                //GPSTimeStamp gpstime = new GPSTimeStamp(ExifTag.GPSTimeStamp, new MathEx.UFraction32[] { new MathEx.UFraction32(hour, 1), new MathEx.UFraction32(miniute, 1), new MathEx.UFraction32(second, 1) });
                ExifAscii datetimeoriginal = new ExifAscii(ExifTag.DateTimeOriginal, time.ToString("yyyy:MM:dd HH:mm:ss"));
                file.Properties.Add(datetimeoriginal);
                file.Properties.Add(gpslatituderef);
                file.Properties.Add(gpsatitudeite);
                file.Properties.Add(gpslongituderef);
                file.Properties.Add(gpslongitude);
                //file.Properties.Add(gpstime);
                file.Save(filename);
            }
            catch
            {
                file.Save(filename);
                Program.Log.Error("向JPG文件写入GPS信息出错");
            }
        }


 /// 
        /// 往JPG图片上加入GPS信息
        /// 
        /// JPG文件路径
        /// 如果成功则返回添加了GPS信息的Bitmap,否则返回原图的Bitmap
        private Bitmap DrawGPSInfo(string filename)
        {
            ImageFile file = null;
            try
            {
                file = ImageFile.FromFile(filename);
                string timestr = file.Properties[ExifTag.DateTimeOriginal].Value.ToString();
                string latituderef = file.Properties[ExifTag.GPSLatitudeRef].Value.ToString();
                string latiude = (file.Properties[ExifTag.GPSLatitude] as ExifProperty).ToString();
                string longituderef = file.Properties[ExifTag.GPSLongitudeRef].Value.ToString();
                string longitude = (file.Properties[ExifTag.GPSLongitude] as ExifProperty).ToString();
                string[] strs1 = latiude.Split(new char[] { '.', '\'', '°' });
                string[] str2 = longitude.Split(new char[] { '.', '\'', '°' });
                string latitudestr = latituderef.Substring(0, 1).ToUpper() + ":" + strs1[0] + "°" + strs1[2] + "'" + strs1[4] + "\"";
                string longitudestr = longituderef.Substring(0, 1).ToUpper() + ":" + str2[0] + "°" + str2[2] + "'" + str2[4] + "\"";
                file.Save(filename);
                Font font = new Font("黑体", 10, FontStyle.Regular); //字体,大小
                SolidBrush brush = new SolidBrush(Color.Red); //笔刷,颜色
                Bitmap tempbitmap = new Bitmap(filename);
                Graphics g = Graphics.FromImage(tempbitmap);
                g.DrawString(timestr, font, brush, 10f, 10f);
                g.DrawString(latitudestr, font, brush, 10f, 30f);
                g.DrawString(longitudestr, font, brush, 10f, 50f);
                return tempbitmap;
            }
            catch
            {
                Program.Log.Error("JPG显示GPS信息出错");
                return new Bitmap(filename);
            }
        }


示例代码  和更改过后的源代码可到这下载

http://download.csdn.net/detail/xiaibiancheng/4972092
 

你可能感兴趣的:(C#,c#,ExifLib,gps,JPG,wince)