From 2bec9c838d2d688025698de8ec1de401ffd7dd1f Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期二, 11 八月 2020 14:13:55 +0800
Subject: [PATCH] 20200811

---
 HDL-ON_Android/Scan.cs |  120 ++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 101 insertions(+), 19 deletions(-)

diff --git a/HDL-ON_Android/Scan.cs b/HDL-ON_Android/Scan.cs
index 9c5dd3d..01cbcb3 100644
--- a/HDL-ON_Android/Scan.cs
+++ b/HDL-ON_Android/Scan.cs
@@ -1,4 +1,5 @@
 锘縰sing System;
+using System.IO;
 using Shared;
 using ZXing.Mobile;
 
@@ -9,36 +10,117 @@
         public Scan()
         {
         }
-
+        static MobileBarcodeScanner scanner;
         public static void OpenScan(Action<string> action)
         {
-            (Shared.Application.Activity as BaseActivity).SetCamera((obj) => {
+            string cancel = "鍙栨秷";
+            string flashText = "";
+            string titleText = "浜岀淮鐮佹壂鎻�";
+            if (Language.CurrentLanguage != "Chinese")
+            {
+                cancel = "Cancel";
+                flashText = "";
+                titleText = "Scan";
+            }
+
+            ((BaseActivity)Shared.Application.Activity).SetCamera(async (obj) =>
+            {
                 if (obj)
                 {
-                    var scanner = new MobileBarcodeScanner { };
-                    // ZXing  
-                    scanner.UseCustomOverlay = false;
-                    // 搴曢儴涓や釜鎸夐挳鏂囧瓧  
-                    scanner.FlashButtonText = "";//"璇嗗埆";
-                    scanner.CancelButtonText = Language.CurrentLanguage == "Chinese" ? "鍙栨秷" : "Cancel";
+                    if (scanner == null)
+                    {
+                        MobileBarcodeScanner.Initialize(Shared.Application.Activity.Application);
+                        var mZXingCustomScanView = new ZXingCustomScanView(Shared.Application.Activity.Application);
+                        mZXingCustomScanView.cancelTextView.Text = cancel;
+                        mZXingCustomScanView.flashTextView.Text = flashText;
+                        mZXingCustomScanView.titleTextView.Text = titleText;
+                        var bOn = false;
+                        scanner = new ZXing.Mobile.MobileBarcodeScanner()
+                        {
+                            UseCustomOverlay = true,
+                            CustomOverlay = mZXingCustomScanView
+                        };
 
-                    // 鏂规涓婏紝涓嬫柟鏂囧瓧鎻愮ず  
-                    scanner.TopText = Language.CurrentLanguage == "Chinese" ? "璇峰皢鏉″舰鐮佸鍑嗘柟妗嗗唴" : "Please align the bar code in the scan box";
-                    scanner.BottomText = "";
+                        mZXingCustomScanView.OnCancel += () =>
+                        {
 
-                    // 寤舵椂涓夌鑷姩鎵  
-                    var opt = new MobileBarcodeScanningOptions();
-                    opt.DelayBetweenContinuousScans = 3000;
-                    scanner.ScanContinuously(opt, (obj2) => {
-                        scanner.Cancel();
-                        action?.Invoke(obj2.Text);
-                    });
+                            scanner?.Cancel();
+                        };
+
+                        mZXingCustomScanView.OnTorch += () =>
+                        {
+                            bOn = !bOn;
+                            scanner?.Torch(bOn);
+                        };
+                    }
+
+                    var result = await scanner.Scan();
+
+                    if (result != null)
+                        action?.Invoke(result.Text);
+                    else
+                        action?.Invoke(null);
+
                 }
                 else
                 {
-                    action?.Invoke("");
+                    action?.Invoke(null);
                 }
             });
         }
+
+        public static byte[] BytesFromText(string text, int width = 300, int height = 300)
+        {
+            var barcodeWriter = new ZXing.Mobile.BarcodeWriter
+            {
+                Format = ZXing.BarcodeFormat.QR_CODE,
+                Options = new ZXing.Common.EncodingOptions
+                {
+                    Width = width,
+                    Height = height,
+                    Margin = 0
+                }
+            };
+
+            barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
+            var bitmap = barcodeWriter.Write(text);
+            using (var stream = new MemoryStream())
+            {
+                bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream);  // this is the diff between iOS and Android
+                stream.Position = 0;
+                return stream.ToArray();
+            }
+        }
+
+        //public static void OpenScan1(Action<string> action)
+        //{
+        //    (Shared.Application.Activity as BaseActivity).SetCamera((obj) => {
+        //        if (obj)
+        //        {
+        //            var scanner = new MobileBarcodeScanner { };
+        //            // ZXing  
+        //            scanner.UseCustomOverlay = false;
+        //            // 搴曢儴涓や釜鎸夐挳鏂囧瓧  
+        //            scanner.FlashButtonText = "";//"璇嗗埆";
+        //            scanner.CancelButtonText = Language.CurrentLanguage == "Chinese" ? "鍙栨秷" : "Cancel";
+
+        //            // 鏂规涓婏紝涓嬫柟鏂囧瓧鎻愮ず  
+        //            scanner.TopText = Language.CurrentLanguage == "Chinese" ? "璇峰皢鏉″舰鐮佸鍑嗘柟妗嗗唴" : "Please align the bar code in the scan box";
+        //            scanner.BottomText = "";
+
+        //            // 寤舵椂涓夌鑷姩鎵  
+        //            var opt = new MobileBarcodeScanningOptions();
+        //            opt.DelayBetweenContinuousScans = 3000;
+        //            scanner.ScanContinuously(opt, (obj2) => {
+        //                scanner.Cancel();
+        //                action?.Invoke(obj2.Text);
+        //            });
+        //        }
+        //        else
+        //        {
+        //            action?.Invoke("");
+        //        }
+        //    });
+        //}
     }
 }

--
Gitblit v1.8.0