| | |
| | | using System; |
| | | using HDL_ON_iOS; |
| | | using Shared; |
| | | using ZXing.Mobile; |
| | | |
| | |
| | | { |
| | | public class Scan |
| | | { |
| | | |
| | | public Scan() |
| | | { |
| | | } |
| | | public static void OpenScan(Action<string> action) |
| | | |
| | | static MobileBarcodeScanner scanner; |
| | | public static async void OpenScan(Action<string> action) |
| | | { |
| | | string cancel = "取消"; |
| | | string flashText = ""; |
| | | string titleText = "二维码扫描"; |
| | | if (Language.CurrentLanguage != "Chinese") |
| | | { |
| | | cancel = "Cancel"; |
| | | flashText = ""; |
| | | titleText = "Scan"; |
| | | } |
| | | |
| | | var scanner = new MobileBarcodeScanner(BaseViewController.Instance) { }; |
| | | // ZXing |
| | | scanner.UseCustomOverlay = false; |
| | | // 底部两个按钮文字 |
| | | scanner.FlashButtonText = "";//"识别"; |
| | | scanner.CancelButtonText = Language.CurrentLanguage == "Chinese" ? "取消" : "Cancel"; |
| | | if (scanner == null) |
| | | { |
| | | var mZXingOverlayView = new ZXingOverlayView(cancel, flashText, titleText); |
| | | scanner = new MobileBarcodeScanner(AppDelegate.rootViewController) { FlashButtonText = flashText, TopText = titleText, BottomText = "", CancelButtonText = cancel }; |
| | | scanner.UseCustomOverlay = true; |
| | | |
| | | // 方框上,下方文字提示 |
| | | //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, (obj) => { |
| | | scanner.Cancel(); |
| | | if (action != null) |
| | | scanner.CustomOverlay = mZXingOverlayView; |
| | | var bOn = false; |
| | | mZXingOverlayView.OnCancel += () => |
| | | { |
| | | action(obj.Text); |
| | | } |
| | | }); |
| | | |
| | | scanner?.Cancel(); |
| | | }; |
| | | |
| | | mZXingOverlayView.OnTorch += () => |
| | | { |
| | | bOn = !bOn; |
| | | scanner?.Torch(bOn); |
| | | }; |
| | | |
| | | } |
| | | |
| | | var result = await scanner.Scan(); |
| | | |
| | | if (result != null) |
| | | action?.Invoke(result.Text); |
| | | else |
| | | 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 uiImage = barcodeWriter.Write(text); |
| | | var data = uiImage.AsJPEG(); |
| | | |
| | | var resultBytes = new byte[data.Length]; |
| | | System.Runtime.InteropServices.Marshal.Copy(data.Bytes, resultBytes, 0, resultBytes.Length); |
| | | |
| | | return resultBytes; |
| | | } |
| | | |
| | | |
| | | //public static void OpenScan(Action<string> action) |
| | | //{ |
| | | |
| | | // var scanner = new MobileBarcodeScanner(BaseViewController.Instance) { }; |
| | | // // 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, (obj) => { |
| | | // scanner.Cancel(); |
| | | // if (action != null) |
| | | // { |
| | | // action(obj.Text); |
| | | // } |
| | | // }); |
| | | //} |
| | | } |
| | | } |
| | | } |