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
| using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks;
namespace ImgCodeToStrTest { public class ImgCodeToStrHelper { private const String host = "https://ocrapi-document.taobao.com"; private const String path = "/ocrservice/document"; private const String method = "POST"; private const String appcode = "xxxx";
public static string ImgCodeToStr(string base64) { String querys = ""; String bodys = "{\"img\":\" "+ base64 + "\",\"url\":\"\",\"prob\":false}"; String url = host + path; HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null;
if (0 < querys.Length) { url = url + "?" + querys; }
if (host.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = method; httpRequest.Headers.Add("Authorization", "APPCODE " + appcode); //根据API的要求,定义相对应的Content-Type httpRequest.ContentType = "application/json; charset=UTF-8"; if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; } Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); return reader.ReadToEnd(); } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } public static string ImgToBase64(string filePath) { //用streamread读这个文件 System.IO.StreamReader sr = new StreamReader(filePath, Encoding.Default, true);
int index; //实例化一个内存流 System.IO.MemoryStream tempStream = new MemoryStream(); //将流转换为字节数组 while ((index = sr.BaseStream.ReadByte()) != -1) { tempStream.WriteByte(((byte)index)); } byte[] array = tempStream.ToArray(); tempStream.Close(); //将得到的字节数组转换为base64位编码 string result = Convert.ToBase64String(array); return result; }
/// <summary> /// 计算bitmap某一区域内argb的数量 /// </summary> /// <param name="bitmap">图片</param> /// <param name="argb">rgb值</param> /// <param name="point">起始点</param> /// <param name="width">横向宽度</param> /// <param name="height">纵向宽度</param> /// <returns>rgb像素点的数量</returns> public static int bmpSum(Bitmap bitmap, int argb, Point point, int width = 50, int height = 50) { int count = 0, x, y; Color pixel; for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { pixel = bitmap.GetPixel(x + point.X, y + point.Y);//获取当前像素的值 if (pixel.ToArgb() == argb) { count++; } } } return count; }
public static int bmpSum(Bitmap bitmap,out char RroB) { int redCount = 0,blackCount=0, x, y; Color pixel; for (x = 0; x < bitmap.Width; x++) { for (y = 0; y < bitmap.Height; y++) { pixel = bitmap.GetPixel(x , y);//获取当前像素的值 if (pixel.R > 165 && pixel.R < 190) { redCount++; } else if (pixel.R > 5 && pixel.R < 60) { blackCount++; } } } if (redCount> blackCount) { RroB = 'R'; return redCount; } else { RroB = 'B'; return blackCount; } }
/// <summary> /// 对bitmap进行剪裁 /// </summary> /// <param name="bitmap">图片</param> /// <param name="point">剪裁的起点</param> /// <param name="width">剪裁的宽度</param> /// <param name="height">剪裁的高度</param> /// <returns>剪裁后的图片</returns> public static Bitmap bmpCut(Bitmap bitmap, Bitmap newbmp,int x, int y, int width, int height) { Color pixel; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { pixel = bitmap.GetPixel(x + i, y + j); newbmp.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, pixel.B)); } } return newbmp; }
public static Bitmap bmpCutSum(Bitmap bitmap, Bitmap newbmp, Point point, int width, int height) { //Bitmap newbmp = new Bitmap(width, height); int x, y; Color pixel; for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { pixel = bitmap.GetPixel(x + point.X, y + point.Y); newbmp.SetPixel(x , y, Color.FromArgb(pixel.R, pixel.G, pixel.B)); } } return newbmp; }
/// <summary> /// 阈值设置 /// </summary> /// <param name="sum"></param> /// <returns></returns> public static string bmpSumToAction(int sum, char RorB) { if (sum >= 705 && RorB == 'B') { return "梅花"; } if (sum < 705 && RorB == 'B') { return "黑桃"; }
if (sum >= 630 && RorB == 'R') { return "红桃"; } if (sum < 630 && RorB == 'R') { return "方块"; } return "未知"; } } }
|