Skip to content

Commit

Permalink
简化方法
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoweijie committed May 28, 2021
1 parent d269b13 commit 994b3e3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Main/Main.csproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LastSelectedProfileId>E:\Git\Onmyoji\Main\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
<_LastSelectedProfileId>F:\Git\Onmyoji\Main\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
</PropertyGroup>
<ItemGroup>
<Compile Update="mainForm.cs">
Expand Down
118 changes: 48 additions & 70 deletions Main/Service/TaskExcuteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ public class TaskExcuteService : ImagePhash
{
//bool state { get; set; } = false;
public static ConcurrentDictionary<int, Digest> keyValuePairs { get; set; } = new();
public static ConcurrentDictionary<GameType, Digest> keyValueGameType { get; set; } = new();
public static ConcurrentDictionary<string, Digest> keyValueGameType { get; set; } = new();


#region 感知哈希算法
/// <summary>
/// 任务开始,比较两张图片的相似度
/// 默认值90%
/// 感知哈希
/// </summary>
/// <param name="imageList"></param>
/// <param name="gameType"></param>
/// <returns></returns>
public bool StartTaskForPHash(List<Bitmap> imageList)
public static bool StartTaskForPHash(List<Bitmap> imageList, GameType gameType)
{
bool state = true;
//比较两张图片的评分
float score;
int count = 0;

for (int i = 0; i < imageList.Count; i++)
{
//比较两张图片的评分
float score;
//////获取图片
//using Bitmap image = (Bitmap)MouseHookHelper.Capture(list[i].hWnd);

Expand All @@ -44,9 +48,10 @@ public bool StartTaskForPHash(List<Bitmap> imageList)
//获取游戏图片的哈希
var gameHash = ComputeDigest(imageList[i].ToLuminanceImage());
//string gameHash = SimilarPhoto.GetHash(image);
var hash = GetMainPic(i);
var hash = GetMainPic(i,gameType);
score = GetCrossCorrelation(gameHash, hash);
if(score < 0.8f)
//如果低于70%
if (score < 0.7f)
{
state = false;
}
Expand All @@ -58,48 +63,50 @@ public bool StartTaskForPHash(List<Bitmap> imageList)


/// <summary>
/// 任务开始,比较两张图片的相似度
/// 默认值90%
/// 感知哈希
/// 获取参照图
/// </summary>
/// <param name="imageList"></param>
/// <param name="id"></param>
/// <param name="gameType"></param>
/// <returns></returns>
public bool StartTaskForPHash(List<Bitmap> imageList, GameType gameType)
public static Digest GetMainPic(int id, GameType gameType)
{
bool state = true;
//比较两张图片的评分
float score;
int count = 0;
for (int i = 0; i < imageList.Count; i++)
if (!keyValueGameType.TryGetValue($"{gameType}_{id}", out Digest hash))
{
//////获取图片
//using Bitmap image = (Bitmap)MouseHookHelper.Capture(list[i].hWnd);
if (gameType == GameType.yu)
{
using var bitmap = (Bitmap)Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Images\\MainImage\\{id + 1}.png"));
hash = ComputeDigest(bitmap.ToLuminanceImage());

//SimilarPhoto similarPhoto = new ();
//Image mainImage = null;
//获取游戏图片的哈希
var gameHash = ComputeDigest(imageList[i].ToLuminanceImage());
//string gameHash = SimilarPhoto.GetHash(image);
var hash = GetMainPic(i,gameType);
score = GetCrossCorrelation(gameHash, hash);
if (score < 0.8f)
}
if (gameType == GameType.ling)
{
state = false;
using var bitmap = (Bitmap)Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\\yuling\\1.png"));
hash = ComputeDigest(bitmap.ToLuminanceImage());
}
if (gameType == GameType.ye)
{
using var bitmap = (Bitmap)Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\\yeyuanhuo\\1.png"));
hash = ComputeDigest(bitmap.ToLuminanceImage());
}

keyValueGameType.TryAdd($"{gameType}_{id}", hash);
}
//默认值90%
//return score > DEFAULT_THRESHOLD;
return state;

return hash;
}

#endregion


#region 均值哈希算法

/// <summary>
/// 任务开始,比较两张图片的相似度
/// 均值哈希
/// </summary>
/// <param name="windowsList"></param>
/// <returns></returns>
public bool StartTaskForAHash(List<Image> imageList)
public static bool StartTaskForAHash(List<Image> imageList)
{
bool state = false;

Expand All @@ -119,45 +126,8 @@ public bool StartTaskForAHash(List<Image> imageList)
return state;
}

public Digest GetMainPic(int id)
{
if (!keyValuePairs.TryGetValue(id, out Digest hash))
{
//Image mainImage = null;
//Bitmap bitmap = null;
//第一张

//mainImage = SimilarPhoto.GetImage(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\\1.png"));
using var bitmap = (Bitmap)Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Images\\MainImage\\{id + 1}.png"));
hash = ComputeDigest(bitmap.ToLuminanceImage());

//var score = ImagePhash.GetCrossCorrelation(hash1, hash2);
keyValuePairs.TryAdd(id, hash);
}
return hash;
}


public Digest GetMainPic(int id,GameType gameType)
{
if (!keyValueGameType.TryGetValue(gameType,out Digest hash))
{
if (gameType == GameType.ling)
{
using var bitmap = (Bitmap)Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\\yuling\\1.png"));
hash = ComputeDigest(bitmap.ToLuminanceImage());
}
if (gameType == GameType.ye)
{
using var bitmap = (Bitmap)Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\\yeyuanhuo\\1.png"));
hash = ComputeDigest(bitmap.ToLuminanceImage());
}
}

return hash;
}

public string GetMainPicForAHash(int id)
public static string GetMainPicForAHash(int id)
{

Image mainImage = null;
Expand Down Expand Up @@ -188,6 +158,14 @@ public string GetMainPicForAHash(int id)
return value;
}


#endregion






/// <summary>
/// 测试方法
/// </summary>
Expand Down
42 changes: 19 additions & 23 deletions Main/mainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class mainForm : Form
///// </summary>
//bool isRunning = false;
KeyEventHandler myKeyEventHandeler;
readonly KeyboardHook service = new KeyboardHook();
readonly KeyboardHook service = new();
bool task = false;
int min = 22;

Expand Down Expand Up @@ -209,7 +209,7 @@ private void timerMouseEvent_Tick(object sender, EventArgs e)
}
else
{
_ = MouseClickMethod(DateTime.Now.Second);
_ = MouseClickMethod();
}
//timerMouseEvent.Start();

Expand All @@ -224,10 +224,10 @@ private void timerMouseEvent_Tick(object sender, EventArgs e)
/// </summary>
/// <param name="flag"></param>
/// <returns></returns>
public async Task MouseClickMethod(int flag)
public async Task MouseClickMethod()
{
//耗时程序
Stopwatch sw = new Stopwatch();
Stopwatch sw = new();
sw.Start();
if (quanTask != null && !quanTask.IsCompleted)
{
Expand All @@ -241,7 +241,7 @@ public async Task MouseClickMethod(int flag)
}
if (gameType == GameType.ye || gameType==GameType.ling)
{
quanTask = Task.Run(() => RunningYe());//委托方法
quanTask = Task.Run(() => RunningYeOrLing());//委托方法
}
await quanTask;
//判断是否已完成
Expand Down Expand Up @@ -284,16 +284,17 @@ private void btnPictrue_Click(object sender, EventArgs e)
{
////获取图片
Image image = MouseHookHelper.Capture(windowInfos[i].hWnd);
Bitmap bmp = new Bitmap(image);
Bitmap bmp = new(image);
PicGetHelper.GetP(bmp, (i+1).ToString(), gameType);
list.Add(bmp);
}
//foreach (var item in windowInfos)
//{

//}
TaskExcuteService taskExcuteService = new();
bool contrastPic = taskExcuteService.StartTaskForPHash(list);
bool contrastPic = TaskExcuteService.StartTaskForPHash(list,gameType);

MessageBox.Show(contrastPic.ToString());
}


Expand Down Expand Up @@ -444,11 +445,10 @@ private void RunningYu()
{


EventMethodService eventMethod = new EventMethodService();
EventMethodService eventMethod = new();
//开始
eventMethod.MouseClick(rects);
Thread.Sleep((min + 4) * 1000);
TaskExcuteService taskExcuteService = new();
//图片对比状态
bool contrastPic = false;
while (!contrastPic)
Expand All @@ -466,24 +466,22 @@ private void RunningYu()
{
////获取图片
using Image image = MouseHookHelper.Capture(item.hWnd);
Bitmap bmp = new Bitmap(image);
Bitmap bmp = new(image);
list.Add(bmp);
}
contrastPic = taskExcuteService.StartTaskForPHash(list);
contrastPic = TaskExcuteService.StartTaskForPHash(list, GameType.yu);
}

}

/// <summary>
/// 业原火,御灵
/// </summary>
private void RunningYe()
private void RunningYeOrLing()
{
EventMethodService eventMethod = new EventMethodService();
EventMethodService eventMethod = new();
//开始
eventMethod.Click(rects);

TaskExcuteService taskExcuteService = new();
//图片对比状态
bool contrastPic = false;
while (!contrastPic)
Expand All @@ -503,26 +501,24 @@ private void RunningYe()
Bitmap bmp = new(image);
list.Add(bmp);
}
contrastPic = taskExcuteService.StartTaskForPHash(list,gameType);
contrastPic = TaskExcuteService.StartTaskForPHash(list,gameType);
}
}

private void btnTestPic_Click(object sender, EventArgs e)
{
TaskExcuteService taskExcuteService = new();
//TaskExcuteService taskExcuteService = new();
List<Bitmap> list = new();
foreach (var item in windowInfos)
{
////获取图片
using Image image = MouseHookHelper.Capture(item.hWnd);
Bitmap bmp = new Bitmap(image);
Bitmap bmp = new(image);
list.Add(bmp);
}
//图片对比状态
bool contrastPic = false;
contrastPic = taskExcuteService.StartTaskForPHash(list);
bool contrastPic = TaskExcuteService.StartTaskForPHash(list, gameType);
MessageBox.Show(contrastPic.ToString());
}
}


}

0 comments on commit 994b3e3

Please sign in to comment.