Browse Source

Added attribute that can be used to get a description for the value of an exif tag.

Former-commit-id: 3bca9a911b0189867911ef22765009909a9bbe00
Former-commit-id: 91add02607f34749757840aa946b1fe987bd234f
Former-commit-id: cb69236b11426f8c1abd1928c22d0922c1554baa
af/merge-core
dirk 10 years ago
parent
commit
b6e091794b
  1. 3
      src/ImageProcessorCore/Profiles/Exif/ExifTag.cs
  2. 49
      src/ImageProcessorCore/Profiles/Exif/ExifTagDescriptionAttribute.cs
  3. 4
      src/ImageProcessorCore/Profiles/Exif/ExifValue.cs
  4. 38
      tests/ImageProcessorCore.Tests/Profiles/Exif/ExifTagDescriptionAttributeTests.cs

3
src/ImageProcessorCore/Profiles/Exif/ExifTag.cs

@ -159,6 +159,9 @@ namespace ImageProcessorCore
/// <summary> /// <summary>
/// ResolutionUnit /// ResolutionUnit
/// </summary> /// </summary>
[ExifTagDescription((ushort)1, "None")]
[ExifTagDescription((ushort)2, "Inches")]
[ExifTagDescription((ushort)3, "Cm")]
ResolutionUnit = 0x0128, ResolutionUnit = 0x0128,
/// <summary> /// <summary>

49
src/ImageProcessorCore/Profiles/Exif/ExifTagDescriptionAttribute.cs

@ -0,0 +1,49 @@
// <copyright file="ExifTag.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessorCore
{
using System;
using System.Linq;
using System.Reflection;
/// <summary>
/// Class that provides a description for an ExifTag value.
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
internal sealed class ExifTagDescriptionAttribute : Attribute
{
private object value;
private string description;
///<summary>
/// Initializes a new instance of the ExifTagDescriptionAttribute class.
///</summary>
/// <param name="value">The value of the exif tag.</param>
/// <param name="description">The description for the value of the exif tag.</param>
public ExifTagDescriptionAttribute(object value, string description)
{
this.value = value;
this.description = description;
}
public static string GetDescription(ExifTag tag, object value)
{
FieldInfo field = tag.GetType().GetTypeInfo().GetDeclaredField(tag.ToString());
if (field == null)
return null;
foreach (CustomAttributeData customAttribute in field.CustomAttributes)
{
object attributeValue = customAttribute.ConstructorArguments[0].Value;
if (Equals(attributeValue, value))
return (string)customAttribute.ConstructorArguments[1].Value;
}
return null;
}
}
}

4
src/ImageProcessorCore/Profiles/Exif/ExifValue.cs

@ -561,6 +561,10 @@ namespace ImageProcessorCore
private string ToString(object value) private string ToString(object value)
{ {
string description = ExifTagDescriptionAttribute.GetDescription(Tag, value);
if (description != null)
return description;
switch (DataType) switch (DataType)
{ {
case ExifDataType.Ascii: case ExifDataType.Ascii:

38
tests/ImageProcessorCore.Tests/Profiles/Exif/ExifTagDescriptionAttributeTests.cs

@ -0,0 +1,38 @@
// <copyright file="ExifProfileTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessorCore.Tests
{
using Xunit;
public class ExifDescriptionAttributeTests
{
[Fact]
public void Test_ExifTag()
{
var exifProfile = new ExifProfile();
exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)1);
ExifValue value = exifProfile.GetValue(ExifTag.ResolutionUnit);
Assert.Equal("None", value.ToString());
exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)2);
value = exifProfile.GetValue(ExifTag.ResolutionUnit);
Assert.Equal("Inches", value.ToString());
exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)3);
value = exifProfile.GetValue(ExifTag.ResolutionUnit);
Assert.Equal("Cm", value.ToString());
exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)4);
value = exifProfile.GetValue(ExifTag.ResolutionUnit);
Assert.Equal("4", value.ToString());
exifProfile.SetValue(ExifTag.ImageWidth, 123);
value = exifProfile.GetValue(ExifTag.ImageWidth);
Assert.Equal("123", value.ToString());
}
}
}
Loading…
Cancel
Save