Phonegap / Cordova camera plugin - how to get photo's date/time stamp?

后端 未结 2 702
情歌与酒
情歌与酒 2020-12-03 09:06

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.

I need to ca

相关标签:
2条回答
  • 2020-12-03 09:39

    So, I've managed to figure this out.

    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

    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);
    
    0 讨论(0)
  • 2020-12-03 09:47

    Like everyone else I dislike answers that start with "do this a completely different way" but I had the same problems as the original poster and using the alternate camera plugin cordova-plugin-camera-with-exif was the best solution I found.

    0 讨论(0)
提交回复
热议问题