C#驗證兩個QQ頭像相似度的示例代碼_第1頁
C#驗證兩個QQ頭像相似度的示例代碼_第2頁
C#驗證兩個QQ頭像相似度的示例代碼_第3頁
C#驗證兩個QQ頭像相似度的示例代碼_第4頁
C#驗證兩個QQ頭像相似度的示例代碼_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第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號,點擊查看,點擊驗證,得出結(jié)果。";

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="相同像素個數(shù):"+countSame+",不同像素個數(shù):"+countDifferent+"用時:"+stopwatch.ElapsedMilliseconds+"毫秒";

//byte[]轉(zhuǎn)圖片

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();這兩句都可以,至于區(qū)別么,下面有解釋

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);

//設(shè)置畫布的描繪質(zhì)量

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();

//以下代碼為保存圖片時,設(shè)置壓縮質(zhì)量

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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論