Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
coraxduya committed Oct 1, 2021
0 parents commit 6e43274
Show file tree
Hide file tree
Showing 34 changed files with 3,299 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
.vs/

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# Mine
Tmp/
/packages/
node_modules/
25 changes: 25 additions & 0 deletions GenshinAutoFish.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31229.75
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenshinAutoFish", "GenshinAutoFish\GenshinAutoFish.csproj", "{F17BE4C5-926C-456D-9CC0-606DAE304ED8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F17BE4C5-926C-456D-9CC0-606DAE304ED8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F17BE4C5-926C-456D-9CC0-606DAE304ED8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F17BE4C5-926C-456D-9CC0-606DAE304ED8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F17BE4C5-926C-456D-9CC0-606DAE304ED8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DFA8F1CD-513D-45D7-856D-378146C4D367}
EndGlobalSection
EndGlobal
47 changes: 47 additions & 0 deletions GenshinAutoFish/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="GenshinAutoFish.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<userSettings>
<GenshinAutoFish.Properties.Settings>
<setting name="StrainBarAreaLocation" serializeAs="String">
<value>0, 0</value>
</setting>
<setting name="StrainBarAreaSize" serializeAs="String">
<value>599, 50</value>
</setting>
<setting name="FormMainLocation" serializeAs="String">
<value>100, 100</value>
</setting>
<setting name="TopMostChecked" serializeAs="String">
<value>False</value>
</setting>
<setting name="FrameRate" serializeAs="String">
<value>30</value>
</setting>
<setting name="AutoPullUpChecked" serializeAs="String">
<value>True</value>
</setting>
<setting name="DisplayDetectChecked" serializeAs="String">
<value>False</value>
</setting>
<setting name="AlwaysHideAreaChecked" serializeAs="String">
<value>False</value>
</setting>
</GenshinAutoFish.Properties.Settings>
</userSettings>
</configuration>
62 changes: 62 additions & 0 deletions GenshinAutoFish/Core/ImageCapture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using GenshinAutoFish.Utils;
using System;
using System.Drawing;

namespace GenshinAutoFish.Core
{
public class ImageCapture
{
IntPtr hwnd;
IntPtr hdc;

public int X { get; set; }
public int Y { get; set; }
public int W { get; set; }
public int H { get; set; }

public void Start(int x, int y, int w, int h)
{
hwnd = Native.GetDesktopWindow();
hdc = Native.GetDC(hwnd);
this.X = x;
this.Y = y;
this.W = w;
this.H = h;
}

public Bitmap Capture(bool extend, out Bitmap rodWordsAreaBitmap)
{
Bitmap bmp = new Bitmap(W, H);
Graphics bmpGraphic = Graphics.FromImage(bmp);
//get handle to source graphic
IntPtr bmpHdc = bmpGraphic.GetHdc();

//copy it
bool res = Native.StretchBlt(bmpHdc, 0, 0, W, H,
hdc, X, Y, W, H, Native.CopyPixelOperation.SourceCopy);
bmpGraphic.ReleaseHdc();

// 非钓鱼期间不需要这个图片
if (extend)
{
rodWordsAreaBitmap = new Bitmap(W, H * 2);
Graphics bmpGraphic2 = Graphics.FromImage(rodWordsAreaBitmap);
IntPtr bmpHdc2 = bmpGraphic2.GetHdc();
Native.StretchBlt(bmpHdc2, 0, 0, W, H * 2,
hdc, X, Y + H, W, H * 2, Native.CopyPixelOperation.SourceCopy);
bmpGraphic2.ReleaseHdc();

}
else
{
rodWordsAreaBitmap = null;
}
return bmp;
}

public void Stop()
{
Native.ReleaseDC(hwnd, hdc);
}
}
}
103 changes: 103 additions & 0 deletions GenshinAutoFish/Core/ImageRecognition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace GenshinAutoFish.Core
{
class ImageRecognition
{
public static Bitmap GetRect(Bitmap img, out List<Rect> rects, bool enableImShow)
{
using (Mat mask = new Mat())
using (Mat rgbMat = new Mat())
using (Mat src = img.ToMat())
{
Cv2.CvtColor(src, rgbMat, ColorConversionCodes.BGR2RGB);
var lowPurple = new Scalar(255, 255, 192);
var highPurple = new Scalar(255, 255, 192);
Cv2.InRange(rgbMat, lowPurple, highPurple, mask);
Cv2.Threshold(mask, mask, 0, 255, ThresholdTypes.Binary); //二值化

OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchy;
Cv2.FindContours(mask, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple, null);
if (contours.Length > 0)
{
var imgTar = src.Clone();
var boxes = contours.Select(Cv2.BoundingRect).Where(w => w.Height >= 10);
rects = boxes.ToList();
foreach (Rect rect in rects)
{
Cv2.Rectangle(imgTar, new OpenCvSharp.Point(rect.X, rect.Y), new OpenCvSharp.Point(rect.X + rect.Width, rect.Y + rect.Height), Scalar.Red, 2);
}
if (enableImShow)
{
Cv2.ImShow("钓鱼条识别窗口", imgTar);
}
return imgTar.ToBitmap();
}
else
{
rects = null;
return src.ToBitmap();
}
}

}

public static Rect MatchWords(Bitmap img, ImageCapture capture, bool enableImShow)
{
using (Mat src = img.ToMat())
using (Mat result = new Mat())
{
Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);
var lowPurple = new Scalar(253, 253, 253);
var highPurple = new Scalar(255, 255, 255);
Cv2.InRange(src, lowPurple, highPurple, src);
Cv2.Threshold(src, src, 0, 255, ThresholdTypes.Binary);
var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(20, 20), new OpenCvSharp.Point(-1, -1));
Cv2.Dilate(src, src, kernel); //膨胀

Scalar color = new Scalar(0, 0, 255);
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchy;
Cv2.FindContours(src, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple, null);
if (contours.Length > 0)
{
var imgTar = img.ToMat();
var boxes = contours.Select(Cv2.BoundingRect);
List<Rect> rects = boxes.ToList();
if (rects.Count > 1)
{
rects.Sort((a, b) => b.Height.CompareTo(a.Height));
}
if (rects[0].Height < src.Height
&& rects[0].Width * 1.0 / rects[0].Height >= 3 // 长宽比判断
&& capture.W > rects[0].Width * 3 // 文字范围3倍小于钓鱼条范围的
&& capture.W * 1.0 / 2 > rects[0].X // 中轴线判断左
&& capture.W * 1.0 / 2 < rects[0].X + rects[0].Width) // 中轴线判断右
{
foreach (Rect rect in rects)
{
Cv2.Rectangle(imgTar, new OpenCvSharp.Point(rect.X, rect.Y), new OpenCvSharp.Point(rect.X + rect.Width, rect.Y + rect.Height), Scalar.Red, 2);
}
if (enableImShow)
{
Cv2.ImShow("自动提杆识别窗口", imgTar);
}
return rects[0];
}
}
}
return Rect.Empty;
}

}
}
78 changes: 78 additions & 0 deletions GenshinAutoFish/Core/YuanShenWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using GenshinAutoFish.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GenshinAutoFish.Core
{
public class YuanShenWindow
{
public static uint WM_LBUTTONDOWN = 0x201; //按下鼠标左键

public static uint WM_LBUTTONUP = 0x202; //释放鼠标左键


private IntPtr hWnd;
public YuanShenWindow()
{

}

public bool GetHWND()
{
var pros = Process.GetProcessesByName("YuanShen");
if (pros.Any())
{
hWnd = pros[0].MainWindowHandle;
return true;
}
else
{
pros = Process.GetProcessesByName("GenshinImpact");
if (pros.Any())
{
hWnd = pros[0].MainWindowHandle;
return true;
}
else
{
return false;
}
}
}

public Rectangle GetSize()
{
Native.RECT rc = new Native.RECT();
Native.GetWindowRect(hWnd, ref rc);
return new Rectangle(rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top);
}

public void MouseLeftButtonDown()
{
IntPtr p = (IntPtr)((0 << 16) | 0);
Native.PostMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, p);

}

public void MouseLeftButtonUp()
{
IntPtr p = (IntPtr)((0 << 16) | 0);
Native.PostMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, p);
}

public void MouseClick(int x, int y)
{
IntPtr p = (IntPtr)((y << 16) | x);
Native.PostMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, p);
Thread.Sleep(100);
Native.PostMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, p);
}
}
}
Loading

0 comments on commit 6e43274

Please sign in to comment.