支持解析图像坐标

This commit is contained in:
Chenx221 2024-07-28 13:50:52 +08:00
parent 3a3f650959
commit 21232e48ef

View File

@ -1,5 +1,6 @@
using ImageMagick; using ImageMagick;
using NLua; using NLua;
using System.Text.RegularExpressions;
namespace ArtemisFgTools namespace ArtemisFgTools
{ {
internal class Program internal class Program
@ -44,8 +45,7 @@ namespace ArtemisFgTools
} }
Console.WriteLine($"fg count: {fgObjects.Count}"); Console.WriteLine($"fg count: {fgObjects.Count}");
//Todo: Combine fgObjects with image position //Todo: Combine fgObjects with image position
//Todo: Get image position from png comment List<int> comment = ReadPngComment("G:/x221.local/fg/baa/fa/a0001.png");
String comment = ReadPngComment("G:/x221.local/fg/baa/fa/a0001.png");
} }
else else
@ -164,16 +164,30 @@ namespace ArtemisFgTools
return result; return result;
} }
public static string ReadPngComment(string filePath) public static List<int> ReadPngComment(string filePath)
{ {
if (File.Exists(filePath)) if (File.Exists(filePath))
{ {
// Read image from file // Read image from file
using var image = new MagickImage(filePath); using var image = new MagickImage(filePath);
if(image.Comment != null) if (image.Comment != null)
{ {
return image.Comment; string pattern = @"^pos,(\d+),(\d+),(\d+),(\d+)$";
Match match = Regex.Match(image.Comment, pattern);
if (match.Success)
{
int x = int.Parse(match.Groups[1].Value);
int y = int.Parse(match.Groups[2].Value);
int w = int.Parse(match.Groups[3].Value);
int h = int.Parse(match.Groups[4].Value);
return [x, y, w, h];
}
else
{
throw new Exception("Unexpected result");
}
} }
else else
{ {
@ -182,7 +196,7 @@ namespace ArtemisFgTools
} }
else else
{ {
Console.WriteLine("File does not exist."); throw new Exception("File does not exist.");
} }
} }