JLChen
2021-10-08 f8457b624a75bf8e41489b74f66009eee6891b8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
//  SMBCorpScannerViewController.swift
//  LCIphoneAdhocIP
//
//  Created by imou on 2020/4/8.
//  Copyright © 2020 dahua. All rights reserved.
//
 
import DHScanner
import UIKit
 
public class SMBCorpScannerViewController: DHScannerViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    let heightRadio = (dh_screenHeight / 812)
    let pickerVC = UIImagePickerController()
 
    override public func viewDidLoad() {
        configSubview()
        super.viewDidLoad()
        configBtn()
        configNav()
    }
 
    func configSubview() {
        let style = DHScannerViewStyle()
        style.showRectangleBorder = false
        style.animationViewType = .line
        style.scanningImage = UIImage(named: "common_scan_line")
        style.rectangleTop = dh_screenHeight / 3.0 * heightRadio
        style.rectangleLeft = 38
        style.cornerLineWith = 0
        style.rectangeRatio = 1
        style.backgroundColor = UIColor.black.withAlphaComponent(0.1)
        viewStyle = style
    }
 
    func configNav() {
        let backBtn = UIButton(type: .custom)
        backBtn.setImage(UIImage(named: "common_icon_backarrow"), for: .normal)
        view.addSubview(backBtn)
        backBtn.snp.makeConstraints { snp in
            snp.left.equalTo(self.view).offset(15)
            snp.top.equalTo(self.view).offset(55)
        }
        backBtn.addTarget(self, action: #selector(backClick(btn:)), for: .touchUpInside)
    }
 
    func configBtn() {
        let albumbBtn = UIButton(type: .custom)
        albumbBtn.setImage(UIImage(named: "common_scan_icon_image"), for: .normal)
        view.addSubview(albumbBtn)
        albumbBtn.snp.makeConstraints { snp in
            snp.bottom.equalTo(self.view).offset(-60 * heightRadio)
            snp.right.equalTo(self.view.snp_right).offset(-74)
//            snp.top.equalTo(self.scanningArea.maxY).offset(20)
        }
        albumbBtn.addTarget(self, action: #selector(albumBtnClick), for: .touchUpInside)
 
        let lightBtn = UIButton(type: .custom)
        lightBtn.setImage(UIImage(named: "common_scan_icon_light"), for: .normal)
        lightBtn.setUIButtonImageUpWithTitleDownUI()
        lightBtn.addTarget(self, action: #selector(lightBtnClick(btn:)), for: .touchUpInside)
        view.addSubview(lightBtn)
        lightBtn.snp.makeConstraints { snp in
            snp.bottom.equalTo(self.view).offset(-60 * heightRadio)
            snp.left.equalTo(self.view.snp_left).offset(74)
//            snp.top.equalTo(self.scanningArea.maxY).offset(20)
        }
    }
 
    @objc func albumBtnClick(btn: UIButton) {
        LCPermissionHelper.requestAlbumPermission { granted in
            if granted {
                self.pickerVC.sourceType = .photoLibrary
                self.pickerVC.allowsEditing = true
                self.pickerVC.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
                self.present(self.pickerVC, animated: true, completion: nil)
            }
        }
    }
 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
        picker.dismiss(animated: true) {
        }
        let mediaType = info[UIImagePickerControllerMediaType] as! String?
        if mediaType == "public.image" {
            if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
                // 处理图片
                LCProgressHUD.show(on: view)
                if let resultStr = DHScannerUtils.decodeImage(image: image), resultStr.count > 0 {
                    print("110801====DHScannerUtils:\(resultStr)")
                    LCProgressHUD.hideAllHuds(view)
                    self.scanResult(text: resultStr, image: image)
                } else {
                    let ciImage : CIImage = CIImage(image: image)!
                    let context = CIContext(options: nil)
                    let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: context, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
                    let features = detector?.features(in: ciImage)
                    if let feature = features?.first as? CIQRCodeFeature, let resultStr = feature.messageString, resultStr.count > 0 {
                        print("110801======CIQRCodeFeature:\(resultStr)")
                        LCProgressHUD.hideAllHuds(view)
                        self.scanResult(text: resultStr, image: image)
                    }else {
                        LCProgressHUD.hideAllHuds(view)
                        LCProgressHUD.showMsg("add_device_scanning_error_tip".lc_T)
                    }
                }
            }
        }
    }
 
    @objc func backClick(btn: UIButton) {
        dismiss(animated: true, completion: nil)
    }
 
    @objc func lightBtnClick(btn: UIButton) {
        torchOn = !torchOn
    }
 
    override public func scanResult(text: String, image: UIImage?) {
        if text.isSafetyWeb() {
            let web = LCAdvertisementDetailViewController()
            web.playUrl = text
            self.present(web, animated: true, completion: nil)
        }
    }
}