使用照片或者录像的拍摄日期命名文件

该程序使用自定义GetPhotoDate()函数获取照片或者录像的“拍摄日期”,作为该照片的文件名。有的照片或者录像没有”拍摄日期“,就不能使用该程序,应该用FileDateTime( )函数获取”创建日期“。
 
  1. Dim Index As Integer
  2. Private Type GdiplusStartupInput
  3.     GdiplusVersion As Long
  4.     DebugEventCallback As Long
  5.     SuppressBackgroundThread As Long
  6.     Suppres***ternalCodecs As Long
  7. End Type
  8.  
  9.  
  10. Private Type PropertyItem
  11.    propId As Long ' ID of this property
  12.    Length As Long ' Length of the property value, in bytes
  13.    Type As Long ' Type of the value, as one of TAG_TYPE_XXX defined above
  14.    Value As Long ' property value
  15. End Type
  16.  
  17.  
  18. Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
  19. Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
  20. Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As Long, hImage As Long) As Long
  21. Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
  22.  
  23. Private Declare Function GdipGetPropertyCount Lib "gdiplus" (ByVal Image As Long, numOfProperty As Long) As Long
  24. Private Declare Function GdipGetPropertyIdList Lib "gdiplus" (ByVal Image As Long, ByVal numOfProperty As Long, list As Long) As Long
  25. Private Declare Function GdipGetPropertyItemSize Lib "gdiplus" (ByVal Image As Long, ByVal propId As Long, Size As Long) As Long
  26. Private Declare Function GdipGetPropertyItem Lib "gdiplus" (ByVal Image As Long, ByVal propId As Long, ByVal propSize As Long, Buffer As Long) As Long
  27. Private Declare Function GdipGetPropertySize Lib "gdiplus" (ByVal Image As Long, totalBufferSize As Long, numProperties As Long) As Long
  28. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDst As Any, lpSrc As Any, ByVal ByteLength As Long)
  29.  
  30.  
  31.  
  32. Private Function GetPhotoDate(ImagePath As String) As String
  33.     Dim Bitmap As Long
  34.     Dim Token As Long
  35.     Dim Index As Long
  36.     Dim PropertyCount As Long
  37.     Dim ItemSize As Long
  38.     Dim Prop As PropertyItem
  39.     Dim GdipInput As GdiplusStartupInput
  40.     Const PropertyTagExifDTOrig As Long = &H9003& ' Date & time of original
  41.  
  42.     GdipInput.GdiplusVersion = 1
  43.     GdiplusStartup Token, GdipInput
  44.     GdipLoadImageFromFile StrPtr(ImagePath), Bitmap
  45.     GdipGetPropertyCount Bitmap, PropertyCount
  46.     ReDim PropertyList(PropertyCount - 1) As Long
  47.     GdipGetPropertyIdList Bitmap, PropertyCount, PropertyList(0)
  48.     For Index = 0 To PropertyCount - 1
  49.         GdipGetPropertyItemSize Bitmap, PropertyList(Index), ItemSize
  50.         ReDim Buffer(ItemSize - 1) As Byte
  51.         GdipGetPropertyItem Bitmap, PropertyList(Index), ItemSize, ByVal VarPtr(Buffer(0))
  52.         CopyMemory Prop, ByVal VarPtr(Buffer(0)), Len(Prop)
  53.         ReDim Data(ItemSize - 16) As Byte
  54.         CopyMemory Data(0), ByVal Prop.Value, ItemSize - 16
  55.         Select Case PropertyList(Index)
  56.         Case PropertyTagExifDTOrig
  57.             GetPhotoDate = StrConv(Data, vbUnicode)
  58.         End Select
  59.     Next
  60.     GdipDisposeImage Bitmap
  61.     GdiplusShutdown Token
  62. End Function
  63.  
  64.  
  65.  
  66.  
  67. Private Sub Command1_Click()
  68.     Dim filename, newfilename$, newfilenameback$, newfilenamekz$, filedt$, exname$
  69.     Dim i%, indexphoto%
  70.     Dim ii%, aa$, cc$
  71.     Dim photoname$(), attachnum%()
  72.     ReDim photoname(1 To File1.ListCount)
  73.     ReDim attachnum(1 To File1.ListCount)
  74.     
  75.     For i = 1 To File1.ListCount
  76.         attachnum(i) = 1
  77.         photoname(i) = ""
  78.     Next i
  79.     indexphoto = 1
  80.     
  81.     For i = 0 To File1.ListCount - 1
  82.         aa = ""
  83.         cc = ""
  84.         File1.ListIndex = i
  85.         filename = CStr(File1.filename)
  86.         exname = Right(filename, 4)
  87.         
  88.         filedt = GetPhotoDate("e:\photo\" & filename)
  89.         filedt = Trim(filedt)
  90.         'MsgBox filedt
  91.         newfilename = getdtname(filedt)
  92.         For ii = 1 To Len(newfilename)
  93.             cc = Mid(newfilename, ii, 1)
  94.             If IsNumeric(cc) = True Then
  95.                 aa = aa & cc
  96.             End If
  97.         Next ii
  98.         newfilename = aa
  99.         newfilenamekz = newfilename & exname
  100.         newfilenameback = newfilenamekz
  101.         '如果有重名的文件,就在文件名后增加一个数字,和重名文件区别开
  102.         If ifsamename(photoname(), newfilenamekz) = True Then
  103.             newfilenamekz = (newfilename & attachnum(Index)) & exname
  104.             attachnum(Index) = attachnum(Index) + 1
  105.         Else
  106.             photoname(indexphoto) = newfilenameback
  107.             indexphoto = indexphoto + 1
  108.         End If
  109.         
  110.         Name "e:\photo\" & filename As "e:\photo\" & newfilenamekz
  111.     Next i
  112.     MsgBox "OK!"
  113. End Sub
  114.  
  115. Private Sub Form_Load()
  116.     File1.Path = "e:\photo\"
  117. End Sub
  118. Private Function getdtname(dt As String) As String
  119.     '文件的创建时间格式为2010-05-13 19:30:23,即为形参dt。
  120.     '通过getdtname函数得到以文件创建时间为名字的文件名,格式为20100513193023
  121.     Dim dtarray() As String, darray() As String, tarray() As String
  122.     If Len(dt) > 0 Then
  123.         dtarray() = Split(dt)
  124.         'MsgBox dtarray(0)
  125.         'MsgBox dtarray(1)
  126.         darray() = Split(dtarray(0), ":")
  127.         'MsgBox darray(0)
  128.         'MsgBox darray(1)
  129.         'MsgBox darray(2)
  130.         tarray() = Split(dtarray(1), ":")
  131.         'MsgBox tarray(0)
  132.         'MsgBox tarray(1)
  133.         'MsgBox tarray(2)
  134.         If Len(darray(1)) = 1 Then darray(1) = 0 & darray(1)
  135.         If Len(darray(2)) = 1 Then darray(2) = 0 & darray(2)
  136.         If Len(tarray(0)) = 1 Then tarray(0) = 0 & tarray(0)
  137.         If Len(tarray(1)) = 1 Then tarray(1) = 0 & tarray(1)
  138.         If Len(tarray(2)) = 1 Then tarray(2) = 0 & tarray(2)
  139.         getdtname = darray(0) & darray(1) & darray(2) & tarray(0) & tarray(1) & tarray(2)
  140.     End If
  141.  
  142. End Function
  143. Private Function ifsamename(name$(), destname As String) As Boolean
  144. '判断和destname是否有重名的文件
  145.     Dim Length As Integer, name_index%
  146.     'length = UBound(name()) - LBound(name()) + 1
  147.     For name_index = LBound(name()) To UBound(name())
  148.         If destname = name(name_index) Then
  149.             ifsamename = True
  150.             Index = name_index
  151.             Exit For
  152.         Else
  153.             ifsamename = False
  154.         End If
  155.     Next name_index
  156.         
  157. End Function
rar.gifuploading.4e448015.gif转存失败rar.gifuploading.4e448015.gif转存失败rar.gifuploading.4e448015.gif转存失败rar.gif 用照片的拍照日期命名.rar   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值