1. 缩略图提取并上传

2. 提取焦距、分辨率、大小等信息
3. 省对接相关部分修改
main
陈伟 1 month ago
parent 54d533ec75
commit e86dcb6c3c

@ -16,10 +16,10 @@ public class ExtData
public int pitch { get; set; } public int pitch { get; set; }
public int roll { get; set; } public int roll { get; set; }
public DateTime? time { get; set; } public DateTime? time { get; set; }
public double height { get; set; } public double? height { get; set; }
public long imgWidth { get; set; } public long imgWidth { get; set; }
public long imgHeight { get; set; } public long imgHeight { get; set; }
public long imgOriginWidth { get; set; } public long imgOriginWidth { get; set; }
public long imgOriginHeight { get; set; } public long imgOriginHeight { get; set; }
public double psjj { get; set; } public double? psjj { get; set; }
} }

@ -1737,7 +1737,7 @@ namespace OpenAuth.App.ServiceApp
imgWidth = dimensions.width, imgWidth = dimensions.width,
imgOriginHeight = dimensions.height, imgOriginHeight = dimensions.height,
imgOriginWidth = dimensions.width, imgOriginWidth = dimensions.width,
psjj = 24, // 等效焦距 todo 等效焦距如何取 psjj = lasaMediaFile.FocalLength,
time = lasaMediaFile.CreateTime, time = lasaMediaFile.CreateTime,
height = lasaMediaFile.Height, height = lasaMediaFile.Height,
lon = lasaMediaFile.Lng, lon = lasaMediaFile.Lng,

@ -5,6 +5,9 @@ using Infrastructure.CloudSdk;
using Infrastructure.CloudSdk.minio; using Infrastructure.CloudSdk.minio;
using Infrastructure.CloudSdk.wayline; using Infrastructure.CloudSdk.wayline;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using Irony.Parsing.Construction;
using MetadataExtractor;
using MetadataExtractor.Formats.Exif;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet.Client; using MQTTnet.Client;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -250,7 +253,9 @@ public class ConfigSubscribe : IJob
FlightId = flightId, FlightId = flightId,
TaskId = taskId, TaskId = taskId,
ParentKey = "0", ParentKey = "0",
Name = string.IsNullOrEmpty(executeTask.TaskName) ? timeStr : $"{executeTask.TaskName} {timeStr}", Name = string.IsNullOrEmpty(executeTask.TaskName)
? timeStr
: $"{executeTask.TaskName} {timeStr}",
WorkspaceId = executeTask.WorkspaceId, WorkspaceId = executeTask.WorkspaceId,
CreateTime = date, CreateTime = date,
}; };
@ -264,8 +269,111 @@ public class ConfigSubscribe : IJob
.Where(a => a.ObjectKey.Equals(objectKey)).SingleAsync(); .Where(a => a.ObjectKey.Equals(objectKey)).SingleAsync();
if (mediaFile == null) if (mediaFile == null)
{ {
string objectKey1 = data.file.object_key.ToString();
string suoluokey = "minipic/" + data.file.name.ToString(); string suoluokey = "minipic/" + data.file.name.ToString();
long? picSize = 0;
int width = 0, height = 0, focalLength = 0;
int offset = 0, length = 0;
var fileUrl ="http://" + _minioService.endPoint + "/" + _minioService._bucketName + "/" + objectKey;
using (var httpClient = new HttpClient())
{
// 目前读取64KB
// 添加Range请求头
httpClient.DefaultRequestHeaders.Range =
new System.Net.Http.Headers.RangeHeaderValue(0, 65535);
try
{
var response = httpClient
.GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead).Result;
if (response.StatusCode == System.Net.HttpStatusCode.PartialContent)
{
//Focal Length 35: 24 mm
//Exif Image Width: 4032 pixels : Exif SubIFD
//Exif Image Height: 3024 pixels : Exif SubIFD
// 3.1 解析Content-Range头格式bytes start-end/total
var contentRange = response.Content.Headers.ContentRange;
if (contentRange != null)
{
picSize = contentRange.Length.Value;
Console.WriteLine(
$"获取范围:{contentRange.From}-{contentRange.To}/{contentRange.Length}");
}
// 成功获取部分内容
var y = response.Content.ReadAsByteArrayAsync().Result;
MemoryStream ms = new MemoryStream(y);
var directories = ImageMetadataReader.ReadMetadata(ms);
foreach (var directory in directories)
{
if (directory is ExifDirectoryBase)
{
if (directory.Name.Equals("Exif SubIFD"))
{
foreach (var tag in directory.Tags)
{
if (tag.Name.Equals("Exif Image Width"))
{
width = int.Parse(tag.Description.Replace("pixels", "")
.Trim());
}
if (tag.Name.Equals("Exif Image Height"))
{
height = int.Parse(tag.Description.Replace("pixels", "")
.Trim());
}
if (tag.Name.Equals("Focal Length 35"))
{
focalLength = int.Parse(tag.Description.Replace("mm", "")
.Trim());
}
}
}
//Console.WriteLine(directory.Name);
if (directory.Name.Equals("Exif Thumbnail"))
{
foreach (var tag in directory.Tags)
{
if (tag.Name.Equals("Thumbnail Offset"))
{
offset = int.Parse(tag.Description.Replace("bytes", "")
.Trim());
}
if (tag.Name.Equals("Thumbnail Length"))
{
length = int.Parse(tag.Description.Replace("bytes", "")
.Trim());
}
}
}
}
}
ms.Seek(offset + 6, SeekOrigin.Begin);
byte[] buffer = new byte[length];
int bytesRead = ms.Read(buffer, 0, length);
// 上传缩略图到MinIO
await _minioService.PutObjectAsync("", data.file.name.ToString(), suoluokey,
new MemoryStream(buffer));
}
else if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
// 服务器不支持Range请求返回完整内容
throw new InvalidOperationException("服务器不支持Range请求");
}
else
{
throw new HttpRequestException($"请求失败: {response.StatusCode}");
}
}
catch (Exception ex)
{
throw new Exception($"下载Range数据失败: {ex.Message}", ex);
}
}
/*string objectKey1 = data.file.object_key.ToString();
//缩略图处理 //缩略图处理
var imageStream = _minioService.GetObjectAsStream("", objectKey1); var imageStream = _minioService.GetObjectAsStream("", objectKey1);
var width = 0; var width = 0;
@ -293,7 +401,7 @@ public class ConfigSubscribe : IJob
await _minioService.PutObjectAsync("", data.file.name.ToString(), suoluokey, await _minioService.PutObjectAsync("", data.file.name.ToString(), suoluokey,
memoryStream); // 根据实际格式修改Content-Type memoryStream); // 根据实际格式修改Content-Type
} }
} }*/
var fileUpload = new LasaMediaFile() var fileUpload = new LasaMediaFile()
{ {
@ -321,9 +429,10 @@ public class ConfigSubscribe : IJob
Width = width, Width = width,
Height = height, Height = height,
minipic = suoluokey, minipic = suoluokey,
//Size = size, Size = picSize,
ShowOnMap = 1, ShowOnMap = 1,
display = 1 display = 1,
FocalLength = focalLength
}; };
// todo 添加事务 // todo 添加事务
await _sqlSugarClient.Insertable(fileUpload).ExecuteCommandAsync(); await _sqlSugarClient.Insertable(fileUpload).ExecuteCommandAsync();

@ -108,9 +108,9 @@ public class LasaMediaFile
public string FileTags { get; set; } public string FileTags { get; set; }
public long Size { get; set; } public long? Size { get; set; }
public int Width { get; set; } public int? Width { get; set; }
public int Height { get; set; } public int? Height { get; set; }
public string Tid { get; set; } public string Tid { get; set; }
public string Bid { get; set; } public string Bid { get; set; }
@ -120,6 +120,8 @@ public class LasaMediaFile
public string PicLink { get; set; } public string PicLink { get; set; }
public string minipic { get; set; } public string minipic { get; set; }
public double? FocalLength { get; set; }
[SugarColumn(IsIgnore = true)] public List<LasaMediaFile> Children { get; set; } [SugarColumn(IsIgnore = true)] public List<LasaMediaFile> Children { get; set; }

@ -9,6 +9,7 @@ using MetadataExtractor.Formats.Exif;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NUnit.Framework; using NUnit.Framework;
using OpenAuth.Repository.Domain;
namespace OpenAuth.WebApi.boot; namespace OpenAuth.WebApi.boot;
@ -423,6 +424,10 @@ public class TestAbc
[Test] [Test]
public void DownloadRange() public void DownloadRange()
{ {
var minio = new MinioService();
long? picSize = 0;
int width = 0, height = 0, focalLength = 0;
int offset = 0, length = 0;
using (var httpClient = new HttpClient()) using (var httpClient = new HttpClient())
{ {
var total = 0L; var total = 0L;
@ -432,7 +437,8 @@ public class TestAbc
{ {
var response = httpClient var response = httpClient
.GetAsync( .GetAsync(
"http://175.27.168.120:6013/test/640aea27-2dd3-4081-933c-fcef432fcec0/71924889-540d-4362-aeb1-9ee787a655a7/DJI_202508050906_002_71924889-540d-4362-aeb1-9ee787a655a7/DJI_20250805090809_0001_V.jpeg", "http://" + minio.endPoint + "/" + minio._bucketName + "/" +
"/640aea27-2dd3-4081-933c-fcef432fcec0/71924889-540d-4362-aeb1-9ee787a655a7/DJI_202508050906_002_71924889-540d-4362-aeb1-9ee787a655a7/DJI_20250805090809_0001_V.jpeg",
HttpCompletionOption.ResponseHeadersRead).Result; HttpCompletionOption.ResponseHeadersRead).Result;
if (response.StatusCode == System.Net.HttpStatusCode.PartialContent) if (response.StatusCode == System.Net.HttpStatusCode.PartialContent)
@ -444,6 +450,7 @@ public class TestAbc
var contentRange = response.Content.Headers.ContentRange; var contentRange = response.Content.Headers.ContentRange;
if (contentRange != null) if (contentRange != null)
{ {
picSize = contentRange.Length;
Console.WriteLine($"获取范围:{contentRange.From}-{contentRange.To}/{contentRange.Length}"); Console.WriteLine($"获取范围:{contentRange.From}-{contentRange.To}/{contentRange.Length}");
} }
@ -451,14 +458,37 @@ public class TestAbc
var y = response.Content.ReadAsByteArrayAsync().Result; var y = response.Content.ReadAsByteArrayAsync().Result;
MemoryStream ms = new MemoryStream(y); MemoryStream ms = new MemoryStream(y);
var directories = ImageMetadataReader.ReadMetadata(ms); var directories = ImageMetadataReader.ReadMetadata(ms);
int offset = 0, length = 0;
foreach (var directory in directories) foreach (var directory in directories)
{ {
if (directory is ExifDirectoryBase) if (directory is ExifDirectoryBase)
{ {
foreach (var directoryTag in directory.Tags) foreach (var directoryTag in directory.Tags)
{ {
Console.WriteLine($"{directoryTag.Name}: {directoryTag.Description} : { directoryTag.DirectoryName}"); Console.WriteLine(
$"{directoryTag.Name}: {directoryTag.Description} : {directoryTag.DirectoryName}");
}
if (directory.Name.Equals("Exif SubIFD"))
{
foreach (var tag in directory.Tags)
{
if (tag.Name.Equals("Exif Image Width"))
{
width = int.Parse(tag.Description.Replace("pixels", "")
.Trim());
}
if (tag.Name.Equals("Exif Image Height"))
{
height = int.Parse(tag.Description.Replace("pixels", "")
.Trim());
}
if (tag.Name.Equals("Focal Length 35"))
{
focalLength = int.Parse(tag.Description.Replace("mm", "")
.Trim());
}
}
} }
//Console.WriteLine(directory.Name); //Console.WriteLine(directory.Name);
@ -480,13 +510,25 @@ public class TestAbc
} }
} }
var fileUpload = new LasaMediaFile()
{
Id = Guid.NewGuid().ToString(),
Width = width,
Height = height,
Size = picSize,
ShowOnMap = 1,
display = 1,
FocalLength = focalLength
};
Console.WriteLine("是否有错呢:" + JsonConvert.SerializeObject(fileUpload));
ms.Seek(offset + 6, SeekOrigin.Begin); ms.Seek(offset + 6, SeekOrigin.Begin);
byte[] buffer = new byte[length]; byte[] buffer = new byte[length];
int bytesRead = ms.Read(buffer, 0, length); int bytesRead = ms.Read(buffer, 0, length);
var x = IsValidImageFormat(buffer); var x = IsValidImageFormat(buffer);
Console.WriteLine("是否为有效图片:" + x); Console.WriteLine("是否为有效图片:" + x);
Console.WriteLine("读取的字节长度: " + bytesRead); Console.WriteLine("读取的字节长度: " + bytesRead);
File.WriteAllBytes("f:/abcccc.jpeg", buffer); File.WriteAllBytes("f:/abbbbbb.jpeg", buffer);
} }
else if (response.StatusCode == System.Net.HttpStatusCode.OK) else if (response.StatusCode == System.Net.HttpStatusCode.OK)
{ {

Loading…
Cancel
Save