protected void GetBitmapMetadata(PhotoDetail photo) { using (FileStream fs = new FileStream(photo.FQPath, FileMode.Open)) { JpegBitmapDecoder decoder = new JpegBitmapDecoder(fs, BitmapCreateOptions.None, BitmapCacheOption.None); BitmapMetadata metadata = decoder.Frames[0].Metadata as BitmapMetadata; string keywords = ""; string dateTaken = ""; if (null != metadata) { keywords = metadata.Keywords.Aggregate((old, val) => old + ";" + val); dateTaken = metadata.DateTaken; } if (getTags) { photo.Tags = keywords; } if (getDateTaken && (!string.IsNullOrEmpty(dateTaken))) { photo.DateTaken = DateTime.Parse(dateTaken); } fs.Close(); } }
public static void GetClosestLocationData(PhotoDetail photo) { // Build URL to request reverse geocoding lookup from google maps String latlng = String.Format("{0},{1}", photo.Latitude, photo.Longitude); String requestURL = String.Format(ConfigurationManager.AppSettings["GPSReverseGeocodingLookupURL"], latlng); // Make the request using (HttpClient request = new HttpClient()) { using (var response = request.GetAsync(requestURL).Result) { if (response.IsSuccessStatusCode) { using (var responseContent = response.Content) { String responseString = responseContent.ReadAsStringAsync().Result; Location resultLocation = JsonConvert.DeserializeObject <Location>(responseString); if (resultLocation.status != "OK" && resultLocation.status != "ZERO_RESULTS") { throw new Exception("Reverse geocoding request rejected with status: " + resultLocation.status); } photo.Location = resultLocation.GetLocation(); } } else // Reverse geocoding request failed { throw new Exception("Reverse geocoding request failed with status code: " + response.StatusCode); } } } }
public void GetPhotoMetadata(PhotoDetail photo) { if (getGPSCoordinates) { GetGPSCoordinatesFromPhoto(photo); } if (getTags || getDateTaken) { GetBitmapMetadata(photo); } }
public static void InsertPhotoMetadata(PhotoDetail photo) { String insertQuery = "INSERT INTO `photos`(`filename`, `datetaken`, `latitude`, `longitude`, `locationstring`, `tagsstring`, `public`) " + "VALUES (@filename,@datetaken,@latitude,@longitude,@locationstring,@tagsstring,@public) " + "ON DUPLICATE KEY UPDATE `filename`=@filename, `datetaken`=@datetaken, `latitude`=@latitude, `longitude`=@longitude, `locationstring`=@locationstring, `tagsstring`=@tagsstring, `public`=@public"; MySqlParameter[] insertParams = new MySqlParameter[7]; insertParams[0] = new MySqlParameter("@filename", photo.FileName); insertParams[1] = new MySqlParameter("@datetaken", photo.DateTaken); insertParams[2] = new MySqlParameter("@latitude", photo.Latitude); insertParams[3] = new MySqlParameter("@longitude", photo.Longitude); insertParams[4] = new MySqlParameter("@locationstring", photo.Location); insertParams[5] = new MySqlParameter("@tagsstring", photo.Tags); insertParams[6] = new MySqlParameter("@public", photo.IsPublic); int rc = MySqlHelper.ExecuteNonQuery(ConnectionString, insertQuery, insertParams); }
protected void GetGPSCoordinatesFromPhoto(PhotoDetail photo) { Double latitude = Double.MinValue; Double longitude = Double.MinValue; int latitudeDirection = int.MinValue; int longitudeDirection = int.MinValue; bool allInfoFound = false; // Note, we make a new Bitmap here instead of using the .Preview member of the PhotoDetail object to save memory // That member will only be popluated if the user actually wants to view the preview using (Image img = new Bitmap(photo.FQPath)) { PropertyItem[] propertyItems = img.PropertyItems; foreach (PropertyItem item in propertyItems) { // Determine Latitute (0x0002) or Longitude (0x0004) if (item.Id == 0x0002 || item.Id == 0x0004) { Double[] coordiantes = new Double[3]; for (int j = 0; j < 3; j++) { byte[] num = new byte[4]; byte[] den = new byte[4]; Array.Copy(item.Value, 0 + 8 * j, num, 0, 4); Array.Copy(item.Value, 4 + 8 * j, den, 0, 4); Int32 numerator = BitConverter.ToInt32(num, 0); Int32 denominator = BitConverter.ToInt32(den, 0); coordiantes[j] = (double)numerator / (double)denominator; } // Now convert the coordinates into degrees (with decimals) only Double degrees = coordiantes[0] + coordiantes[1] / 60.0 + coordiantes[2] / 3600.0; if (item.Id == 0x0002) { latitude = degrees; } else { longitude = degrees; } } // Determine direction for Latitude (0x0001) or Longitude (0x0003) else if (item.Id == 0x0001 || item.Id == 0x0003) { String value = Encoding.ASCII.GetString(item.Value).Trim('\0'); if (String.Compare(value, "N", true) == 0) { latitudeDirection = 1; } else if (String.Compare(value, "S", true) == 0) { latitudeDirection = -1; } else if (String.Compare(value, "E", true) == 0) { longitudeDirection = 1; } else if (String.Compare(value, "W", true) == 0) { longitudeDirection = -1; } else { throw new Exception("Invalid Latitude or Longitude reference: \"" + value + "\""); } } // Break early if all values are found if (latitude != Double.MinValue && longitude != Double.MinValue && latitudeDirection != int.MinValue && longitudeDirection != int.MinValue) { allInfoFound = true; break; } } // Only proceed if all required info was found if (allInfoFound) { // Combine degrees and direction latitude *= latitudeDirection; longitude *= longitudeDirection; // Save the latitude and longitude photo.Latitude = latitude; photo.Longitude = longitude; } else { throw new Exception("Failed to determine GPS coordinates"); } } }