-
Notifications
You must be signed in to change notification settings - Fork 1
/
CV.cs
executable file
·254 lines (219 loc) · 11 KB
/
CV.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; // 用 DllImport 需用此 命名空間
using System.IO;// 用 Path
using System.Windows.Forms;
namespace InstaFilter
{
class CV
{
//Random rnd = new Random(Guid.NewGuid().GetHashCode());
#region private 資料
private string _sourceFileInTempName; //原始檔案複製一份到暫存
private string _resizeFileName; //縮小版本檔案
private string _tempPath; //暫存資料夾位置
private string _resizeDll; //resize用dll位置
private string _showDll; //show用dll位置
#endregion
#region CV constructor
public CV(string resizeDll, string showDll, string sourceFile, string resizeFIleName, string tempPath)
{
try
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
_resizeDll = resizeDll;
//解決中文檔名問題
_sourceFileInTempName = tempPath + @"\_" + rnd.Next() + Path.GetExtension(sourceFile);//將原檔重新命名放入暫存裡
File.Copy(sourceFile, _sourceFileInTempName); //複製檔案
_resizeFileName = resizeFIleName;
_tempPath = tempPath;
_showDll = showDll;
//resize版本存在時, 刪除檔案
if (File.Exists(_resizeFileName))
File.Delete(_resizeFileName);
//製作resize版本
string a = Resize(_sourceFileInTempName, _resizeFileName);
if (a == null) throw (new Exception("resize時, 發生錯誤"));
}
catch (Exception ex) { MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}//end CV constructor
#endregion
#region Resize
public string Resize(string inputFile, string outputFile)
{
dld myDLD = new dld();
myDLD.LoadFun(_resizeDll, "_opencvResize@8");
//回傳型態參數設定
object[] Parameters = new object[] { Marshal.StringToBSTR(inputFile), Marshal.StringToBSTR(outputFile) }; // 參數列
Type[] ParameterTypes = new Type[] { typeof(IntPtr), typeof(IntPtr) }; // 參數資料型態
dld.ModePass[] themode = new dld.ModePass[] { dld.ModePass.ByValue, dld.ModePass.ByValue }; // 全部傳值呼叫
Type Type_Return = typeof(bool);// 回傳型態
//回傳值判斷
bool a = (bool)myDLD.Invoke(Parameters, ParameterTypes, themode, Type_Return);
myDLD.UnLoadDll();
if(a)
return outputFile;
return null;
}//end resize
#endregion
#region insideProcess
private string insideProcess(string dll, string inputFile, string outputFile)
{
try
{
dld myDLD = new dld();
myDLD.LoadFun(dll, "_opencvProcess@8");
//回傳型態參數設定
object[] Parameters = new object[] { Marshal.StringToBSTR(inputFile), Marshal.StringToBSTR(outputFile) }; // 參數列
Type[] ParameterTypes = new Type[] { typeof(IntPtr), typeof(IntPtr) }; // 參數資料型態
dld.ModePass[] themode = new dld.ModePass[] { dld.ModePass.ByValue, dld.ModePass.ByValue }; // 全部傳值呼叫
Type Type_Return = typeof(bool);// 回傳型態
//回傳值判斷
bool a = (bool)myDLD.Invoke(Parameters, ParameterTypes, themode, Type_Return);
myDLD.UnLoadDll();
if (a)
return outputFile;
return null;
}
catch (Exception ex) { MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; }
}//end insideProcess
#endregion
#region Process
public string Process(string dll)
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
string outputName = _tempPath + @"\" + rnd.Next() + Path.GetExtension(_sourceFileInTempName);
return this.insideProcess(dll, _resizeFileName, outputName);
}//end Process
#endregion
#region ProcessSP
public string ProcessSP(string dll)
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
string outputName = _tempPath + @"\" + rnd.Next() + Path.GetExtension(_sourceFileInTempName);
string tempOutput = this.insideProcess(dll, _sourceFileInTempName, outputName);
if (tempOutput != null)//正確做出原尺寸的濾鏡
{
string outputName2 = _tempPath + @"\" + rnd.Next() + Path.GetExtension(_sourceFileInTempName);
return this.Resize(tempOutput, outputName2);
}//end if
else
return null;
}//end Process
#endregion
#region ProcessOrginalSize
public string ProcessOrginalSize(string processImage, string dll)
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
string outputName = _tempPath + @"\" + rnd.Next() + Path.GetExtension(processImage);
if (Path.GetExtension(dll) == ".if")
return this.customizeFilterOrginalSize(processImage, dll);
else
return this.insideProcess(dll, processImage, outputName);
}//end ProcessOrignalSize
#endregion
#region GetFilterName
public static string GetFilterName(String dll)
{
try
{
if (!File.Exists(dll))
throw new Exception("濾鏡不存在");
if (Path.GetExtension(dll) == ".if")
{
StreamReader sr = new StreamReader(dll);
string filterName = sr.ReadLine();//濾鏡名稱
string[] temp = filterName.Split(new string[]{ Environment.NewLine }, 256, StringSplitOptions.None);
filterName = temp[0];
sr.Close();
return filterName;
}//end if
else
{
dld myDLD = new dld();
myDLD.LoadFun(dll, "_getFilterName@0");
object[] Parameters = new object[] { };
Type[] ParameterTypes = new Type[] { };
dld.ModePass[] themode = new dld.ModePass[] { };
Type Type_Return = typeof(IntPtr);// 回傳型態
string filter = Marshal.PtrToStringUni((IntPtr)myDLD.Invoke(Parameters, ParameterTypes, themode, Type_Return));
myDLD.UnLoadDll();
return filter;
}//end else
}
catch (Exception ex) { MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; }
}//end GetFilterName
#endregion
#region Show
public static void Show(string showFile, string windowName, bool isHistogram)
{
new Show(showFile, windowName, false, isHistogram);
}//end Show
#endregion
#region ShowInfo
public static void ShowInfo(string showFile, string windowName, bool isHistogram)
{
new Show(showFile, windowName, true, isHistogram);
}//end Show
#endregion
#region applyCustomizeSetting
public static void applyCustomizeSetting(string inputFile, string outputFile, int[] values, bool isGray)
{
dld myDLD = new dld();
myDLD.LoadFun(Directory.GetCurrentDirectory() + @"\Customize.dll", "_customizeImage@16");
//回傳型態參數設定
object[] Parameters = new object[] { Marshal.StringToBSTR(inputFile), Marshal.StringToBSTR(outputFile), values, isGray }; // 參數列
Type[] ParameterTypes = new Type[] { typeof(IntPtr), typeof(IntPtr), typeof(int[]), typeof(bool) }; // 參數資料型態
dld.ModePass[] themode = new dld.ModePass[] { dld.ModePass.ByValue, dld.ModePass.ByValue, dld.ModePass.ByValue, dld.ModePass.ByValue }; // 全部傳值呼叫
Type Type_Return = typeof(bool);// 回傳型態
//回傳值判斷
bool a = (bool)myDLD.Invoke(Parameters, ParameterTypes, themode, Type_Return);
myDLD.UnLoadDll();
myDLD = null;
if (!a)
throw new Exception("使用者自訂濾鏡發生錯誤");
}//end applyCustomizeSetting
#endregion
#region customizeFilter
public string customizeFilter(string settingsFile)
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
string outputName = _tempPath + @"\" + rnd.Next() + Path.GetExtension(_sourceFileInTempName);
return insideCustomizeFilter(_resizeFileName, outputName, settingsFile);
}//end customizeFilter
#endregion
#region customizeFilterOrginalSize
public string customizeFilterOrginalSize(string inputFile, string settingsFile)
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
string outputName = _tempPath + @"\" + rnd.Next() + Path.GetExtension(_sourceFileInTempName);
if (Path.GetExtension(settingsFile) == ".dll")
return ProcessOrginalSize(inputFile, settingsFile);
else
return insideCustomizeFilter(inputFile, outputName, settingsFile);
}//end customizeFilterOrginalSize
#endregion
#region insideCustomizeFilter
public string insideCustomizeFilter(string inputFile, string outputFile, string settingsFile)
{
dld myDLD = new dld();
myDLD.LoadFun(Directory.GetCurrentDirectory() + @"\Customize.dll", "_customizeFilter@12");
//回傳型態參數設定
object[] Parameters = new object[] { Marshal.StringToBSTR(inputFile), Marshal.StringToBSTR(outputFile), Marshal.StringToBSTR(settingsFile) }; // 參數列
Type[] ParameterTypes = new Type[] { typeof(IntPtr), typeof(IntPtr), typeof(IntPtr) }; // 參數資料型態
dld.ModePass[] themode = new dld.ModePass[] { dld.ModePass.ByValue, dld.ModePass.ByValue, dld.ModePass.ByValue }; // 全部傳值呼叫
Type Type_Return = typeof(bool);// 回傳型態
//回傳值判斷
bool a = (bool)myDLD.Invoke(Parameters, ParameterTypes, themode, Type_Return);
myDLD.UnLoadDll();
myDLD = null;
if (!a)
return null;
else
return outputFile;
}//end customizeFilter
#endregion
}
}