使用Ruby解析图片EXIF数据获取坐标信息

最近在做一个项目时需要将图片EXIF信息解析出来并获取相应GPS坐标信息,用于在Google Map中使用, 找到了一些Ruby解析EXIF信息的类库, 相比之下还是 exifr这个gem不错, 零依赖,直接ruby调用.

1. 获取EXIF信息

require 'rubygems'
require 'exifr'

obj = EXIFR::JPEG.new('geo.jpg')
if obj.exif?
  puts "--- EXIF information ---".center(50)
  hash??= obj.exif.to_hash
  hash.each_pair do |k, v|
    puts "-- #{k.to_s.rjust(20)} -> #{v}"
  end
end

 运行上述代码后效果如下:

 
             --- EXIF information ---             
--     gps_latitude_ref -> N
--    pixel_x_dimension -> 600
--   date_time_original -> Sat Nov 21 09:24:08 +0800 2009
--         y_resolution -> 72
--      resolution_unit -> 2
-- gps_img_direction_ref -> T
--     exposure_program -> 2
--   ycb_cr_positioning -> 1
--            sharpness -> 1
--    pixel_y_dimension -> 800
--                flash -> 32
--  date_time_digitized -> Sat Nov 21 09:24:08 +0800 2009
--                 make -> Apple
--    gps_img_direction -> 102933/295
--        gps_longitude -> 104809/200
--         focal_length -> 77/20
--                model -> iPhone 3GS
--             software -> 3.1.2
--       gps_time_stamp -> 924417/100
--    iso_speed_ratings -> 76
--    gps_longitude_ref -> W
--            date_time -> Sat Nov 21 09:24:08 +0800 2009
--        exposure_mode -> 0
--  shutter_speed_value -> 5855/1277
--        exposure_time -> 1/24
--         gps_latitude -> 391019/200
--       sensing_method -> 2
--          color_space -> 1
--        metering_mode -> 1
--         x_resolution -> 72
--        white_balance -> 0
--       aperture_value -> 4281/1441
--             f_number -> 14/5

 未加工前的坐标信息是以时/分/秒构成的, 类似这样:

:gps_latitude=>[Rational(39, 1), Rational(1019, 20), Rational(0, 1)]
 为了在Google Map中显示图片拍摄的位置信息,需要得到坐标的两个值, 但直接获取的数据仍然需要进行一下加工方可正常使用.

2. 加工坐标信息

lat = obj.exif[0].gps_latitude[0].to_f + (obj.exif[0].gps_latitude[1].to_f / 60) + (obj.exif[0].gps_latitude[2].to_f / 3600)
lat = lat * -1 if obj.exif[0].gps_latitude_ref == 'S'    # (N is +, S is -)
long = obj.exif[0].gps_longitude[0].to_f + (obj.exif[0].gps_longitude[1].to_f / 60) + (obj.exif[0].gps_longitude[2].to_f / 3600) 
long = long * -1 if obj.exif[0].gps_longitude_ref == 'W' # (W is -, E is +)

 加工后的坐标信息类似这样:

39.8491666666667 #  lat
-104.674166666667 # long

 坐标转换方法

# Example. Assume a latitude of 45° 53' 36" (45 degrees, 53 minutes and 36 seconds). In degrees, the latitude will be:
latitude = 45 + (53 / 60) + (36 / 3600) = 45.89
# General Formulation:
latitude (degrees) = degrees + (minutes / 60) + (seconds / 3600)
 完事, 收工. 如果希望得到验证,可以将产生的坐标信息录入>这里<进行检查.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值