{"id":776,"date":"2021-09-07T22:06:51","date_gmt":"2021-09-07T14:06:51","guid":{"rendered":"http:\/\/www.wayln.com\/?p=776"},"modified":"2021-09-07T22:06:51","modified_gmt":"2021-09-07T14:06:51","slug":"c%e4%bd%bf%e7%94%a8qrcoder%e5%92%8czxing%e5%ae%9e%e7%8e%b0%e4%ba%8c%e7%bb%b4%e7%a0%81%e7%9a%84%e7%94%9f%e6%88%90%e5%92%8c%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=776","title":{"rendered":"C#\u4f7f\u7528QRCoder\u548cZXing\u5b9e\u73b0\u4e8c\u7ef4\u7801\u7684\u751f\u6210\u548c\u8bc6\u522b"},"content":{"rendered":"<pre><code class=\"language-CSharp line-numbers\">using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Drawing;\nusing System.Drawing.Imaging;\nusing System.IO;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\nusing QRCoder;\nusing Windows.Devices.PointOfService;\nusing ZXing;\nusing ZXing.Common;\nusing ZXing.QrCode;\nusing ZXing.QrCode.Internal;\n\nnamespace WinFormsApp4\n{\n    public partial class Form1 : Form\n    {\n        public Form1()\n        {\n            InitializeComponent();\n        }\n\n        private void button1_Click(object sender, EventArgs e)\n        {\n            string name = \"1234\";\n            QRCodeGenerator QR = new QRCodeGenerator();\n            QRCodeData cd = QR.CreateQrCode(name, QRCodeGenerator.ECCLevel.Q);\n           QRCoder.QRCode qRCode = new QRCoder.QRCode(cd);\n\n            Bitmap qrCodeImage = qRCode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);\n\n            qrCodeImage.Save(Environment.CurrentDirectory + \"\\\\t.jpeg\");\n        }\n\n        private void button2_Click(object sender, EventArgs e)\n        {\n\n\n            button2.Text = DecodeQrCode(Environment.CurrentDirectory + \"\\\\t.jpeg\");\n        }\n\n\n        string DecodeQrCode(string filename)\n        {\n            \/\/\u76f4\u63a5new Bitmap\u4f1a\u5bfc\u81f4\u56fe\u7247\u5360\u7528\uff0c\u65e0\u6cd5\u518d\u6b21\u8986\u76d6\u751f\u6210\n            Bitmap bitmap = FileToBitmap(filename);\n            try\n            {\n                BarcodeReader reader = new BarcodeReader { AutoRotate = true };\n                Result result = reader.Decode(bitmap);\n                string decodedData = result.Text;\n\n                return decodedData;\n            }\n            catch\n            {\n                throw new Exception(\"Cannot decode the QR code\");\n            }\n            return null;\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u8bfb\u53d6Bitmap\u56fe\u7247\uff0c\u9632\u6b62\u8d44\u6e90\u5360\u7528\u5bfc\u81f4\u5728\u6b64\u8bfb\u53d6\u5931\u8d25\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"fileName\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        Bitmap FileToBitmap(string fileName)\n        {\n            \/\/ \u6253\u5f00\u6587\u4ef6  \n            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);\n            \/\/ \u8bfb\u53d6\u6587\u4ef6\u7684 byte[]  \n            byte[] bytes = new byte[fileStream.Length];\n            fileStream.Read(bytes, 0, bytes.Length);\n            fileStream.Close();\n            \/\/ \u628a byte[] \u8f6c\u6362\u6210 Stream  \n            Stream stream = new MemoryStream(bytes);\n\n            stream.Read(bytes, 0, bytes.Length);\n            \/\/ \u8bbe\u7f6e\u5f53\u524d\u6d41\u7684\u4f4d\u7f6e\u4e3a\u6d41\u7684\u5f00\u59cb  \n            stream.Seek(0, SeekOrigin.Begin);\n\n            MemoryStream mstream = null;\n            try\n            {\n                mstream = new MemoryStream(bytes);\n                return new Bitmap((Image)new Bitmap(stream));\n            }\n            catch (ArgumentNullException ex)\n            {\n                return null;\n            }\n            catch (ArgumentException ex)\n            {\n                return null;\n            }\n            finally\n            {\n                stream.Close();\n            }\n        }\n\n    }\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using System; using System.Collections.Generic; using S [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[],"class_list":["post-776","post","type-post","status-publish","format-standard","hentry","category-c","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=776"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/776\/revisions"}],"predecessor-version":[{"id":777,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/776\/revisions\/777"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}