0
点赞
收藏
分享

微信扫一扫

C#Winfrom和PYTHON接入腾讯云OCR


程序基本目的是实现提取图片中的文字。做了一个winfrom界面,接了腾讯云的OCR图片识别。

这里接入文档就不给出了,想看的直接去腾讯云看接入文档即可。

由于想做跨平台的应用。分析了接入文档,并且之前想做一个web界面接口的,但是无奈javascript技术不到家,表单传递时的header头问题迟迟没有解决。

所有就在我的网站里弄了一个接口来计算我的Authorization,计算方法是直接采用腾讯给出的标准文档中的方法。直接把计算结果echo到web上。

而我windows上采用的是C#编程。主要想法就是先使用HTTPREQUEST方法做一个简单的爬虫。具体方法在我之前的博客《C#使用Winfrom编程时的注意事项》有讲过到。

主要还是卡在了header传值的问题。由于Authorization不是标准Header值,需要采用别的方式来进行传递。这是比较需要注意的。

这个地方我主要采用了Content-Type=application/x-www-form-urlencoded,用POST方法把上传文件到腾讯云的API接口,然后接受返回值而已。

接受的时候也要注意,由于收到的信息是JSON码的形式,需要自己下载一个Newtonsoft.Json.dll的文件,并加入解决方案的引用当中。然后命名空间引用

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

这样就可以直接使用JObject对象来转换string型的json对象。并采用json标准来进行对象的访问。

C# Winfrom的界面就不具体放出,我先给出C#的代码。

Windows下的代码

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PDF转WORD
{

public partial class Form2 : Form
{
private string filename="";
public Form2()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
this.Text = "图片转PDF";
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "请选择文件";
fileDialog.Filter = "(*jpg*)|*.jpg*||(*.jpg*)||*.*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string file = fileDialog.FileName;
filename = file;
label2.Text = filename.ToString();
MessageBox.Show("选择成功!");
}
}

private void POST_DATA() {
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(@"我的web服务器"));
req.Method = "POST";
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0";
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "application/json";
string result1 = "";

StringBuilder builder = new StringBuilder();

byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{ reqStream.Write(data, 0, data.Length); reqStream.Close(); }
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{ result1 = reader.ReadToEnd(); }

HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://recognition.image.myqcloud.com/ocr/general"));
Stream memStream = new MemoryStream();
webReq.Method = "POST";
string boundary = "--------------" + DateTime.Now.Ticks.ToString("x");// 边界符
webReq.ContentType = "multipart/form-data; boundary=" + boundary;
byte[] enter = Encoding.ASCII.GetBytes("\r\n"); //换行
memStream.Write(enter, 0, enter.Length);
Dictionary<string, string> dic = new Dictionary<string, string>()
{
{"appid","你的APPID"}
};
//写入文本字段
string inputPartHeaderFormat = "--" + boundary + "\r\n" + "Content-Disposition:form-data;name=\"{0}\";" + "\r\n\r\n{1}\r\n";
foreach (var kv in dic)
{
string inputPartHeader = string.Format(inputPartHeaderFormat, kv.Key, kv.Value);
var inputPartHeaderBytes = Encoding.ASCII.GetBytes(inputPartHeader);
memStream.Write(inputPartHeaderBytes, 0, inputPartHeaderBytes.Length);
}

var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
// 写入文件
string imagePartHeader = "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: image/jpeg\r\n\r\n";
var header = string.Format(imagePartHeader, "image", "1.jpg");
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
var buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
byte[] endBoundary = Encoding.ASCII.GetBytes("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "\r\n" + boundary + "--\r\n");
memStream.Write(endBoundary, 0, endBoundary.Length);
webReq.ContentLength = memStream.Length;
webReq.Headers.Add(HttpRequestHeader.Authorization, result1);
webReq.Host = "recognition.image.myqcloud.com";
var requestStream = webReq.GetRequestStream();
memStream.Position = 0;
memStream.CopyTo(requestStream);
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
var ret = sr.ReadToEnd();
sr.Close();
response.Close();
requestStream.Close();
memStream.Close();
JObject content_info = JObject.Parse(ret);
//这里就是主要的对返回的json的处理了
for (int i = 0; i < content_info["data"]["items"].Count(); i++)
{
try
{
richTextBox1.AppendText(content_info["data"]["items"][i]["itemstring"].ToString());
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}

private void button2_Click(object sender, EventArgs e)
{
if(filename!="")
{
Thread th1 = new Thread(POST_DATA);
th1.IsBackground = true;
th1.Start();
}
else {
MessageBox.Show("请先选择图片。", "提示!");
}
}

private void button3_Click(object sender, EventArgs e)
{
filename = "";
if (filename == "")
{
label2.Text = "暂未选择图片";
MessageBox.Show("清空成功!");
}
}

private void button4_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
}
}

以上就是主要界面的代码。而主界面的代码就不再放出,我先给出windows下的实验效果。(照片带水印)

Windows下的效果

C#Winfrom和PYTHON接入腾讯云OCR_爬虫

C#Winfrom和PYTHON接入腾讯云OCR_.NET_02

LINUX上的代码实现(ubuntu)

用python实现的,所以代码比较简洁。主要用到的函数库为requests

没什么需要注意的==毕竟python比较简单。直接安装官方给的文档设置image和header就可以了。话不多说直接上代码QWQ

#!/usr/bin/env python
#encoding=utf-8
import re
import sys,os
import requests
def GetAuthorization():
html = requests.post(url="我的WEB服务器地址")
html = html.text
info = html.split("\n")
return info[1].replace("\n","")

Authorization = str(GetAuthorization().replace("\n","").replace("\r",""))
def createRequest(filepath):
global Authorization
headers = {
'host':'recognition.image.myqcloud.com',
'Authorization':Authorization
}
files = {
'appid':(None,'你的appid'),
'image':('1.jpg',open(filepath,'rb'),'image/jpeg')
}

r = requests.post("http://recognition.image.myqcloud.com/ocr/general", files=files,headers=headers)
responseinfo = r.content
data = responseinfo.decode('utf-8')
print data
r_index = r'itemstring":"(.*?)"'
result = re.findall(r_index, data)
for i in result:
print(i)

def main():
createRequest(sys.argv[1])


if __name__ == '__main__':
main()

程序的第一个参数就是你要上传文件的绝对路径==就是一个普通脚本,没有什么很大的技术难度。

我要处理的照片还是上面那张,现在给出扫描结果。

LINUX下的效果图

C#Winfrom和PYTHON接入腾讯云OCR_.NET_03



举报

相关推荐

0 条评论