贴代码:

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 "未知";
}
}
}
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImgCodeToStrTest
{
class Program
{

static List<int> listHeight = new List<int>();
static List<int> listCount = new List<int>();
static List<string> listWord = new List<string>();
static List<string> listHS = new List<string>();


static void Main(string[] args)
{
string name = "8";
string path = @"C:\Users\gx_143\Desktop\img\imgCut\" + name + ".jpg";
string str = ImgCodeToStrHelper.ImgToBase64(path);
string code = ImgCodeToStrHelper.ImgCodeToStr(str);
Bitmap bmp = new Bitmap(path);
JObject jObject = (JObject)JsonConvert.DeserializeObject(code);
int length = jObject["prism_wordsInfo"].Count();
int firstX = int.Parse(jObject["prism_wordsInfo"][0]["pos"][0]["x"].ToString()),
XXX = int.Parse(jObject["prism_wordsInfo"][length-1]["pos"][0]["x"].ToString()),
firstY =0;
for (int i = 0; i < length; i++)
{
string word = jObject["prism_wordsInfo"][i]["word"].ToString();

firstY = int.Parse(jObject["prism_wordsInfo"][i]["pos"][0]["y"].ToString());

//均值计算
listHeight.Add(firstY);
listWord.Add(word);
}

int avgLine = reMaxMin(listHeight) + 80;

Bitmap newBmp = new Bitmap(XXX-firstX, 50);
newBmp = ImgCodeToStrHelper.bmpCut(bmp, newBmp, firstX, avgLine, newBmp.Width, newBmp.Height);
newBmp.Save(@"C:\Users\gx_143\Desktop\img\imgCut\" + name+"-11" + ".jpg");

int count=0,redCount = 0, blackCount = 0, t = 0 ;
char RorB = ' ';
bool falg = false;
Color pixel;
for (int i = 0; i < newBmp.Width; i++)
{
falg = false;
for (int j = 0; j < newBmp.Height; j++)
{
pixel = newBmp.GetPixel(i, j);//获取当前像素的值
if (pixel.R > 165 && pixel.R < 190)
{
redCount++;
falg = true;
}
else if (pixel.R > 5 && pixel.R < 60)
{
blackCount++;
falg = true;
}
}
if (falg==false && redCount + blackCount > 30)
{
if(t % 2 == 0)
{
if (redCount > blackCount)
{
RorB = 'R';
count = redCount;
}
else
{
RorB = 'B';
count = blackCount;
}
string hs = ImgCodeToStrHelper.bmpSumToAction(count, RorB);
listHS.Add(hs);
listCount.Add(count);
}
t++;
falg = false; count = 0; redCount = 0; blackCount = 0;
}
}

for (int i = 0; i < listCount.Count; i++)
{
Console.WriteLine(listWord[i] + "---" + listHS[i] + "---" + listCount[i]);
}

Console.ReadKey();
}

/// <summary>
/// 矫正X轴坐标
/// </summary>
/// <param name="listX"></param>
/// <returns></returns>
//private static List<int> Correct(List<int> listX)
//{
// List<int> arr = new List<int>();
// for (int i = 0; i < listX.Count -1 ; i++)
// {
// arr.Add( listX[i + 1] - listX[i]);
// }
// int difference = reMaxMin(arr);
// for (int i = 1; i < listX.Count; i++)
// {
// //if (listX[i] - listX[i - 1] > difference - 3 || listX[i] - listX[i - 1] < difference + 3)
// {
// listX[i] = listX[i - 1] + difference;
// }
// }
// return listX;
//}

private static int reMaxMin(List<int> list)
{
int min = list.Min();
int max = list.Max();
int sum = list.Sum() - min - max;
return sum / (list.Count - 2);
}
/// <summary>
/// 扣字
/// </summary>
//private static Bitmap fun(Bitmap bmp, string word, int avgLine, int width, int x, int height = 50)
//{
// Bitmap newBmp = new Bitmap(width, height);
// newBmp = ImgCodeToStrHelper.bmpCut(bmp, newBmp, x, avgLine, width, height);
// //newBmp.Save(@"C:\Users\gx_143\Desktop\img\imgCut\" + word +"-" + Guid.NewGuid().ToString() + ".jpg");
// return newBmp;
//}
}
}

可以进行图片的分割,文字的识别!