基于C#实现Win界面调用名片识别接口.doc_第1页
基于C#实现Win界面调用名片识别接口.doc_第2页
基于C#实现Win界面调用名片识别接口.doc_第3页
基于C#实现Win界面调用名片识别接口.doc_第4页
基于C#实现Win界面调用名片识别接口.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于C#实现Winform界面调用名片识别接口本程序适用于聚合名片识别接口:/docs/api/id/139注:项目中如遇无法引用 System.Web.dll 问题,修改“项目属性”-“应用程序”-“目标框架” ,由“.NET Framework 4 Client Profile”修改为“.NET Framework 4”。然后再引用 .NET 4.0 的 System.Web.dll 即可前台界面代码:namespacemingpianshibiepartialclassForm1/Requireddesignervariable./privateSystem.ComponentModel.IContainercomponents=null;/Cleanupanyresourcesbeingused./trueifmanagedresourcesshouldbedisposed;otherwise,tectedoverridevoidDispose(booldisposing)if(disposing&(components!=null)components.Dispose();base.Dispose(disposing);#regionWindowsFormDesignergeneratedcode/RequiredmethodforDesignersupport-donotmodify/thecontentsofthismethodwiththecodeeditor./privatevoidInitializeComponent()this.open=newSystem.Windows.Forms.Button();this.pixel=newSystem.Windows.Forms.Button();this.pictureBox2=newSystem.Windows.Forms.PictureBox();this.pictureBox1=newSystem.Windows.Forms.PictureBox();this.ImgToBase64=newSystem.Windows.Forms.Button();this.button2=newSystem.Windows.Forms.Button();this.groupBox1=newSystem.Windows.Forms.GroupBox();this.label1=newSystem.Windows.Forms.Label();this.lb_show=newSystem.Windows.Forms.Label();(System.ComponentModel.ISupportInitialize)(this.pictureBox2).BeginInit();(System.ComponentModel.ISupportInitialize)(this.pictureBox1).BeginInit();this.groupBox1.SuspendLayout();this.SuspendLayout();/open/this.open.Location=newSystem.Drawing.Point(414,29);this.open.Name=open;this.open.Size=newSystem.Drawing.Size(112,29);this.open.TabIndex=0;this.open.Text=选择图片;this.open.UseVisualStyleBackColor=true;this.open.Click+=newSystem.EventHandler(this.open_Click);/pixel/this.pixel.Location=newSystem.Drawing.Point(414,92);this.pixel.Name=pixel;this.pixel.Size=newSystem.Drawing.Size(112,29);this.pixel.TabIndex=3;this.pixel.Text=灰度化处理;this.pixel.UseVisualStyleBackColor=true;this.pixel.Click+=newSystem.EventHandler(this.pixel_Click);/pictureBox2/this.pictureBox2.Location=newSystem.Drawing.Point(6,141);this.pictureBox2.Name=pictureBox2;this.pictureBox2.Size=newSystem.Drawing.Size(373,113);this.pictureBox2.TabIndex=5;this.pictureBox2.TabStop=false;/pictureBox1/this.pictureBox1.Location=newSystem.Drawing.Point(6,17);this.pictureBox1.Name=pictureBox1;this.pictureBox1.Size=newSystem.Drawing.Size(373,110);this.pictureBox1.TabIndex=4;this.pictureBox1.TabStop=false;/ImgToBase64/this.ImgToBase64.Location=newSystem.Drawing.Point(414,153);this.ImgToBase64.Name=ImgToBase64;this.ImgToBase64.Size=newSystem.Drawing.Size(112,29);this.ImgToBase64.TabIndex=6;this.ImgToBase64.Text=图片Base64转换;this.ImgToBase64.UseVisualStyleBackColor=true;this.ImgToBase64.Click+=newSystem.EventHandler(this.ImgToBase64_Click);/button2/this.button2.Location=newSystem.Drawing.Point(414,216);this.button2.Name=button2;this.button2.Size=newSystem.Drawing.Size(112,29);this.button2.TabIndex=7;this.button2.Text=名片识别;this.button2.UseVisualStyleBackColor=true;this.button2.Click+=newSystem.EventHandler(this.button2_Click_1);/groupBox1/this.groupBox1.BackColor=System.Drawing.SystemColors.Control;this.groupBox1.Controls.Add(this.pictureBox1);this.groupBox1.Controls.Add(this.pictureBox2);this.groupBox1.Location=newSystem.Drawing.Point(12,12);this.groupBox1.Name=groupBox1;this.groupBox1.Size=newSystem.Drawing.Size(396,260);this.groupBox1.TabIndex=8;this.groupBox1.TabStop=false;this.groupBox1.Text=图片展示;/label1/this.label1.AutoSize=true;this.label1.Location=newSystem.Drawing.Point(16,299);this.label1.Name=label1;this.label1.Size=newSystem.Drawing.Size(65,12);this.label1.TabIndex=9;this.label1.Text=识别结果:;/lb_show/this.lb_show.AutoSize=true;this.lb_show.Location=newSystem.Drawing.Point(106,299);this.lb_show.Name=lb_show;this.lb_show.Size=newSystem.Drawing.Size(0,12);this.lb_show.TabIndex=10;/Form1/this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.ClientSize=newSystem.Drawing.Size(553,348);this.Controls.Add(this.lb_show);this.Controls.Add(this.label1);this.Controls.Add(this.groupBox1);this.Controls.Add(this.button2);this.Controls.Add(this.ImgToBase64);this.Controls.Add(this.pixel);this.Controls.Add(this.open);this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.FixedToolWindow;this.Name=Form1;this.Text=名片识别;(System.ComponentModel.ISupportInitialize)(this.pictureBox2).EndInit();(System.ComponentModel.ISupportInitialize)(this.pictureBox1).EndInit();this.groupBox1.ResumeLayout(false);this.ResumeLayout(false);this.PerformLayout();#endregionprivateSystem.Windows.Forms.Buttonopen;privateSystem.Windows.Forms.Buttonpixel;privateSystem.Windows.Forms.PictureBoxpictureBox2;privateSystem.Windows.Forms.PictureBoxpictureBox1;privateSystem.Windows.Forms.ButtonImgToBase64;privateSystem.Windows.Forms.Buttonbutton2;privateSystem.Windows.Forms.GroupBoxgroupBox1;privateSystem.Windows.Forms.Labellabel1;privateSystem.Windows.Forms.Labellb_show;后台代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Net;usingSystem.Net.Security;usingSystem.Web;usingSystem.Security.Cryptography.X509Certificates;usingSystem.Diagnostics;namespacemingpianshibiepublicpartialclassForm1:FormBitmapcurBitmap;Bitmapbitmap;Stringstrbaser64;publicForm1()InitializeComponent();/图片转base64/privatevoidImgToBase64_Click(objectsender,EventArgse)OpenFileDialogdlg=newOpenFileDialog();dlg.Multiselect=true;dlg.Title=选择要转换的图片;dlg.Filter=Imagefiles(*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles(*.*)|*.*;if(DialogResult.OK=dlg.ShowDialog()for(inti=0;idlg.FileNames.Length;i+)ImgToBase64String(dlg.FileNamesi.ToString();/选择图片/privatevoidopen_Click(objectsender,EventArgse)OpenFileDialogopnDlg=newOpenFileDialog();opnDlg.Filter=所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;+*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|+位图(*.bmp;*.jpg;*.png;.)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|+矢量图(*.wmf;*.eps;*.emf;.)|*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf;opnDlg.Title=打开图像文件;opnDlg.ShowHelp=true;if(opnDlg.ShowDialog()=DialogResult.OK)stringcurFileName=opnDlg.FileName;try/使用Image.FromFile创建图像对象curBitmap=(Bitmap)Image.FromFile(curFileName);bitmap=curBitmap;pictureBox1.Image=curBitmap;catch(Exceptionexp)MessageBox.Show(exp.Message);/对窗体进行重新绘制Invalidate();/提取灰度法/为了将位图的颜色设置为灰度或其他颜色,需要使用GetPixel来读取当前像素的颜色-计算灰度值-使用SetPixel应用新的颜色/privatevoidpixel_Click(objectsender,EventArgse)if(bitmap!=null)ColorcurColor;intret;/二维图像数组循环for(inti=0;ibitmap.Width;i+)for(intj=0;jbitmap.Height;j+)/读取当前像素的RGB颜色值curColor=bitmap.GetPixel(i,j);/利用公式计算灰度值(加权平均法)ret=(int)(curColor.R*0.299+curColor.G*0.587+curColor.B*0.114);/设置该点像素的灰度值,R=G=B=retbitmap.SetPixel(i,j,Color.FromArgb(ret,ret,ret);this.pictureBox2.Image=bitmap;/另存灰度处理后的图片bitmap.Save(Environment.CurrentDirectory+/+System.DateTime.Now.Second.ToString()+.jpg);MessageBox.Show(处理成功!);/对窗体进行重新绘制Invalidate();elseMessageBox.Show(请选择图片!,消息);/图片转为base64编码的文本/privatevoidImgToBase64String(stringImagefilename)tryBitmapbmp=newBitmap(Imagefilename);this.pictureBox1.Image=bmp;/保存图片转Base64后的txt文件FileStreamfs=newFileStream(Imagefilename+.base64.txt,FileMode.Create);StreamWritersw=newStreamWriter(fs);MemoryStreamms=newMemoryStream();bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);bytearr=newbytems.Length;ms.Position=0;ms.Read(arr,0,(int)ms.Length);ms.Close();strbaser64=Convert.ToBase64String(arr);sw.Write(strbaser64);sw.Close();fs.Close();MessageBox.Show(转换成功!);catch(Exceptionex)MessageBox.Show(ImgToBase64String转换失败nException:+ex.Message);/名片识别/privatevoidbutton2_Click_1(objectsender,EventArgse)if(strbaser64!=null)/接口地址stringurl=/hanvon/bcard/query;IDictionaryParem=newDictionary();/参数imageParem.Add(image,strbaser64);/参数keyParem.Add(key,申请的key);/调用post请求stringresult=DoPost(url,Parem);lb_show.Text=result;MessageBox.Show(result);Debug.WriteLine(识别结果:+result);elseMessageBox.Show(请先将图片Base64处理!,消息);/执行HTTPPOST请求。/请求地址/请求参数/HTTP响应publicstringDoPost(stringurl,IDictionaryparameters)HttpWebRequestreq=null;HttpWebResponsersp=null;System.IO.StreamreqStream=null;tryreq=GetWebRequest(url,POST);req.ContentType=application/x-www-form-urlencoded;charset=utf-8;bytepostData=Encoding.UTF8.GetBytes(BuildQuery(parameters,utf8);reqStream=req.GetRequestStream();reqStream.Write(postData,0,postData.Length);rsp=(HttpWebResponse)req.GetResponse();Encodingencoding=Encoding.GetEncoding(rsp.CharacterSet);returnGetResponseAsString(rsp,encoding);catch(Exceptionex)returnex.Message;finallyif(reqStream!=null)reqStream.Close();if(rsp!=null)rsp.Close();/创建http或https请求/publicHttpWebRequestGetWebRequest(stringurl,stringmethod)HttpWebRequestreq=null;if(url.Contains(https)/验证服务器证书回调ServicePointManager.ServerCertificateValidationCallback=newRemoteCertificateValidationCallback(CheckValidationResult);req=(HttpWebRequest)WebRequest.CreateDefault(newUri(url);elsereq=(HttpWebRequest)WebRequest.Create(url);req.ServicePoint.Expect100Continue=false;req.ServicePoint.ConnectionLimit=1000;req.Method=method;req.KeepAlive=false;req.ProtocolVersion=HttpVersion.Version10;req.UserAgent=Top4Net;req.Timeout=5000;returnreq;/组装普通文本请求参数。/Key-Value形式请求参数字典/URL编码后的请求数据publicstaticstringBuildQuery(IDictionaryparameters,stringencode)StringBuilderpostData=newStringBuilder();boolh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论