Пример #1
0
        private GpsCoordinate AsGps(Array array, string gpsDir)
        {
            if (array.Length != 3)
            {
                return(null);
            }

            GpsCoordinate gps = new GpsCoordinate();

            if (array.GetValue(0) is Rational <uint> )
            {
                gps.Degrees = (Rational <uint>)array.GetValue(0);
            }
            else
            {
                gps.SetDegrees(Convert.ToDecimal(array.GetValue(0)));
            }

            if (array.GetValue(1) is Rational <uint> )
            {
                gps.Minutes = (Rational <uint>)array.GetValue(1);
            }
            else
            {
                gps.SetMinutes(Convert.ToDecimal(array.GetValue(1)));
            }

            if (array.GetValue(2) is Rational <uint> )
            {
                gps.Seconds = (Rational <uint>)array.GetValue(2);
            }
            else
            {
                gps.SetSeconds(Convert.ToDecimal(array.GetValue(2)));
            }

            try
            {
                gps.Direction = gpsDir;
            }
            catch { }

            return(gps);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// References:
        /// http://www.media.mit.edu/pia/Research/deepview/exif.html
        /// http://en.wikipedia.org/wiki/APEX_system
        /// http://en.wikipedia.org/wiki/Exposure_value
        /// </remarks>
        protected string FormatValue()
        {
            object rawValue = this.Value;

            switch (this.Tag)
            {
            case ExifTag.ISOSpeed:
            {
                if (rawValue is Array)
                {
                    Array array = (Array)rawValue;
                    if (array.Length < 1 || !(array.GetValue(0) is IConvertible))
                    {
                        goto default;
                    }
                    rawValue = array.GetValue(0);
                }

                if (!(rawValue is IConvertible))
                {
                    goto default;
                }

                return(String.Format("ISO-{0:###0}", Convert.ToDecimal(rawValue)));
            }

            case ExifTag.Aperture:
            case ExifTag.MaxAperture:
            {
                // The actual aperture value of lens when the image was taken.
                // To convert this value to ordinary F-number(F-stop),
                // calculate this value's power of root 2 (=1.4142).
                // For example, if value is '5', F-number is 1.4142^5 = F5.6.
                double fStop = Math.Pow(2.0, Convert.ToDouble(rawValue) / 2.0);
                return(String.Format("f/{0:#0.0}", fStop));
            }

            case ExifTag.FNumber:
            {
                // The actual F-number (F-stop) of lens when the image was taken.
                return(String.Format("f/{0:#0.0}", Convert.ToDecimal(rawValue)));
            }

            case ExifTag.FocalLength:
            case ExifTag.FocalLengthIn35mmFilm:
            {
                return(String.Format("{0:#0.#} mm", Convert.ToDecimal(rawValue)));
            }

            case ExifTag.ShutterSpeed:
            {
                if (!(rawValue is Rational <int>))
                {
                    goto default;
                }

                // To convert this value to ordinary 'Shutter Speed';
                // calculate this value's power of 2, then reciprocal.
                // For example, if value is '4', shutter speed is 1/(2^4)=1/16 second.
                Rational <int> shutter = (Rational <int>)rawValue;

                if (shutter.Numerator > 0)
                {
                    double speed = Math.Pow(2.0, Convert.ToDouble(shutter));
                    return(String.Format("1/{0:####0} sec", speed));
                }
                else
                {
                    double speed = Math.Pow(2.0, -Convert.ToDouble(shutter));
                    return(String.Format("{0:####0.##} sec", speed));
                }
            }

            case ExifTag.ExposureTime:
            {
                if (!(rawValue is Rational <uint>))
                {
                    goto default;
                }

                // Exposure time (reciprocal of shutter speed). Unit is second.
                Rational <uint> exposure = (Rational <uint>)rawValue;

                if (exposure.Numerator < exposure.Denominator)
                {
                    exposure.Reduce();
                    return(String.Format("{0} sec", exposure));
                }
                else
                {
                    return(String.Format("{0:####0.##} sec", Convert.ToDecimal(rawValue)));
                }
            }

            case ExifTag.XResolution:
            case ExifTag.YResolution:
            case ExifTag.ThumbnailXResolution:
            case ExifTag.ThumbnailYResolution:
            case ExifTag.FocalPlaneXResolution:
            case ExifTag.FocalPlaneYResolution:
            {
                return(String.Format("{0:###0} dpi", Convert.ToDecimal(rawValue)));
            }

            case ExifTag.ImageHeight:
            case ExifTag.ImageWidth:
            case ExifTag.CompressedImageHeight:
            case ExifTag.CompressedImageWidth:
            case ExifTag.ThumbnailHeight:
            case ExifTag.ThumbnailWidth:
            {
                return(String.Format("{0:###0} pixels", Convert.ToDecimal(rawValue)));
            }

            case ExifTag.SubjectDistance:
            {
                return(String.Format("{0:###0.#} m", Convert.ToDecimal(rawValue)));
            }

            case ExifTag.ExposureBias:
            case ExifTag.Brightness:
            {
                string val;
                if (rawValue is Rational <int> )
                {
                    val = ((Rational <int>)rawValue).Numerator != 0 ? rawValue.ToString() : "0";
                }
                else
                {
                    val = Convert.ToDecimal(rawValue).ToString();
                }

                return(String.Format("{0} EV", val));
            }

            case ExifTag.CompressedBitsPerPixel:
            {
                return(String.Format("{0:###0.0} bits", Convert.ToDecimal(rawValue)));
            }

            case ExifTag.DigitalZoomRatio:
            {
                return(Convert.ToString(rawValue).Replace('/', ':'));
            }

            case ExifTag.GpsLatitude:
            case ExifTag.GpsLongitude:
            case ExifTag.GpsDestLatitude:
            case ExifTag.GpsDestLongitude:
            {
                if (!(rawValue is Array))
                {
                    goto default;
                }

                Array array = (Array)rawValue;
                if (array.Length < 1)
                {
                    return(String.Empty);
                }
                else if (array.Length != 3)
                {
                    goto default;
                }

                // attempt to use the GPSCoordinate XMP formatting guidelines
                GpsCoordinate gps = new GpsCoordinate();

                if (array.GetValue(0) is Rational <uint> )
                {
                    gps.Degrees = (Rational <uint>)array.GetValue(0);
                }
                else
                {
                    gps.SetDegrees(Convert.ToDecimal(array.GetValue(0)));
                }
                if (array.GetValue(1) is Rational <uint> )
                {
                    gps.Minutes = (Rational <uint>)array.GetValue(1);
                }
                else
                {
                    gps.SetMinutes(Convert.ToDecimal(array.GetValue(1)));
                }
                if (array.GetValue(2) is Rational <uint> )
                {
                    gps.Seconds = (Rational <uint>)array.GetValue(2);
                }
                else
                {
                    gps.SetSeconds(Convert.ToDecimal(array.GetValue(2)));
                }

                return(gps.ToString());
            }

            case ExifTag.GpsTimeStamp:
            {
                Array array = (Array)rawValue;
                if (array.Length < 1)
                {
                    return(String.Empty);
                }

                string[] time = new string[array.Length];
                for (int i = 0; i < array.Length; i++)
                {
                    time[i] = Convert.ToDecimal(array.GetValue(i)).ToString();
                }
                return(String.Join(":", time));
            }

            default:
            {
                if (rawValue is Enum)
                {
                    string description = Utility.GetDescription((Enum)rawValue);
                    if (!String.IsNullOrEmpty(description))
                    {
                        return(description);
                    }
                }
                else if (rawValue is Array)
                {
                    Array array = (Array)rawValue;
                    if (array.Length < 1)
                    {
                        return(String.Empty);
                    }

                    Type type = array.GetValue(0).GetType();
                    if (!type.IsPrimitive || type == typeof(char) || type == typeof(float) || type == typeof(double))
                    {
                        return(Convert.ToString(rawValue));
                    }

                    //const int ElemsPerRow = 40;
                    int           charSize = 2 * System.Runtime.InteropServices.Marshal.SizeOf(type);
                    string        format   = "{0:X" + (charSize) + "}";
                    StringBuilder builder  = new StringBuilder(((charSize + 1) * array.Length) /*+(2*array.Length/ElemsPerRow)*/);
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (i > 0)
                        {
                            //if ((i+1)%ElemsPerRow == 0)
                            //{
                            //    builder.AppendLine();
                            //}
                            //else
                            {
                                builder.Append(" ");
                            }
                        }

                        builder.AppendFormat(format, array.GetValue(i));
                    }
                    return(builder.ToString());
                }

                return(Convert.ToString(rawValue));
            }
            }
        }