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

查看:30
本文介绍了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.

最初我以为我可以使用文件 API 的 FileEntry.getMetadata() 调用,但这不会返回修改时间属性的有效日期.我认为 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.

推荐答案

所以,我设法解决了这个问题.

So, I've managed to figure this out.

日期/时间戳以及一系列其他信息可以从 JPEG 文件内的 EXIF 数据标签中检索.这可以使用这个有用的 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 标签,只有在使用相机拍摄图像时才复制 EXIF 标签,所以这是一个问题,但我将通过分叉插件来解决这个问题.该插件的 iOS 版本似乎做了正确的事情.

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天全站免登陆