using System; using System.IO; using Android.Content; using Shared; using ZXing.Mobile; namespace HDL_ON { public class Scan: Java.Lang.Object,HDL_ON_Android.ScanActivity.ScanResult { public Scan() { } private static Scan ins; public static Scan Ins { get { if (ins == null) { ins = new Scan(); } return ins; } } public string TipMsg = string.Empty; Action resultAction; static MobileBarcodeScanner scanner; public void OpenScan(Action action) { resultAction = action; ((BaseActivity)Shared.Application.Activity).SetCamera(async (obj) => { if (obj)//权限是否打开 { if (scanner == null) { var intent = new Intent(Shared.Application.Activity, typeof(HDL_ON_Android.ScanActivity)); Shared.Application.Activity.StartActivity(intent); HDL_ON_Android.ScanActivity.InitDdd(this); //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; //mZXingCustomScanView.SetMinimumWidth( 1800); //var bOn = false; //scanner = new ZXing.Mobile.MobileBarcodeScanner() //{ // UseCustomOverlay = true, // CustomOverlay = mZXingCustomScanView //}; ////scanner.CustomOverlay.width //mZXingCustomScanView.OnCancel += () => //{ // 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(null); Action action1 = () => { Intent intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings); intent.AddFlags(ActivityFlags.NewTask); Android.Net.Uri uri = Android.Net.Uri.FromParts("package", Application.Activity.PackageName, null); intent.SetData(uri); Application.Activity.StartActivity(intent); }; if (string.IsNullOrEmpty(TipMsg)) { if (Language.CurrentLanguage == "Chinese") { new HDL_ON.UI.PublicAssmebly().TipOptionMsg(StringId.Tip, "相机访问权限已被拒绝,请前往系统设置打开相关权限。", action1); } else { new HDL_ON.UI.PublicAssmebly().TipOptionMsg(StringId.Tip, "Camera access has been denied. Please go to system settings to open relevant permissions.", action1); } } else { new HDL_ON.UI.PublicAssmebly().TipOptionMsg(StringId.Tip, TipMsg,action1,45); TipMsg = string.Empty; } } }); } public static byte[] BytesFromText(string text, int width = 300, int height = 300) { if (text == "") return null; 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 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(""); // } // }); //} //static void initFromCameraParameters(Android.Graphics.Camera camera) //{ // //camera.SetLocation //} public void onResult(string result) { if (!string.IsNullOrEmpty( result)) resultAction?.Invoke(result); else resultAction?.Invoke(null); } } }