领胜LDS 键盘AOI检测项目
xcd
2020-07-04 0e0908cbbc6354b4df435bf0837e540609ec0118
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
 
namespace Bro.Common.Helper
{
    public static class StaticHelper
    {
        /// <summary>
        /// 数值转换为byte数组 高位在前,低位在后
        /// </summary>
        /// <param name="number"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static byte[] IntToBytes(this int number, int size = 2)
        {
            byte[] result = new byte[size];
 
            int temp = size;
            while (temp > 0)
            {
                result[size - temp] = (byte)(number >> ((temp - 1) * 8) & 0xff);
 
                temp--;
            }
 
            return result;
        }
 
        /// <summary>
        /// 字节数组转换为整数
        /// </summary>
        /// <param name="data">字节数组</param>
        /// <param name="HtL">true:数组序号低的在高位 false:数组序号低的在低位</param>
        /// <returns></returns>
        public static int BytesToInt(this byte[] data, bool HtL = true)
        {
            int res = 0;
 
            for (int i = 0; i < data.Length; i++)
            {
                int index = i;
 
                if (HtL)
                {
                    index = data.Length - 1 - i;
                }
 
                res += data[index] << (8 * i);
            }
 
            return res;
        }
 
        /// <summary>
        /// 将32位整形拆分为无符号16位整形
        /// </summary>
        /// <param name="num">需要拆分的32位整形</param>
        /// <param name="bitNum">拆分为16位整形的位数 1或者2</param>
        /// <param name="HtL">true:高位在前,低位在后;false:高位在后,低位在前</param>
        /// <returns></returns>
        public static List<ushort> ParseIntToUnsignShortList(this int num, int bitNum = 2, bool HtL = false)
        {
            if (bitNum == 2)
            {
                ushort high = (ushort)(num >> 16);
                ushort low = (ushort)num;
 
                if (HtL)
                {
                    return new List<ushort>() { high, low };
                }
                else
                {
                    return new List<ushort>() { low, high };
                }
            }
            else
            {
                if (num < 0)
                {
                    num = ushort.MaxValue + 1 + num;
                }
 
                return new List<ushort>() { (ushort)num };
            }
        }
 
        /// <summary>
        /// 将32位整形数组拆分为无符号16位整形数组
        /// </summary>
        /// <param name="list">需要拆分的32位整形</param>
        /// <param name="bitNum">拆分为16位整形的位数 1或者2</param>
        /// <param name="HtL">true:高位在前,低位在后;false:高位在后,低位在前</param>
        /// <returns></returns>
        public static List<ushort> ParseIntToUnsignShortList(this List<int> list, int bitNum = 2, bool HtL = false)
        {
            return list.SelectMany(u => u.ParseIntToUnsignShortList(bitNum, HtL)).ToList();
        }
 
        /// <summary>
        /// 将ushort的集合转换为16位带符号整形
        /// </summary>
        /// <param name="numList"></param>
        /// <param name="bitNum">合并的位数 1或者2</param>
        /// <param name="HtL">true:高位在前,低位在后;false:高位在后,低位在前</param>
        /// <returns></returns>
        public static List<int> ParseUnsignShortListToInt(this List<int> numList, int bitNum = 2, bool HtL = false)
        {
            if (bitNum == 1)
            {
                return numList.ConvertAll(n =>
                {
                    int num = n;
                    if (num > short.MaxValue)
                    {
                        num = num - ushort.MaxValue - 1;
                    }
 
                    return num;
                });
            }
            else
            {
                List<int> list = new List<int>();
                for (int i = 0; i < numList.Count; i += 2)
                {
                    int high = HtL ? numList[i] : numList[i + 1];
                    int low = HtL ? numList[i + 1] : numList[i];
                    list.Add((high << 16) | low);
                }
 
                return list;
            }
        }
 
        public static string GetExceptionMessage(this Exception ex)
        {
            string msg = "异常信息:" + ex.Message;
            if (ex.InnerException != null)
            {
                msg += ";\r\n内部异常信息:" + ex.InnerException.GetExceptionMessage();
            }
 
            msg += (";\r\nStackTrace:" + ex.StackTrace);
 
            return msg;
        }
 
        public static T DeepSerializeClone<T>(this T t) 
        {
            return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t));
        }
 
        public static void DataFrom<T1, T2>(this T1 destT, T2 sourceT, List<string> exceptionProps = null) where T1 : class where T2 : class
        {
            if (sourceT == null)
            {
                destT = null;
                return;
            }
 
            PropertyInfo[] propDest = destT.GetType().GetProperties();//.Where(p => !(p.GetMethod.IsVirtual && !p.GetMethod.IsFinal)).ToArray();
            PropertyInfo[] propSource = sourceT.GetType().GetProperties();
 
            Array.ForEach(propDest, prop =>
            {
                if (exceptionProps == null || !exceptionProps.Contains(prop.Name))
                {
                    if (prop.CanWrite)
                    {
                        PropertyInfo propS = propSource.FirstOrDefault(p => p.Name == prop.Name);
                        if (propS != null && propS.CanRead)
                        {
                            prop.SetValue(destT, propS.GetValue(sourceT));
                        }
                    }
                }
            });
        }
 
        public static Bitmap BitmapSerializeCopy(this Bitmap map)
        {
            Bitmap image = null;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, map);
                ms.Seek(0, SeekOrigin.Begin);
                image = (Bitmap)bf.Deserialize(ms);
 
                //ms.Close();
            }
 
            return image;
 
            //Bitmap image = new Bitmap(map.Width, map.Height);
 
            //int bitdepth_per_pixel = Bitmap.GetPixelFormatSize(map.PixelFormat) / 8;
            //BitmapData source_bitmapdata = null;
            //BitmapData destination_bitmapdata = null;
 
            //try
            //{
            //    source_bitmapdata = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadWrite, map.PixelFormat);
            //    destination_bitmapdata = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);
 
            //    int source_bitmapdata_bitdepth_width = source_bitmapdata.Width * bitdepth_per_pixel;
            //    int source_bitmapdata_height = source_bitmapdata.Height;
            //    int source_bitmapdata_bitdepth_stride = source_bitmapdata.Stride;
 
            //    unsafe
            //    {
            //        byte* source_ptr = (byte*)source_bitmapdata.Scan0;
            //        byte* destination_ptr = (byte*)destination_bitmapdata.Scan0;
 
            //        int offset = source_bitmapdata_bitdepth_stride - source_bitmapdata_bitdepth_width;
 
            //        for (int i = 0; i < source_bitmapdata_height; i++)
            //        {
            //            for (int j = 0; j < source_bitmapdata_bitdepth_width; j++, source_ptr++, destination_ptr++)
            //            {
            //                *destination_ptr = *source_ptr;
            //            }
 
            //            source_ptr += offset;
            //            destination_ptr += offset;
            //        }
            //    }
 
            //    map.UnlockBits(source_bitmapdata);
            //    image.UnlockBits(destination_bitmapdata);
            //}
            //catch { }
 
            //return image;
        }
 
        public static void Sort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
        {
            List<T> sortedList = collection.OrderByDescending(x => x).ToList();//这里用降序 
            for (int i = 0; i < sortedList.Count(); i++)
            {
                collection.Move(collection.IndexOf(sortedList[i]), i);
            }
        }
 
        // 布尔类型转换为整型
        public static int ToInt(this object obj)
        {
            if (Convert.ToBoolean(obj) == true)
                return 1;
            else
                return 0;
        }
 
        // 整型转换为布尔类型
        public static bool ToBool(this object obj)
        {
            if (Convert.ToInt32(obj) == 1)
                return true;
            else
                return false;
        }
    }
}