如何获取照片的拍摄日期和时间?
我正在 Delphi 中编写一个程序,它应该获取用相机拍摄的照片的日期和时间,然后它会重命名该文件以包含它找到的日期+时间。
到目前为止,我已经通过以二进制方式打开文件并搜索特殊的字节顺序来实现这一点。这些字节后面跟着日期和时间。所以我遇到了一个问题。其实问题很少。
因为它一个接一个地读取文件,所以读取文件是一个缓慢的过程。如果找到日期,它通常位于文件的开头,不会花费很长时间,但是如果未找到“特殊字节顺序”,它将读取整个文件。所以我的方法太慢了。
即使是用同一台相机拍摄的,某些图片中的特殊字节顺序也可能会发生变化(我不知道为什么)。因此,我的程序有时无法在文件中找到日期,即使它在那里。
Windows 资源管理器在所有图片中查找日期都没有问题,所以我在想也许有某种特殊功能可以满足我的需要?
如何从图片中获取我需要的信息,以便它适用于所有格式?
谢谢
I am writing a program in Delphi which should get the date and time of the picture which was taken with photo cam and then it would rename the file to include the date+time it found.
So far i have achieved that by opening file as binary and searching for a special order of bytes. These bytes were then followed by the date and then time. So i've come to a problem. Actually few problems.
Because it reads the file 1 byte after another, reading a file is a slow process. If the date was found, it is usually at the start of file, it doesn't take long, however if 'special byte order' was not found, it will read the whole file. So my method is way too slow.
The special byte order may change in some pictures (i have no idea why) even if it was taken with the same camera. So my program sometimes fails to find the date in the file even though it is there.
Windows explorer has no problems finding date in all of the pictures, so i was thinking maybe there are some kind of special functions which could get me what i need?
How do i get the information i need from the picture so it works with all the formats?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我觉得你只需要看看EXIF信息就可以了。 http://en.wikipedia.org/wiki/Exif
有一些开源工具可以完成那个,但我不知道 Delphi 的具体情况。如果你不害怕 Java,你可以看一下这个开源项目的源代码:http: //sourceforge.net/projects/jexifviewer/ 查看他们如何评估日期字段。
然后,您可以优化您的阅读器,仅查看相关区域。您可能需要记住,Java 中的字节顺序与 Delphi 中的字节顺序不同。
I think you only need to look at the EXIF information. http://en.wikipedia.org/wiki/Exif
There are some open source tools which accomplish that, but I don't know of anything Delphi specific. If you're not scared of Java, you can have a look at the source code of this open source project: http://sourceforge.net/projects/jexifviewer/ to see how they evaluate the date field.
You can then optimise your reader, to only look at the relevant area. You might want to keep in mind that the Endianness in Java is different to Delphi.
Jules 关于 Exif 数据的说法是正确的;你可能想尝试这个Delphi库:
http://delphihaven.wordpress.com/ccr-exif/< /a>
Jules is right about Exif data; you might want to try this Delphi library:
http://delphihaven.wordpress.com/ccr-exif/
这是一个查看 Exif 数据的神奇网站。
http://regex.info/exif.cgi
This is an amazing site to view Exif data.
http://regex.info/exif.cgi
如果它是图形文件(如您的情况),要使用对话框打开它,请在窗体上放置一个
TOpenPictureDialog
组件。还放置组件
TLabel
,用于显示文件的创建日期。在按钮的位置添加以下代码:
为了在代码中打开 jpeg 和 png 文件,在
uses
行中,您需要添加两个库的名称JPEG
,PNGImage
。如果您有文件的完整地址,则可以编写上面的代码而不是上面的代码:
如果您只需要日期,不需要文件创建时间,则使用
DateTimeToStr
命令代替DateToStr
命令。If it is a graphic file (as in your case), to open it with a dialog box, place a
TOpenPictureDialog
component on the form.Also place the component
TLabel
, for displaying the date of creation of the file.In the button's place the following code:
In order to open jpeg and png files in the code, in the line
uses
you need to add the name of the two libraries,JPEG
,PNGImage
.If you have the full address of your file, you can write the code above instead of the code above:
If you only need the date, without the file creation time, then instead of the
DateTimeToStr
command, use theDateToStr
command.