473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

how to read exif information?

sam
same as subject?
Nov 5 '08 #1
3 8861
Try :
http://www.eggheadcafe.com/articles/20030706.asp

(and please always try Google first, you have tons of content, please ask a
specific question if you tried and have a particular problem).
--
Patrice

"sam" <fa*********@sina.coma écrit dans le message de groupe de discussion
: OD**************@TK2MSFTNGP05.phx.gbl...
same as subject?
Nov 5 '08 #2
On Nov 5, 4:16*pm, "sam" <fansongy...@sina.comwrote:
same as subject?
..NET or GDI+ doesn't provide reading and writing Exif metadata
directly. However you can also check these out to accomplish Exif
manipulation:

http://www.codeproject.com/KB/graphi...ag_Editor.aspx
http://www.codeproject.com/KB/vb/exif_reader.aspx
http://www.codeproject.com/KB/graphics/cexif.aspx

and search group.

HTH,

Onur Güzel
Nov 5 '08 #3
sam wrote:
same as subject?
How can I write a short reply to a mere snippet of a question like that?

<minor preamble rant>Open an image file in a text editor to get a feel for
how easy it's going to be to extract the data. Actually manage to find
documentation that doesn't skip the important bits (the Photoshop 6 SDK has
a doc with the tag numbers you'll need - in the docs about tiffs). Read that
and wonder why some length data includes the length of the length data and
some doesn't. Realize that all you have to do is get to the xmp stuff, so
all the weird iptc etc metadata format doesn't matter. Figure out how to
extract the xmp data into a string via two encodings. Marvell at the
stupidity of making xml appear to be human-readable when it's only really
meant for computers, except that you're the one who has to tell the computer
how to understand it. Fiddle around with it until you coerce the data into
the format you want.</rant>

Read in the file (actually just the first 32KB will hopefully contain the
EXIF data).

Parse through the tags until you find the appropriate section. Extract that
(XMP) section into a stringbuilder, remembering that UTF8 is used.

Put that into a string, which will magically take it into the .net framework
world of UTF16.

Get rid of the <?xpacket stuff as that only messes things up.

Similarly, get rid of the namespacey stuff.

[See below for a function to do all that.]

Now you can use the Microsoft.Xml.XQuery to apply xqueries to the data.
(' XQuery info at http://aspnet.4guysfromrolla.com/articles/071603-1.aspx
' XQuery.msi available from
http://aspnet.4guysfromrolla.com/code/XQueryStuff.zip )

///////////////////
Dim doc As New XPathDocument(New StringReader(getXMPdata()))
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim col As New XQueryNavigatorCollection
col.AddNavigator(nav, "xmp")
\\\\\\\\\\\\\\\\\

Now, say that i has an enum value, and we are choosing which metadata to
extract:

/////////////////////
Select Case i
Case MetaDataType.Caption
query = "FOR $x IN document(""xmp"")//description RETURN $x//li"
Case MetaDataType.Keywords
query = "FOR $x IN document(""xmp"")//subject RETURN $x//li"
case ....
\\\\\\\\\\\\\\\\\\\\\

Now perform the xquery to get some raw xml:

//////////////////////
Dim expr As New XQueryExpression(query)
Dim rawXML As String = (expr.Execute(col)).ToXml()
\\\\\\\\\\\\\\\\\\\\

And depending on the type of the metadata, we may have to process it in
different ways to get the value/values:

//////////////////
' the item needs to be extracted from the remaining surrounding xml tags
' uses RegexOptions.Singleline because sometimes trailing CRLFs get in
Select Case i
Case MetaDataType.Caption
myCaption = Regex.Replace(rawXML, "<li.*>(.+)</li>", "$1",
RegexOptions.Singleline).Trim
Case MetaDataType.Keywords
Dim mc As MatchCollection = Regex.Matches(rawXML,
"(?:<li>)(?<data>.+?)(?:</li>)")
For j As Integer = 0 To mc.Count - 1
myKeywords.Add(mc(j).Groups("data").Value)
Next
case...
\\\\\\\\\\\\\\\\\\\\
'Function to extract xap packet:

' Note: the following variables need to be declared at the class level:
' Dim src as Byte() ' and read in the first 32KB of a jfif file.
' Dim ptr As Int32 = 0 ' pointer to current position in data

///////////////////////////////////
Friend Function getXMPdata() As String
reset()

Dim s As New StringBuilder

' Start of image tag
Dim SOI() As Byte = {&HFF, &HD8}
If Not (checkBytes(src, SOI)) Then
Throw New Exception("Wrong SOI")
End If

' application marker
Dim AppMarker() As Byte = {&HFF, &HE0}
If Not (checkBytes(src, AppMarker)) Then
Throw New Exception("Wrong AppMarker")
End If

' skip the appmarker segment
' s.Append("ptr=&" & Hex(ptr) & vbCrLf)
' s.Append("src(ptr)=" & src(ptr).ToString & " src(ptr) *256=" & (src(ptr)
* 256).ToString & " src(ptr + 1)=" & src(ptr + 1).ToString & vbCrLf)
ptr += (src(ptr) * 256) + src(ptr + 1)
' s.Append("ptr=&" & Hex(ptr) & vbCrLf)

' Now go through the segments until we find the APP1 one with the
namespace field containing http://ns.adobe.com/xap/1.0/
Dim APP1() As Byte = {&HFF, &HE1}
Dim markerFound As Boolean = False
'Dim segmentMarker As String

Dim namespaceFieldEncoding As New System.Text.ASCIIEncoding

's.Append("Seek APP14 0xFF 0xED" & vbCrLf)
While Not (markerFound)
If checkBytes(src, APP1) AndAlso namespaceFieldEncoding.GetString(src,
ptr + 2, 28) = "http://ns.adobe.com/xap/1.0/" Then
's.Append("Found at &" & Hex(ptr) & vbCrLf)
markerFound = True
Else
's.Append("Segment size: &" & Hex(src(ptr)) & " &" & Hex(src(ptr + 1)) &
" " & ((src(ptr) * 256) + src(ptr + 1)).ToString & vbCrLf)
ptr += (readBytes(src, 1)(0) * 256) + readBytes(src, 1)(0)
's.Append("ptr=" & ptr.ToString & vbCrLf)
If ptr >= UBound(src) Then
Throw New Exception("Out of data seeking APP1 marker.")
's.Append("Fallen off end." & vbCrLf)
'markerFound = True
End If
End If
End While

' Return s.ToString

' ptr is now at the start of the APP1 segment
' APP1 segment format:-
'[Byte Offset] [Field value] [Field name] [Length (bytes)]
Comments
'0 0xFFE1 APP1 2 APP 1 marker.
'2 2 + length of namespace (29) Lp 2 Size in
bytes of this count plus the
' + length of XMP Packet following
two portions.
'
'4 Null-terminated namespace 29 XMP
namespace URI, used as unique ID:
' ASCII string
http://ns.adobe.com/xap/1.0/
' without(quotation)
' marks.
'
'33 < XMP Packet Must be encoded
as UTF-8.
'

Dim APP1SegSize As Int32 = readBytes(src, 1)(0) * 256 + readBytes(src,
1)(0)
Dim XMPpacketSize As Int32 = APP1SegSize - 29 - 2

ptr += 31 ' skip fields "Lp" and "namespace"

Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

If XMPpacketSize 2 Then
s.Append(encoding.GetString(src, ptr - 2, XMPpacketSize))
End If
Dim r As String = s.ToString ' this will convert it to Unicode (UTF16)
' get rid of the <?xpacket stuff
Dim re As New Regex(".*\?>")
r = re.Replace(r, "").Trim

' get rid of the namespaces in <namespace:nameand </namespace/name>
re = New Regex("(</{0,1})[A-Za-z]*?:")
r = re.Replace(r, "$1")

Return r
End Function

' Functions to help getXMPdata()

<System.Diagnostics.DebuggerStepThrough()Private Function checkBytes(ByVal
src() As Byte, ByVal cf() As Byte) As Boolean
' compare bytes in the source to the given array of bytes
' and advance the pointer
Dim nBytes As Int32 = 0
' if it goes past the end of src, it is not a match
If ptr + nBytes UBound(src) Then
Return False
End If
Dim a As Boolean = True

'Debug.WriteLine("Checkbytes starting at ptr=&" & Hex(ptr) & vbCrLf)
For i As Int32 = 0 To UBound(cf)
'Debug.WriteLine(Hex(src(ptr + i)) & " ")
'b = src(ptr + i) : c = cf(i) ' just to make debugger show them
nBytes += 1
If src(ptr + i) <cf(i) Then
a = False
Exit For
End If
Next
ptr += nBytes
Return a
End Function

<System.Diagnostics.DebuggerStepThrough()Private Function
peekCheckBytes(ByVal src() As Byte, ByVal cf() As Byte) As Boolean
' compare bytes in the source to the given array of bytes
' do not advance the pointer
Dim nBytes As Int32 = UBound(cf) + 1
' if it goes past the end of src, it is not a match
If ptr + nBytes UBound(src) Then
Return False
End If
Dim a As Boolean = True
For i As Int32 = 0 To UBound(cf)
If src(ptr + i) <cf(i) Then
a = False
Exit For
End If
Next
Return a
End Function

<System.Diagnostics.DebuggerStepThrough()Private Function readBytes(ByVal
src() As Byte, ByVal nBytes As Int32) As Byte()
' return an array of bytes from the source
' and advance the pointer
'Debug.WriteLine("Readbytes at ptr=&" & Hex(ptr) & vbCrLf)
If ptr + nBytes UBound(src) Then
Throw New Exception("Attempted read past end of source in readBytes.")
End If
Dim dest(nBytes - 1) As Byte
Array.Copy(src, ptr, dest, 0, nBytes)

ptr += nBytes
Return dest
End Function
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I did that with .NET 1.1, so xpath stuff wasn't available, hence using
Microsoft.Xml.Xquery. Either that or it was so convoluted I went with
xquery. The pointer stuff would have looked neater with C#, I suppose.

Go on then you lot, laugh at my coding :-) But it does work.

HTH

Andrew
Nov 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: K | last post by:
I am absolutly exausted to figure this one out. I require the exif module to work under 5.0.1 but apache (2.0.48) just dies with a 1067 failure, I can't find any usable log info apart from the...
4
by: Jan Schmidt | last post by:
Hi NG, i created my own Picture Gallery, which reads the EXIF Data of each Picture before displaying on website. I'd like to give my users the ability to alter some opened EXIF Values. It would...
1
by: leo | last post by:
hi there how can i get in python EXIF data (particularky the shouuting time) of a jpg? is there a special module around? thanks, leo
9
by: BluDog | last post by:
Hi I was wandering if anyone knew if it was possible to add custom EXIF tags (such as Photographer and Title) to a JPEG image, or to modify the existing tags (such as ImageDescription). There...
0
by: dazandbec | last post by:
Hi, am having trouble extracting the Makernote contained in the EXIF data in a JPEG image from a digital camera. I am able to successfully extract the data contained in the makernote tag,...
4
by: FayeC | last post by:
I have tried to use a php code (found it online) to create a gallery but I am wondering if thereare any other PHP options besides using EXIF. The reason is that the images I am using for the...
3
by: Merlin | last post by:
Hi all Not a pure c# question, but is someone here knowing a good lib in C# to read and write exif and iptc information in image files ? thanks in advance.. -- //\/\\3rL1n_______
14
by: Frank | last post by:
I see that ImageFormat includes exif. But I can't find out if I've System.Drawing.Image.FromStream or something like it can read and/or write that format.
2
by: boarderstu | last post by:
Hi All, I'm looking for some tuts or advice on reading Meta data from files - preferably Exif data, any body got any advice on this? ta
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.