e mới hỏi AI giúp cụ, cụ thử xem chứ e chưa thử
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using OfficeOpenXml;
using MetadataExtractor;
using MetadataExtractor.Formats.Exif;
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\Your\Photo\Folder";
string excelPath = @"C:\Your\Output\Coordinates.xlsx";
List<PhotoCoordinates> photoCoordinatesList = new List<PhotoCoordinates>();
foreach (string file in Directory.GetFiles(folderPath, "*.jpg"))
{
var coordinates = GetCoordinates(file);
if (coordinates != null)
{
photoCoordinatesList.Add(new PhotoCoordinates
{
FileName = Path.GetFileName(file),
Latitude = coordinates.Value.latitude,
Longitude = coordinates.Value.longitude
});
}
}
ExportToExcel(photoCoordinatesList, excelPath);
}
static (double latitude, double longitude)? GetCoordinates(string filePath)
{
try
{
var directories = ImageMetadataReader.ReadMetadata(filePath);
var gpsDirectory = directories.OfType<GpsDirectory>().FirstOrDefault();
if (gpsDirectory != null)
{
var location = gpsDirectory.GetGeoLocation();
if (location != null)
{
return (location.Latitude, location.Longitude);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading coordinates from {filePath}: {ex.ToString()}");
}
return null;
}
static void ExportToExcel(List<PhotoCoordinates> photoCoordinates, string excelPath)
{
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Coordinates");
worksheet.Cells[1, 1].Value = "File Name";
worksheet.Cells[1, 2].Value = "Latitude";
worksheet.Cells[1, 3].Value = "Longitude";
for (int i = 0; i < photoCoordinates.Count; i++)
{
worksheet.Cells[i + 2, 1].Value = photoCoordinates
.FileName;
worksheet.Cells[i + 2, 2].Value = photoCoordinates.Latitude;
worksheet.Cells[i + 2, 3].Value = photoCoordinates.Longitude;
}
FileInfo fileInfo = new FileInfo(excelPath);
package.SaveAs(fileInfo);
}
}
}
class PhotoCoordinates
{
public string FileName { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}