Rambler's Top100
Главная
Новости
Статьи
Форумы
Книги
Коды
Сообщество
Блоги
О нас
 

Логин

Email:
  Пароль:

Войти
Зарегистрироваться
Забыл пароль

Поиск

 Искать :
 
Вперед

Книги по теме

Искать:
в:
Порядок:

Исходник

Автор:

gaech

 
Название:

Чтение EXIF-метаданных из изображений

Дата: 23 October 2005
Описание: Этот исходник может оказаться весьма полезен тем, кто хочет написать свою web-галлерею или view`ер. Все предельно просто! EXIFReader reader = new EXIFReader("ASP.JPG"); Console.WriteLine(reader[PropertyTagId.DateTime]); Буду рад услышать любые замечания и предложения. Надеюсь, что мой код кому-нибудь окажется полезен. 
  Разместить ссылку на этот исходник в форуме вы можете вставив в текст сообщения следующую строку: [CODEPOST ID=167]Чтение EXIF-метаданных из изображений[/CODEPOST]
Оценка: Проголосовало 12 посетителей, средняя оценка 4.75
Оценить:
  1 //PropertyTagId.cs
  2 //Id всех метаполей
  3 //gaech(с)2005
  4 using System;
  5 
  6 namespace GA.EXIF.Property
  7 {
  8 	public enum PropertyTagId
  9 	{
 10 		GpsVer = 0x0000,
 11 		GpsLatitudeRef = 0x0001,
 12 		GpsLatitude = 0x0002,
 13 		GpsLongitudeRef = 0x0003,
 14 		GpsLongitude = 0x0004,
 15 		GpsAltitudeRef = 0x0005,
 16 		GpsAltitude = 0x0006, 
 17 		GpsGpsTime = 0x0007, 
 18 		GpsGpsSatellites = 0x0008, 
 19 		GpsGpsStatus = 0x0009, 
 20 		GpsGpsMeasureMode = 0x000A,
 21 		GpsGpsDop = 0x000B,
 22 		GpsSpeedRef = 0x000C, 
 23 		GpsSpeed = 0x000D, 
 24 		GpsTrackRef = 0x000E, 
 25 		GpsTrack = 0x000F, 
 26 		GpsImgDirRef = 0x0010, 
 27 		GpsImgDir = 0x0011, 
 28 		GpsMapDatum = 0x0012, 
 29 		GpsDestLatRef = 0x0013, 
 30 		GpsDestLat = 0x0014, 
 31 		GpsDestLongRef = 0x0015, 
 32 		GpsDestLong = 0x0016, 
 33 		GpsDestBearRef = 0x0017, 
 34 		GpsDestBear = 0x0018, 
 35 		GpsDestDistRef = 0x0019, 
 36 		GpsDestDist = 0x001A, 
 37 		NewSubfileType = 0x00FE, 
 38 		SubfileType = 0x00FF, 
 39 		ImageWidth = 0x0100, 
 40 		ImageHeight = 0x0101, 
 41 		BitsPerSample = 0x0102, 
 42 		Compression = 0x0103, 
 43 		PhotometricInterp = 0x0106, 
 44 		ThreshHolding = 0x0107, 
 45 		CellWidth = 0x0108, 
 46 		CellHeight = 0x0109, 
 47 		FillOrder = 0x010A,
 48 		DocumentName = 0x010D,
 49 		ImageDescription = 0x010E,
 50 		EquipMake = 0x010F,
 51 		EquipModel = 0x0110, 
 52 		StripOffsets = 0x0111,
 53 		Orientation = 0x0112,
 54 		SamplesPerPixel = 0x0115,
 55 		RowsPerStrip = 0x0116,
 56 		StripBytesCount = 0x0117,
 57 		MinSampleValue = 0x0118,
 58 		MaxSampleValue = 0x0119,
 59 		XResolution = 0x011A,
 60 		YResolution = 0x011B,
 61 		PlanarConfig = 0x011C, 
 62 		PageName = 0x011D,
 63 		XPosition = 0x011E, 
 64 		YPosition = 0x011F,
 65 		FreeOffset = 0x0120, 
 66 		FreeByteCounts = 0x0121,
 67 		GrayResponseUnit = 0x0122,
 68 		GrayResponseCurve = 0x0123, 
 69 		T4Option = 0x0124,
 70 		T6Option = 0x0125,
 71 		ResolutionUnit = 0x0128,
 72 		PageNumber = 0x0129,
 73 		TransferFunction = 0x012D,
 74 		SoftwareUsed = 0x0131,
 75 		DateTime = 0x0132,
 76 		Artist = 0x013B,
 77 		HostComputer = 0x013C,
 78 		Predictor = 0x013D,
 79 		WhitePoint = 0x013E,
 80 		PrimaryChromaticities = 0x013F,
 81 		ColorMap = 0x0140,
 82 		HalftoneHints = 0x0141,
 83 		TileWidth = 0x0142,
 84 		TileLength = 0x0143,
 85 		TileOffset = 0x0144,
 86 		TileByteCounts = 0x0145,
 87 		InkSet = 0x014C,
 88 		InkNames = 0x014D,
 89 		NumberOfInks = 0x014E,
 90 		DotRange = 0x0150,
 91 		TargetPrinter = 0x0151,
 92 		ExtraSamples = 0x0152,
 93 		SampleFormat = 0x0153,
 94 		SMinSampleValue = 0x0154,
 95 		SMaxSampleValue = 0x0155,
 96 		TransferRange = 0x0156,
 97 		JPEGProc = 0x0200,
 98 		JPEGInterFormat = 0x0201,
 99 		JPEGInterLength = 0x0202,
100 		JPEGRestartInterval = 0x0203,
101 		JPEGLosslessPredictors = 0x0205,
102 		JPEGPointTransforms = 0x0206,
103 		JPEGQTables = 0x0207,
104 		JPEGDCTables = 0x0208,
105 		JPEGACTables = 0x0209,
106 		YCbCrCoefficients = 0x0211,
107 		YCbCrSubsampling = 0x0212,
108 		YCbCrPositioning = 0x0213,
109 		REFBlackWhite = 0x0214,
110 		Gamma = 0x0301,
111 		ICCProfileDescriptor = 0x0302,
112 		SRGBRenderingIntent = 0x0303,
113 		ImageTitle = 0x0320,
114 		ResolutionXUnit = 0x5001,
115 		ResolutionYUnit = 0x5002,
116 		ResolutionXLengthUnit = 0x5003,
117 		ResolutionYLengthUnit = 0x5004,
118 		PrintFlags = 0x5005,
119 		PrintFlagsVersion = 0x5006,
120 		PrintFlagsCrop = 0x5007,
121 		PrintFlagsBleedWidth = 0x5008,
122 		PrintFlagsBleedWidthScale = 0x5009,
123 		HalftoneLPI = 0x500A,
124 		HalftoneLPIUnit = 0x500B,
125 		HalftoneDegree = 0x500C,
126 		HalftoneShape = 0x500D,
127 		HalftoneMisc = 0x500E,
128 		HalftoneScreen = 0x500F,
129 		JPEGQuality = 0x5010,
130 		GridSize = 0x5011,
131 		ThumbnailFormat = 0x5012,
132 		ThumbnailWidth = 0x5013,
133 		ThumbnailHeight = 0x5014, 
134 		ThumbnailColorDepth = 0x5015,
135 		ThumbnailPlanes = 0x5016,
136 		ThumbnailRawBytes = 0x5017,
137 		ThumbnailSize = 0x5018,
138 		ThumbnailCompressedSize = 0x5019,
139 		ColorTransferFunction = 0x501A,
140 		ThumbnailData = 0x501B,
141 		ThumbnailImageWidth = 0x5020,
142 		ThumbnailImageHeight = 0x5021, 
143 		ThumbnailBitsPerSample = 0x5022,
144 		ThumbnailCompression = 0x5023,
145 		ThumbnailPhotometricInterp = 0x5024,
146 		ThumbnailImageDescription = 0x5025,
147 		ThumbnailEquipMake = 0x5026,
148 		ThumbnailEquipModel = 0x5027, 
149 		ThumbnailStripOffsets = 0x5028,
150 		ThumbnailOrientation = 0x5029,
151 		ThumbnailSamplesPerPixel = 0x502A,
152 		ThumbnailRowsPerStrip = 0x502B,
153 		ThumbnailStripBytesCount = 0x502C,
154 		ThumbnailResolutionX = 0x502D,
155 		ThumbnailResolutionY = 0x502E,
156 		ThumbnailPlanarConfig = 0x502F,
157 		ThumbnailResolutionUnit = 0x5030,
158 		ThumbnailTransferFunction = 0x5031,
159 		ThumbnailSoftwareUsed = 0x5032,
160 		ThumbnailDateTime = 0x5033,
161 		ThumbnailArtist = 0x5034,
162 		ThumbnailWhitePoint = 0x5035,
163 		ThumbnailPrimaryChromaticities = 0x5036,
164 		ThumbnailYCbCrCoefficients = 0x5037,
165 		ThumbnailYCbCrSubsampling = 0x5038,
166 		ThumbnailYCbCrPositioning = 0x5039,
167 		ThumbnailRefBlackWhite = 0x503A,
168 		ThumbnailCopyRight = 0x503B,
169 		LuminanceTable = 0x5090,
170 		ChrominanceTable = 0x5091,
171 		FrameDelay = 0x5100,
172 		LoopCount = 0x5101,
173 		GlobalPalette = 0x5102,
174 		IndexBackground = 0x5103,
175 		IndexTransparent = 0x5104, 
176 		PixelUnit = 0x5110,
177 		PixelPerUnitX = 0x5111,
178 		PixelPerUnitY = 0x5112,
179 		PaletteHistogram = 0x5113,
180 		Copyright = 0x8298,
181 		ExifExposureTime = 0x829A,
182 		ExifFNumber = 0x829D,
183 		ExifIFD = 0x8769,
184 		ICCProfile = 0x8773,
185 		ExifExposureProg = 0x8822,
186 		ExifSpectralSense = 0x8824, 
187 		GpsIFD = 0x8825,
188 		ExifISOSpeed = 0x8827,
189 		ExifOECF = 0x8828,
190 		ExifVer = 0x9000,
191 		ExifDTOrig = 0x9003,
192 		ExifDTDigitized = 0x9004,
193 		ExifCompConfig = 0x9101,
194 		ExifCompBPP = 0x9102,
195 		ExifShutterSpeed = 0x9201,
196 		ExifAperture = 0x9202,
197 		ExifBrightness = 0x9203,
198 		ExifExposureBias = 0x9204,
199 		ExifMaxAperture = 0x9205,
200 		ExifSubjectDist = 0x9206,
201 		ExifMeteringMode = 0x9207, 
202 		ExifLightSource = 0x9208,
203 		ExifFlash = 0x9209,
204 		ExifFocalLength = 0x920A,
205 		ExifMakerNote = 0x927C,
206 		ExifUserComment = 0x9286,
207 		ExifDTSubsec = 0x9290,
208 		ExifDTOrigSS = 0x9291,
209 		ExifDTDigSS = 0x9292,
210 		ExifFPXVer = 0xA000,
211 		ExifColorSpace = 0xA001,
212 		ExifPixXDim = 0xA002,
213 		ExifPixYDim = 0xA003,
214 		ExifRelatedWav = 0xA004,
215 		ExifInterop = 0xA005,
216 		ExifFlashEnergy = 0xA20B,
217 		ExifSpatialFR = 0xA20C,
218 		ExifFocalXRes = 0xA20E,
219 		ExifFocalYRes = 0xA20F,
220 		ExifFocalResUnit = 0xA210,
221 		ExifSubjectLoc = 0xA214,
222 		ExifExposureIndex = 0xA215,
223 		ExifSensingMethod = 0xA217,
224 		ExifFileSource = 0xA300,
225 		ExifSceneType = 0xA301,
226 		ExifCfaPattern = 0xA302 
227 	}
228 }
229 //PropertyTagType.cs
230 //Id типов метаполей
231 //gaech(с)2005
232 using System;
233 
234 namespace GA.EXIF.Property
235 {
236 	public enum PropertyTagType
237 	{
238 		PixelFormat4bppIndexed = 0,
239 		Byte = 1,
240 		ASCII = 2,
241 		Short = 3,
242 		Long = 4,
243 		Rational = 5,
244 		Undefined = 7,
245 		SLONG = 9,
246 		SRational = 10
247 	}
248 }
249 //Fraction.cs
250 //Специальный класс, который представляет
251 //дробь в виде числителя и знаменателя.
252 //gaech(с)2005
253 using System;
254 
255 namespace GA.EXIF.Property
256 {
257 	public class Fraction
258 	{
259 		#region Class constructor
260 
261 		public Fraction(int numerator, int denumerator)
262 		{
263 			Numerator = numerator;
264 			Denumerator = denumerator;
265 		}
266 
267 		public Fraction(uint numerator, uint denumerator)
268 		{
269 			Numerator = Convert.ToInt32(numerator);
270 			Denumerator = Convert.ToInt32(denumerator);
271 		}
272 
273 		public Fraction(int numerator)
274 		{
275 			Numerator = numerator;
276 			Denumerator = 1;
277 		}
278 
279 		#endregion
280 
281 		#region Numerator
282 		
283 		private int numerator;
284 		public int Numerator
285 		{
286 			get
287 			{
288 				return numerator;
289 			}
290 			set
291 			{
292 				numerator = value;
293 			}
294 		}
295 
296 		#endregion
297 
298 		#region Denumerator
299 		
300 		private int denumerator;
301 		public int Denumerator
302 		{
303 			get
304 			{
305 				return denumerator;
306 			}
307 			set
308 			{
309 				denumerator = value;
310 			}
311 		}
312 
313 		#endregion
314 
315 		#region ToString override
316 
317 		public override string ToString()
318 		{
319 			if(Denumerator == 1)
320 				return String.Format("{0}",Numerator);
321 			else
322 				return String.Format("{0}/{1}",Numerator,Denumerator);
323 		}
324 
325 
326 		#endregion
327 
328 	}
329 }
330 //PropertyTagValue.cs.cs
331 //Класс содержит один статический метод,
332 //который возвращает значение метаполя.
333 //gaech(с)2005
334 using System;
335 using System.Drawing.Imaging;
336 using System.Text;
337 using GA.EXIF.Property;
338 
339 namespace GA.EXIF.Property
340 {
341 	public class PropertyTagValue
342 	{
343 		public static object GetValueObject(PropertyItem property)
344 		{
345 			if(property == null)
346 				return null;
347 			switch ((PropertyTagType)property.Type)
348 			{
349 				//ASCII
350 				case PropertyTagType.ASCII :
351 					ASCIIEncoding encoding = new ASCIIEncoding();
352 					return encoding.GetString(property.Value,0,property.Len - 1);
353 				//BYTE
354 				case PropertyTagType.Byte :
355 					if(property.Len == 1)
356 						return property.Value[0];
357 					else
358 						return property.Value;
359 				//LONG
360 				case PropertyTagType.Long :
361 					uint[] resultLong = new uint[property.Len / 4];
362 					for (int i=0; i<resultLong.Length; i++)
363 						resultLong[i] = BitConverter.ToUInt32(property.Value, i*4);
364 					if(resultLong.Length == 1) 
365 						return resultLong[0]; 
366 					else
367 						return resultLong;
368 				//SHORT
369 				case PropertyTagType.Short :
370 					ushort[] resultShort = new ushort[property.Len / 2];
371 					for (int i=0; i<resultShort.Length; i++)
372 						resultShort[i] = BitConverter.ToUInt16(property.Value, i*2);
373 					if(resultShort.Length == 1) 
374 						return resultShort[0]; 
375 					else
376 						return resultShort;
377 				//SLONG
378 				case PropertyTagType.SLONG :
379 					int[] resultSLong = new int[property.Len / 4];
380 					for (int i=0; i<resultSLong.Length; i++)
381 						resultSLong[i] = BitConverter.ToInt32(property.Value, i*4);
382 					if(resultSLong.Length == 1) 
383 						return resultSLong[0]; 
384 					else
385 						return resultSLong;
386 				//RATIONAL
387 				case PropertyTagType.Rational :
388 					Fraction[] resultRational = new Fraction[property.Len / 8];
389 					uint uNumerator;
390 					uint uDenumerator;
391 					for (int i=0; i<resultRational.Length; i++)
392 					{
393 						uNumerator = BitConverter.ToUInt32(property.Value, i*8);
394 						uDenumerator = BitConverter.ToUInt32(property.Value, i*8 + 4);
395 						resultRational[i] = new Fraction(uNumerator,uDenumerator);
396 					}
397 					if(resultRational.Length == 1) 
398 						return resultRational[0]; 
399 					else
400 						return resultRational;
401 				//SRATIONAL
402 				case PropertyTagType.SRational :
403 					Fraction[] resultSRational = new Fraction[property.Len / 8];
404 					int sNumerator;
405 					int sDenumerator;
406 					for (int i=0; i<resultSRational.Length; i++)
407 					{
408 						sNumerator = BitConverter.ToInt32(property.Value, i*8);
409 						sDenumerator = BitConverter.ToInt32(property.Value, i*8 + 4);
410 						resultSRational[i] = new Fraction(sNumerator,sDenumerator);
411 					}
412 					if(resultSRational.Length == 1) 
413 						return resultSRational[0]; 
414 					else
415 						return resultSRational;
416 				//UNDEFINE
417 				default:
418 					if(property.Len == 1)
419 						return property.Value[0];
420 					else
421 						return property.Value;
422 			}
423 		}
424 	}
425 }
426 //EXIFReader.cs
427 //Класс для чтения метаданных.
428 //gaech(с)2005
429 using System;
430 using System.Drawing;
431 using System.Drawing.Imaging;
432 using System.IO;
433 
434 using GA.EXIF.Property;
435 
436 namespace GA.EXIF
437 {
438 	public class EXIFReader
439 	{
440 
441 		#region EXIFReader constructors
442 		
443 		public EXIFReader(Image image)
444 		{
445 			this.image = image;
446 		}
447 
448 		public EXIFReader(string path)
449 		{
450 			this.image = Image.FromFile(path);
451 		}
452 
453 		public EXIFReader(Stream stream)
454 		{
455 			this.image = Image.FromStream(stream);
456 		}
457 
458 		#endregion
459 
460 		#region Image
461 
462 		private Image image;
463 
464 		#endregion
465 
466 		#region EXIF property indexers
467 
468 		public object this[int Id]
469 		{
470 			get
471 			{
472 				try
473 				{
474 					PropertyItem property = image.GetPropertyItem(Id);
475 					return PropertyTagValue.GetValueObject(property);
476 				}
477 				catch
478 				{
479 					return null;
480 				}
481 			}
482 		}
483 
484 		public object this[PropertyTagId TagId]
485 		{
486 			get
487 			{
488 				try
489 				{
490 					PropertyItem property = image.GetPropertyItem((int)TagId);
491 					return PropertyTagValue.GetValueObject(property);
492 				}
493 				catch
494 				{
495 					return null;
496 				}
497 			}
498 		}
499 
500 		#endregion
501 	}
502 }
503 
Вернуться к списку исходников в категории Немного оффтопа
 
Наш Киев

Apartments for Rent

Rambler's Top100
Рейтинг@Mail.ru
Идея: Dimon aka Manowar Программирование: Dimon aka Manowar Дизайн: Dan Lebedev
Хостинг от компании Parking.ru
Карта сайта