Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Media.selectPhoto Photo Date and Time Created Info?
Started by DonWono Mar 11 2016 04:56 PM

3 replies to this topic
media.select date time exif
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

DonWono

[GLOBAL: userInfoPane.html]
DonWono
  • Enthusiast

  • 31 posts
  • Corona SDK

Hi Corona community!

    I'm using Media.selectPhoto to open up the photo album, and allowing users to select a picture to import into my Corona SDK app. Is there a way to get the date and time that these pictures were taken?

 

I've tried to import the picture from the photo album to system.TemporaryDirectory, then using:

local pathOrigin = system.pathForFile( photoData[i].fileName, system.TemporaryDirectory );

local fileAtributes = lfs.attributes(pathOrigin);
local dateMod = fileAtributes.modification;
        
local dateTime = os.date("%Y:%m:%d %H:%M:%S", dateMod);

To get the date and time of when the picture was created. This works in the simulator on my computer, but when I tested it on my iPhone it returns the current date and time.

 

Any help will be much appreciated.

 

Thank you very much Corona Community!



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,061 posts
  • Enterprise

There isn't a way with Corona SDK that I know of. Corona Enterprise lets you access native iOS and Android SDK API functions and both have ways to read the images meta data.

 

Rob



[TOPIC: post.html]
#3

DonWono

[GLOBAL: userInfoPane.html]
DonWono
  • Enthusiast

  • 31 posts
  • Corona SDK

Thanks Rob.

 

I am able to use this function to extract the EXIF width and height of an image, any ideas on how I can get the date and time using it?

local function getJpegInfo(fh, path)
    
    local function unpackMsbUint16(s)
            local c,d = s:byte(1,#s)
            local num = (c * 256) + d
            return num
    end
    --
    local function readMsbUint16(fh)
            return unpackMsbUint16(fh:read(2))
    end
    --
    local filePath = path or nil
    local theFile = nil
    --
    if filePath == nil then
            theFile = system.pathForFile(fh, system.ResourceDirectory)
    else
            theFile = system.pathForFile(fh, path)
    end
    --
    local fileHandle = assert(io.open(theFile, 'rb'))
    --

    local self = {}
    --
    --JPEG Markers
    local jpegSOI  = string.char( 255 ) .. string.char( 216 )
    local jpegAPP0 = string.char( 255 ) .. string.char( 224 )
    local jpegSOF0 = string.char( 255 ) .. string.char( 192 )
    local jpegSOF2 = string.char( 255 ) .. string.char( 194 )

    --
    local tempBytes
    --
    repeat
            tempBytes = fileHandle:read(2)

    until ( ( tempBytes == jpegSOI ) or ( tempBytes == nil ) )
    --assert( tempBytes~=nil, "SOI not found. Are you sure this is a JPEG file?" )
    --

    local APP0Marker = fileHandle:read(2)
    --assert( APP0Marker == jpegAPP0, "APP0 Marker not what's expected. Is this really a JPEG file?")
    --

    
    

    local APP0Length = readMsbUint16(fileHandle) - 4 --we're 4 bytes into this thing already
    fileHandle:seek( "cur", APP0Length ) -- skipping ahead, but this is where the DPI is stored if you need it.
    --
    local readHead
    --
    repeat
            repeat
                    tempBytes = fileHandle:read(2)
            until ( ( tempBytes == jpegSOF0 ) or ( tempBytes == jpegSOF2 ) or ( tempBytes == nil ))        
            if tempBytes ~= nil then
                    readHead = fileHandle:seek()
                    if (tempBytes == jpegSOF0 or tempBytes == jpegSOF2) then
                        print("Found SOF0:", tempBytes == jpegSOF0, "SOF2:", tempBytes == jpegSOF2)
                    end

            end
    until tempBytes == nil

    
    if readHead then
            fileHandle:seek("set", readHead)
            fileHandle:read(3) -- length of frame
            self.bitDepth = readByte(fileHandle) -- bit depth
            self.height = readMsbUint16(fileHandle) -- image height
            self.width = readMsbUint16(fileHandle) -- image width
            print(readHead);
    end
                            
    --close the file
    io.close(fileHandle)
    fileHandle = nil
    return self
end

The credit for the function above goes to Toby2.

 

I've been playing around with the function and I'm able to also get the image DPI info, but not the date and time created. Any help will be appreciated.

 

Thanks guys!



[TOPIC: post.html]
#4

DonWono

[GLOBAL: userInfoPane.html]
DonWono
  • Enthusiast

  • 31 posts
  • Corona SDK

I was able to extract the date and time created from an image in Simulator using the following code that I wrote:

local function ExtractDateTimeFile(photoName)
    local filePath = system.pathForFile(photoName, system.TemporaryDirectory);

    local file = io.open(filePath);
    local jpg = file:read(1000);

    
    
    local ofst = 0;
    for jr = 2000, 2030 do
         ofst = string.find(jpg, jr);
        if (ofst ~= nil and ofst > 0) then
            print(ofst);
            break;
        end
    end


    if (ofst == 0) then
        if (string.find(jpg,"PC100")) then
            ofst = 169;
        elseif (string.find(jpg, "FUJIFILM")) then
            ofst = 241;
        elseif (string.find(jpg, "KODAK")) then
            ofst = 449;
        elseif (string.find(jpg, "CYBERSHOT")) then
            ofst = 211;
        elseif (string.find(jpg, "MINOLTA")) then
            ofst = 277;
        elseif (string.find(jpg, "iPhone")) then
            print("iPhone Camera Image Detected")
            ofst = 197;
        end
    end

    io.close(file);

    if (ofst == nil or ofst == 0) then
        print("Oh no! Can't get the meta data date and time")
        return 0;
    end

    local ofstEnd = ofst+19;

    local dateTime = jpg:sub(ofst, ofstEnd);
    print("This is the date and time taken from the metadata: "..dateTime);
    return dateTime;
end

Unfortunately on the actual iPhone, it's unable to get the date and time information. I did a little digging and it seems like on the iPhone when I copy the image that I select from the photo album to system.temporaryDirectory, it strips away the date and time information from the image. Is this correct? When running on the simulator this is not an issue and the date and time is returned properly.




[topic_controls]
[/topic_controls]