Phonegap / Cordova相机插件 - 如何获取照片的日期/时间戳? [英] Phonegap / Cordova camera plugin - how to get photo's date/time stamp?

查看:404
本文介绍了Phonegap / Cordova相机插件 - 如何获取照片的日期/时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Phonegap应用程式,需要让使用者透过手机的相机拍摄相片,让使用者从装置上已有的相片中挑选。

I have a Phonegap app that needs to let the user both take photos using the phone's camera and let the user select from photo's already on the device.

我需要以捕获照片作为元数据的一部分的日期/时间,但我很难用Phonegap / Cordova来了解如何做到这一点。

I need to capture the date/time the photo was taken as part of the metadata, but I'm having a hard time figuring out how to do this with Phonegap / Cordova.

最初,我想我可以使用File API的FileEntry.getMetadata()调用,但这不会返回一个有效的日期的modificationTime属性。我认为phonegap也转换设备上的文件,所以你从相机插件回来不是设备上的原始文件,所以即使getMetadata()调用工作,日期将不是正确的。

Initially I thought I could use the File API's FileEntry.getMetadata() call but this doesn't return a valid date for the modificationTime attribute. I think that phonegap also transforms the file on the device so that the you recieve back from the camera plugin is not the original file on the device, so even if the getMetadata() call worked, the date wouldn't be the correct one.

有没有其他方法,没有为我需要的每个平台编写我自己的版本的相机插件?

Is there any other way around this, short of writing my own version of the camera plugin for each platform I need?

看起来很疯狂,这是唯一的方法。

Seems crazy that this would be the only way around it.

推荐答案

日期/时间戳以及一堆其他信息可以从EXIF数据标签中检索出来。 JPEG文件。这可以使用此有用的JS库 - https://github.com/jseidelin/exif-js

The date/time stamp along with a bunch of other information can be retrieved from the EXIF data tags inside the JPEG file. This can be done using this helpful JS library - https://github.com/jseidelin/exif-js

不幸的是,Android的Cordova摄像头插件在转换从图库中选择的图片时不会复制EXIF标签,只有当使用摄像头拍摄图片时,这是一个问题,但是我将通过分叉插件来解决这个问题。

Unfortunately the Cordova camera plugin for Android doesn't copy EXIF tags when transforming an image selected from the gallery, only when taking an image using the camera, so this is a problem, but I will fix this by forking the plugin. The iOS version of the plugin seems to do this right thing.

任何感兴趣的代码 -

Code for anyone interested -

var source = fromCamera 
       ? Camera.PictureSourceType.CAMERA 
       : Camera.PictureSourceType.PHOTOLIBRARY;

var opts = {
    encodingType: Camera.EncodingType.JPEG,
    sourceType: source,
    destinationType: Camera.DestinationType.NATIVE_URI
};

navigator.camera.getPicture(
        function(imageURI) {
            window.resolveLocalFileSystemURL(imageURI,
                    function(entry) {
                        entry.file(function(file) {
                            EXIF.getData(file, function() {
                                var datetime = EXIF.getTag(this, "DateTimeOriginal");
                                alert(datetime);
                            });                                                

                            // do something useful....

                        }, standardErrorHandler);
                    },
                    function(e) {
                        alert('Unexpected error obtaining image file.');
                        standardErrorHandler(e);
                    });
        },
        function() {
            // nada - cancelled
        },
        opts);

这篇关于Phonegap / Cordova相机插件 - 如何获取照片的日期/时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆