Quantcast
Viewing all articles
Browse latest Browse all 8899

C#でQRコードをPythonで読み込みクリップボードする。

C#でバーコードリーダーを作成する。

C#でスクリーンショットを取得して、pyzbarで複数行のQRコードのデコードを行いクリップボードにコピーする。

usingSystem;usingSystem.IO;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Windows.Forms;usingSystem.Diagnostics;namespacehash_qrcode_decoder{classProgram{staticstringtmpfile=@"C:\~\qrcode.jpg";[STAThread]staticvoidMain(string[]args){varpythonScriptPath=@"C:\~\qrcode-decoder.py";varpythonInterpreterPath=@"C:\Users\~\Anaconda3\envs\py36\python.exe";vararguments=newList<string>{pythonScriptPath};Programobj=newProgram();obj.Screenshot();varprocess=newProcess(){StartInfo=newProcessStartInfo(pythonInterpreterPath){CreateNoWindow=true,UseShellExecute=false,RedirectStandardOutput=true,Arguments=string.Join(" ",arguments),},};if(File.Exists(tmpfile)){process.Start();Clipboard.GetDataObject();process.WaitForExit();}File.Delete(tmpfile);}publicvoidScreenshot(){Rectanglerc=Screen.PrimaryScreen.Bounds;Bitmapprintscreen=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);Graphicsgraphics=Graphics.FromImage(printscreenasImage);graphics.CopyFromScreen(0,0,0,0,printscreen.Size);printscreen.Save(tmpfile,ImageFormat.Jpeg);}}}
importosimportsysfromPILimportImagefrompyzbarimportpyzbarimportreimportpyperclipread_data=pyzbar.decode(Image.open(os.path.join(r"C:\~\QRcode_Decoder","tmp_qrcode.jpg")))hash_dict={}fordatainread_data:one_qrcode_data=data[0].decode('utf-8','ignore')one_qrcode_data=re.sub(r"\r?\n","\r\n",one_qrcode_data)qrcode_idx,hash_value=one_qrcode_data.split(":")hash_dict.update({int(qrcode_idx.replace("-","")):hash_value})hash_list=[]fordatainsorted(hash_dict.items()):hash_list.append(data[1])clipboard="\r\n".join(hash_list)ifnotclipboard:pyperclip.copy("Error")else:pyperclip.copy(clipboard)

と書いたところでC#はZXing.NETでQRコードの読み取り用ライブラリがあることがわかった。。。


Viewing all articles
Browse latest Browse all 8899

Trending Articles