示例#1
0
 public void EndRead()
 {
     if (_exifReader != null)
     {
         _exifReader.Dispose();
         _exifReader = null;
     }
 }
示例#2
0
文件: GPUtil.cs 项目: tinymin/Groupic
        public static String GetExifDate(string path)
        {
            String strDate;

            try
            {
                ExifReader exifData = new ExifReader(path);
                exifData.GetTagValue(ExifTags.DateTimeOriginal, out strDate);
                exifData.Dispose(); // Close file
            }
            catch (ExifLibException ex)
            {
                Console.WriteLine(ex.ToString());
                return(String.Empty);
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.ToString());
                return(String.Empty);
            }

            if (null == strDate || String.Empty == strDate)
            {
                return(String.Empty);
            }

            strDate = strDate.Replace(":", "-");
            return(strDate);
        }
示例#3
0
        private void showCurrentIndexImage()
        {
            if (current_index < 0 || current_index > ifiles.Count - 1)
            {
                return;
            }
            if (pbox.Image != null)
            {
                pbox.Image.Dispose();
            }
            var fn   = ifiles[current_index].fi.FullName;
            var exif = new ExifReader(fn);
            var img  = Image.FromFile(fn);

            UInt16 orientation;

            if (exif.GetTagValue <UInt16>(ExifTags.Orientation, out orientation))
            {
                UInt32 iso;
                if (exif.GetTagValue <UInt32>(ExifTags.ISOSpeed, out iso))
                {
                    label1.Text = "ISO " + iso;
                }
                switch (orientation)
                {
                case 3:
                    img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;

                case 6:
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;

                case 8:
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;
                }
            }

            pbox.Image = img;
            if (img.Width < this.Width && img.Height < this.Height)
            {
                pbox.SizeMode = PictureBoxSizeMode.CenterImage;
            }
            else
            {
                pbox.SizeMode = PictureBoxSizeMode.Zoom;
            }
            lbGrade.Text = new String('★', ifiles[current_index].grade);
            this.Text    = ifiles[current_index].fi.Name + " ## " + (current_index + 1) + " / " + (ifiles.Count + 1);

            exif.Dispose();
        }
示例#4
0
        // взето от https://codereview.stackexchange.com/questions/77453/c-photo-sorter

        public static DateTime getTakenDateTime(string filePath)
        {
            try
            {
                ExifReader reader = new ExifReader(filePath);
                DateTime   datePictureTaken;
                if (reader.GetTagValue <DateTime>(ExifTags.DateTimeDigitized, out datePictureTaken))
                {
                    reader.Dispose();
                    return(datePictureTaken);
                }
                else
                {
                    reader.Dispose();
                    return(new DateTime(1970, 04, 03)); //The FolderPath builder will recognize a date as 0001 for the year as an error and build a path to the error folder!
                }
            }
            catch
            {
                return(File.GetCreationTime(filePath));
            }
        }
        public static DateTime?GetTakenTime(string fullName)
        {
            ExifReader reader = null;

            try
            {
                reader = new ExifReader(fullName);

                DateTime datePictureTaken;

                if (reader.GetTagValue(ExifTags.DateTimeOriginal, out datePictureTaken))
                {
                    Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken));
                    return(datePictureTaken);
                }

                if (reader.GetTagValue(ExifTags.DateTimeDigitized, out datePictureTaken))
                {
                    Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken));
                    return(datePictureTaken);
                }

                if (reader.GetTagValue(ExifTags.DateTime, out datePictureTaken))
                {
                    Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken));
                    return(datePictureTaken);
                }

                if (reader.GetTagValue(ExifTags.GPSDateStamp, out datePictureTaken))
                {
                    Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken));
                    return(datePictureTaken);
                }

                Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken));
                return(datePictureTaken);
            }
            catch (Exception ex)
            {
                // Something didn't work!
                Debug.WriteLine(ex.ToString());
                //                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (reader != null)
                {
                    reader.Dispose();
                }
            }

            return(null);
        }
示例#6
0
        private bool extractData(string fileName, out double lat, out double lng)
        {
            bool       retVal = false;
            ExifReader reader = null;

            lat = lng = 0;
            try
            {
                reader = new ExifReader(fileName);

                // Get the image thumbnail (if present)
                var thumbnailBytes = reader.GetJpegThumbnailBytes();

                if (thumbnailBytes == null)
                {
                    pictureBox1.Image = null;
                }
                else
                {
                    using (var stream = new MemoryStream(thumbnailBytes))
                        pictureBox1.Image = Image.FromStream(stream);
                }

                // To read a single field, use code like this:

                /*
                 * DateTime datePictureTaken;
                 * if (reader.GetTagValue<DateTime>(ExifTags.DateTimeDigitized, out datePictureTaken))
                 * {
                 *  MessageBox.Show(this, string.Format("The picture was taken on {0}", datePictureTaken), "Image information", MessageBoxButtons.OK);
                 * }
                 */
                double[] vals;
                if (reader.GetTagValue <double[]>(ExifTags.GPSLatitude, out vals) == false)
                {
                    return(retVal);
                }
                string stringval;
                if (reader.GetTagValue <string>(ExifTags.GPSLatitudeRef, out stringval) == false)
                {
                    return(retVal);
                }
                double loc;
                loc = decodeLocation(vals);
                if (stringval == "S")
                {
                    loc *= -1;
                }
                lat = loc;

                if (reader.GetTagValue <double[]>(ExifTags.GPSLongitude, out vals) == false)
                {
                    return(retVal);
                }
                if (reader.GetTagValue <string>(ExifTags.GPSLongitudeRef, out stringval) == false)
                {
                    return(retVal);
                }
                loc = decodeLocation(vals);
                if (stringval == "W")
                {
                    loc *= -1;
                }
                lng    = loc;
                retVal = true;
                // Parse through all available fields and generate key-value labels
                //var props = Enum.GetValues(typeof (ExifTags)).Cast<ushort>().Select(tagID =>
                //{
                //    object val;
                //    if (reader.GetTagValue(tagID, out val))
                //        return string.Format("{0}: {1}", Enum.GetName(typeof (ExifTags), tagID), RenderTag(val));

                //    return null;

                //}).Where(x => x != null).ToArray();
            }
            catch (Exception)
            {
                // Something didn't work!
                //MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (reader != null)
                {
                    reader.Dispose();
                }
            }
            return(retVal);
        }
示例#7
0
        public void Parse2(string filename)
        {
            int cameraNameOffset           = 0x32E;
            int distortionCorrectionOffset = 0x340;

            UInt16[]   distortionValues = new UInt16[16];
            string     cameraName;
            string     lensName;
            Distortion distortion;
            Distortion distortion2;
            double     focalLength = 0;
            double     aperture    = 0;

            byte[]        fileBytes = File.ReadAllBytes(filename);
            StringBuilder sb        = new StringBuilder();

            for (int i = 0; i < 32; i++)
            {
                sb.Append(Convert.ToString(fileBytes[distortionCorrectionOffset + i], 16).PadLeft(2, '0'));
            }

            FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            fs.Seek(cameraNameOffset, 0);
            BinaryReader binaryReader = new BinaryReader(fs);

            cameraName = (new string(Util.ReplaceNULWithBlanks(binaryReader.ReadChars(18)))).Trim();
            for (int i = 0; i < 16; i++)
            {
                distortionValues[i] = ReverseBytes(binaryReader.ReadUInt16());
            }

            //fs.Seek(GetLensOffsetFromCameraName((new string(Util.ReplaceNULWithBlanks(cameraName)).Trim())), 0);
            //BinaryReader binaryReaderLensName = new BinaryReader(fs);
            //lensName = binaryReaderLensName.ReadChars(34);
            lensName = offsets[cameraName].GetLensName(fs);

            distortion.n     = ReverseBytes(distortionValues[12]);
            distortion.scale = 1.0 / (1.0 + (ReverseBytes(distortionValues[5]) / 32768.0));
            distortion.a     = distortion.scale * (ReverseBytes(distortionValues[8]) / 32768.0);
            distortion.b     = distortion.scale * (ReverseBytes(distortionValues[4]) / 32768.0);
            distortion.c     = distortion.scale * (ReverseBytes(distortionValues[11]) / 32768.0);

            distortion2.n     = distortionValues[12];
            distortion2.scale = 1.0 / (1.0 + (distortionValues[5] / 32768.0));
            distortion2.a     = distortion2.scale * (distortionValues[8] / 32768.0);
            distortion2.b     = distortion2.scale * (distortionValues[4] / 32768.0);
            distortion2.c     = distortion2.scale * (distortionValues[11] / 32768.0);
            //EM5 vielleicht:0xC20E oder 0x2854 bis 0x2863

            ExifReader reader = null;

            try
            {
                reader = new ExifReader(filename, 0x800);

                //string CameraMaker;
                //string CameraModel;
                //reader.GetTagValue<String>(ExifTags.Make, out CameraMaker);
                //reader.GetTagValue<String>(ExifTags.Model, out CameraModel);
                reader.GetTagValue <Double>(ExifTags.FocalLength, out focalLength);
                reader.GetTagValue <Double>(ExifTags.ApertureValue, out aperture);
            }
            catch (Exception ex)
            {
                // Something didn't work!


                if (reader != null)
                {
                    reader.Dispose();
                }
            }

            //using (FileStream fs2 = new FileStream("test.ab", FileMode.OpenOrCreate))
            //{
            //    using (BinaryWriter w = new BinaryWriter(fs2))
            //    {
            //        for (int i = 0; i < 16; i++)
            //        {
            //            w.Write(distortionValues[i]);
            //        }
            //    }
            //}
            //distortionValues[10]= 1;
            //int checksumResult = Verify_checksums(distortionValues);

            WriteOutputToFile(cameraName, lensName, focalLength.ToString(), aperture.ToString(), distortionValues, distortion, distortion2, filename);
        }
示例#8
0
        private void btnPopulate_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtFileName.Text))
            {
                MessageBox.Show(this, "Please enter a valid filename", "File not found", MessageBoxButtons.OK);
                return;
            }

            ExifReader reader = null;

            try
            {
                reader = new ExifReader(txtFileName.Text);

                // Get the image thumbnail (if present)
                var thumbnailBytes = reader.GetJpegThumbnailBytes();

                if (thumbnailBytes == null)
                {
                    pictureBoxThumbnail.Image = null;
                }
                else
                {
                    using (var stream = new MemoryStream(thumbnailBytes))
                        pictureBoxThumbnail.Image = Image.FromStream(stream);
                }

                // To read a single field, use code like this:

                /*
                 * DateTime datePictureTaken;
                 * if (reader.GetTagValue<DateTime>(ExifTags.DateTimeDigitized, out datePictureTaken))
                 * {
                 *  MessageBox.Show(this, string.Format("The picture was taken on {0}", datePictureTaken), "Image information", MessageBoxButtons.OK);
                 * }
                 */

                // Parse through all available fields
                string props = "";
                foreach (ushort tagID in Enum.GetValues(typeof(ExifTags)))
                {
                    object val;
                    if (reader.GetTagValue(tagID, out val))
                    {
                        // Arrays don't render well without assistance.
                        string renderedTag;
                        if (val is Array)
                        {
                            renderedTag = "";
                            foreach (object item in (Array)val)
                            {
                                renderedTag += item + ",";
                            }
                            renderedTag = renderedTag.Substring(0, renderedTag.Length - 1);
                        }
                        else
                        {
                            renderedTag = val.ToString();
                        }

                        props += string.Format("{0}:{1}\r\n", Enum.GetName(typeof(ExifTags), tagID), renderedTag);
                    }
                }

                // Remove the last carriage return
                props = props.Substring(0, props.Length - 2);

                txtFields.Text = props;
            }
            catch (Exception ex)
            {
                // Something didn't work!
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (reader != null)
                {
                    reader.Dispose();
                }
            }
        }
示例#9
0
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var      Photo       = _db.Photos.OrderByDescending(p => p.Id).FirstOrDefault();
                string   oldFileName = Path.GetFileName(file.FileName);
                string[] name        = oldFileName.Split('.');
                int      id          = (Photo == null) ? 0 : Photo.Id;
                string   newFileName = (Convert.ToInt16(id) + 1).ToString() + "." + name[1];
                string   tempPath    = Path.Combine(Server.MapPath("~/Images/uploads/temp"), newFileName);
                string   path        = Path.Combine(Server.MapPath("~/Images/uploads"), newFileName);
                file.SaveAs(tempPath);

                try
                {
                    using (ExifReader reader = new ExifReader(tempPath))
                    {
                        Double[] GpsLongArray;
                        Double[] GpsLatArray;
                        UInt16   Orientation;
                        Double   GpsLongDouble;
                        Double   GpsLatDouble;
                        DateTime datePictureTaken;

                        if (reader.GetTagValue <Double[]>(ExifTags.GPSLongitude, out GpsLongArray) &&
                            reader.GetTagValue <Double[]>(ExifTags.GPSLatitude, out GpsLatArray) &&
                            reader.GetTagValue <DateTime>(ExifTags.DateTimeDigitized, out datePictureTaken) &&
                            reader.GetTagValue <UInt16>(ExifTags.Orientation, out Orientation))
                        {
                            GpsLongDouble = GpsLongArray[0] + GpsLongArray[1] / 60 + GpsLongArray[2] / 3600;
                            GpsLatDouble  = GpsLatArray[0] + GpsLatArray[1] / 60 + GpsLatArray[2] / 3600;

                            var record = from r in _db.Photos
                                         where r.Longitude == GpsLongDouble &&
                                         r.Latitude == GpsLatDouble &&
                                         r.PictureTaken == datePictureTaken
                                         orderby r.Id
                                         select r.Id;

                            if (record.Count() == 0)
                            {
                                _db.Photos.Add(new Photo {
                                    UserId       = WebSecurity.CurrentUserId,
                                    imgName      = newFileName,
                                    imgPath      = path,
                                    Longitude    = GpsLongDouble,
                                    Latitude     = GpsLatDouble,
                                    Orientation  = Orientation,
                                    PictureTaken = datePictureTaken
                                });

                                reader.Dispose();
                                System.IO.File.Move(tempPath, path);
                                Photo.Rotate(path);
                                _db.SaveChanges();
                                ViewBag.Message = "Photo uploaded successfully";
                            }
                            else
                            {
                                reader.Dispose();
                                System.IO.File.Delete(tempPath);
                                ViewBag.Message = "Photo already exists!";
                            }
                        }
                        else
                        {
                            reader.Dispose();
                            System.IO.File.Delete(tempPath);
                            ViewBag.Message = "The Photo does not contain GPS info!";
                        }
                    }
                }
                catch (Exception ex) {
                    System.IO.File.Delete(tempPath);
                    ViewBag.Message = ex.Message.ToString();
                }
            }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }
            return(RedirectToAction("Index", "Photo", new { error = ViewBag.Message }));
        }