C#验证两个QQ头像相似度的示例代码_第1页
C#验证两个QQ头像相似度的示例代码_第2页
C#验证两个QQ头像相似度的示例代码_第3页
C#验证两个QQ头像相似度的示例代码_第4页
C#验证两个QQ头像相似度的示例代码_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

第C#验证两个QQ头像相似度的示例代码利用c#查看出某个其他qq的头像与自己头像的相似度,先看效果图

这里我是将左边的头像作为比对的基本图,我目前做的是一图比对一图,因为理解好了一对一,一对多也不难,我们可以得出相似的像素,然后大于多少百分比就是同一图的改变了,以下是完整代码

usingNewtonsoft.Json;

usingNewtonsoft.Json.Linq;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Diagnostics;

usingSystem.Drawing;

usingSystem.Drawing.Drawing2D;

usingSystem.Drawing.Imaging;

usingSystem.IO;

usingSystem.Linq;

usingSystem.Net;

usingSystem.Net.Http;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

namespaceWindowsFormsApp1

publicpartialclassForm1:Form

publicForm1()

InitializeComponent();

publicstaticintwidth;//图片宽

publicstaticintheight;//图片高

publicstaticstringmypicurl;//我的图片地址

publicstaticstringpicurl;//图片地址

privatevoidForm1_Load(objectsender,EventArgse)

this.MyPicture.SizeMode=PictureBoxSizeMode.StretchImage;

this.MyPicture.BorderStyle=BorderStyle.FixedSingle;

this.OtherPicture.SizeMode=PictureBoxSizeMode.StretchImage;

this.OtherPicture.BorderStyle=BorderStyle.FixedSingle;

this.explain.Text="操作步骤:左边输入自己qq号查看显示,右边输入别人qq号,点击查看,点击验证,得出结果。";

privatevoidbutton2_Click(objectsender,EventArgse)

Stopwatchstopwatch=newStopwatch();

stopwatch.Start();

intcountSame=0;

intcountDifferent=0;

Imageimg=this.MyPicture.Image;

BitmapbitmapSource=newBitmap(img);

//BitmapbitmapSource=BytesToBitmap(ResizeImage(mypicurl));

width=bitmapSource.Width;

height=bitmapSource.Height;

BitmapbitmapTarget=BytesToBitmap(ResizeImage(picurl));

//照片尺寸必须一样

for(inti=0;ibitmapTarget.Width;i++)

for(intj=0;jbitmapTarget.Height;j++)

if(bitmapSource.GetPixel(i,j).Equals(bitmapTarget.GetPixel(i,j)))

countSame++;

else

countDifferent++;

stopwatch.Stop();

this.result.Text="相同像素个数:"+countSame+",不同像素个数:"+countDifferent+"用时:"+stopwatch.ElapsedMilliseconds+"毫秒";

//byte[]转图片

publicstaticBitmapBytesToBitmap(byte[]Bytes)

MemoryStreamstream=null;

stream=newMemoryStream(Bytes);

returnnewBitmap((Image)newBitmap(stream));

catch(ArgumentNullExceptionex)

throwex;

catch(ArgumentExceptionex)

throwex;

finally

stream.Close();

///summary

///图片大小裁剪

////summary

///paramname="filePath"/param

///returns/returns

publicstaticbyte[]ResizeImage(stringfilePath)

WebRequestrequest=(WebRequest)HttpWebRequest.Create(filePath);

WebResponseresponse=request.GetResponse();

using(Streamstream=response.GetResponseStream())

Bitmapbm=(Bitmap)Image.FromStream(stream);

bm=GetThumbnail(bm,height,width);

MemoryStreamms=newMemoryStream();

bm.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);

byte[]bytes=ms.GetBuffer();//byte[]bytes=ms.ToArray();这两句都可以,至于区别么,下面有解释

ms.Close();

returnbytes;

///summary

///修改图片的大小

////summary

///paramname="b"/param

///paramname="destHeight"/param

///paramname="destWidth"/param

///returns/returns

publicstaticBitmapGetThumbnail(Bitmapb,intdestHeight,intdestWidth)

System.Drawing.ImageimgSource=b;

System.Drawing.Imaging.ImageFormatthisFormat=imgSource.RawFormat;

intsW=0,sH=0;

//按比例缩放

intsWidth=imgSource.Width;

intsHeight=imgSource.Height;

if(sHeightdestHeight||sWidthdestWidth)

if((sWidth*destHeight)(sHeight*destWidth))

sW=destWidth;

sH=(destWidth*sHeight)/sWidth;

else

sH=destHeight;

sW=(sWidth*destHeight)/sHeight;

else

sW=sWidth;

sH=sHeight;

BitmapoutBmp=newBitmap(destWidth,destHeight);

Graphicsg=Graphics.FromImage(outBmp);

g.Clear(Color.Transparent);

//设置画布的描绘质量

g.CompositingQuality=CompositingQuality.HighQuality;

g.SmoothingMode=SmoothingMode.HighQuality;

g.InterpolationMode=InterpolationMode.HighQualityBicubic;

g.DrawImage(imgSource,newRectangle((destWidth-sW)/2,(destHeight-sH)/2,sW,sH),0,0,imgSource.Width,imgSource.Height,GraphicsUnit.Pixel);

g.Dispose();

//以下代码为保存图片时,设置压缩质量

EncoderParametersencoderParams=newEncoderParameters();

long[]quality=newlong[1];

quality[0]=100;

EncoderParameterencoderParam=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);

encoderParams.Param[0]=encoderParam;

imgSource.Dispose();

returnoutBmp;

privatevoidbutton3_Click(objectsender,EventArgse)

if(this.OtherQQ.Text=="")

MessageBox.Show("请输入qq号!");

return;

HttpClienthttpClient=newHttpClient();

stringurl="/qq/"+this.OtherQQ.Text;

varrsp=httpClient.GetAsync(url).Result;

varstr=rsp.Content.ReadAsStringAsync().Result;

JObjectjo=(JObject)JsonConvert.DeserializeObject(str);

if((string)jo["code"]=="200")

Imagepic=Image.FromStream(WebRequest.Create((string)jo["data"]["avatar"]).GetResponse().GetResponseStream());

this.OtherPicture.Image=pic;

picurl=(string)jo["data"]["avatar"];

else

MessageBox.Show("请输入正确的qq号!");

privatevoidbutton4_Click(objectsender,EventArgse)

if(this.MyQQ.Text=="")

MessageBox.Show("请输入qq号!");

return;

HttpClienthttpClient=newHttpClient();

stringurl="/qq/"+this.MyQQ.Text;

varrsp=httpClient.GetAsync(url).Result;

varstr=rsp.Content.ReadAsStringAsync().Result;

JObjectjo=(JObject)JsonConvert.Des

温馨提示

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

评论

0/150

提交评论