mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-26 19:34:25 +02:00
Create QRCodeReaderViewController used to scan a QR code.
This commit is contained in:
+62
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="wTo-mk-4wn">
|
||||
<device id="retina6_1" orientation="portrait">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--Code Reader View Controller-->
|
||||
<scene sceneID="yHG-c0-Vr8">
|
||||
<objects>
|
||||
<viewController id="wTo-mk-4wn" customClass="QRCodeReaderViewController" customModule="Riot" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="eyK-7t-b1z">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<subviews>
|
||||
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Rdg-xW-u5X">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</view>
|
||||
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="HjZ-fK-SvP" customClass="CloseButton" customModule="Riot" customModuleProvider="target">
|
||||
<rect key="frame" x="357" y="57" width="44" height="44"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="width" secondItem="HjZ-fK-SvP" secondAttribute="height" multiplier="1:1" id="csD-MZ-BJU"/>
|
||||
<constraint firstAttribute="height" constant="44" id="tWy-os-fYQ"/>
|
||||
</constraints>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="15"/>
|
||||
<state key="normal" image="close_button"/>
|
||||
<connections>
|
||||
<action selector="closeButtonAction:" destination="wTo-mk-4wn" eventType="touchUpInside" id="Dyd-I3-uHS"/>
|
||||
</connections>
|
||||
</button>
|
||||
</subviews>
|
||||
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="trailing" secondItem="Rdg-xW-u5X" secondAttribute="trailing" id="DzV-SN-Iog"/>
|
||||
<constraint firstItem="Rdg-xW-u5X" firstAttribute="top" secondItem="eyK-7t-b1z" secondAttribute="top" id="J4V-oh-elV"/>
|
||||
<constraint firstAttribute="bottom" secondItem="Rdg-xW-u5X" secondAttribute="bottom" id="TD5-6H-rgZ"/>
|
||||
<constraint firstItem="HjZ-fK-SvP" firstAttribute="top" secondItem="Ndt-0H-a0X" secondAttribute="top" constant="13" id="gkO-XS-hSc"/>
|
||||
<constraint firstItem="Rdg-xW-u5X" firstAttribute="leading" secondItem="eyK-7t-b1z" secondAttribute="leading" id="iLN-43-eOy"/>
|
||||
<constraint firstItem="Ndt-0H-a0X" firstAttribute="trailing" secondItem="HjZ-fK-SvP" secondAttribute="trailing" constant="13" id="qyo-8y-264"/>
|
||||
</constraints>
|
||||
<viewLayoutGuide key="safeArea" id="Ndt-0H-a0X"/>
|
||||
</view>
|
||||
<connections>
|
||||
<outlet property="closeButton" destination="HjZ-fK-SvP" id="mGR-8w-Hpt"/>
|
||||
<outlet property="codeReaderContainerView" destination="Rdg-xW-u5X" id="Sxz-D5-Axf"/>
|
||||
</connections>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="oGu-Wb-sGr" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="-261" y="29"/>
|
||||
</scene>
|
||||
</scenes>
|
||||
<resources>
|
||||
<image name="close_button" width="16" height="16"/>
|
||||
</resources>
|
||||
</document>
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
Copyright 2020 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import UIKit
|
||||
import ZXingObjC
|
||||
|
||||
protocol QRCodeReaderViewControllerDelegate: class {
|
||||
func qrCodeReaderViewController(_ viewController: QRCodeReaderViewController, didFound payloadData: Data)
|
||||
func qrCodeReaderViewControllerDidCancel(_ viewController: QRCodeReaderViewController)
|
||||
}
|
||||
|
||||
/// QRCodeReaderViewController is a view controller used to scan a QR code
|
||||
/// Some methods are based on [ZXing sample](https://github.com/zxingify/zxingify-objc/blob/master/examples/BarcodeScannerSwift/BarcodeScannerSwift/ViewController.swift)
|
||||
final class QRCodeReaderViewController: UIViewController {
|
||||
|
||||
// MARK: - Constants
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Outlets
|
||||
|
||||
@IBOutlet private weak var closeButton: CloseButton!
|
||||
@IBOutlet private weak var codeReaderContainerView: UIView!
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private var theme: Theme!
|
||||
private var errorPresenter: MXKErrorPresentation!
|
||||
|
||||
private lazy var zxCapture: ZXCapture = ZXCapture()
|
||||
private var captureSizeTransform: CGAffineTransform?
|
||||
private var isScanning: Bool = false
|
||||
private var isFirstApplyOrientation: Bool = false
|
||||
|
||||
// MARK: Public
|
||||
|
||||
weak var delegate: QRCodeReaderViewControllerDelegate?
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
class func instantiate() -> QRCodeReaderViewController {
|
||||
let viewController = StoryboardScene.QRCodeReaderViewController.initialScene.instantiate()
|
||||
viewController.theme = ThemeService.shared().theme
|
||||
return viewController
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.stopScanning()
|
||||
}
|
||||
|
||||
// MARK: - Life cycle
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
// Do any additional setup after loading the view.
|
||||
|
||||
self.setupViews()
|
||||
self.errorPresenter = MXKErrorAlertPresentation()
|
||||
|
||||
self.registerThemeServiceDidChangeThemeNotification()
|
||||
self.update(theme: self.theme)
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
|
||||
self.startScanning()
|
||||
}
|
||||
|
||||
override public func viewWillDisappear(_ animated: Bool) {
|
||||
self.stopScanning()
|
||||
|
||||
super.viewWillDisappear(animated)
|
||||
}
|
||||
|
||||
override var preferredStatusBarStyle: UIStatusBarStyle {
|
||||
return self.theme.statusBarStyle
|
||||
}
|
||||
|
||||
override func viewDidLayoutSubviews() {
|
||||
super.viewDidLayoutSubviews()
|
||||
|
||||
guard isFirstApplyOrientation == false else {
|
||||
return
|
||||
}
|
||||
|
||||
isFirstApplyOrientation = true
|
||||
applyOrientation()
|
||||
}
|
||||
|
||||
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
|
||||
super.viewWillTransition(to: size, with: coordinator)
|
||||
|
||||
coordinator.animate(alongsideTransition: { (context) in
|
||||
// do nothing
|
||||
}, completion: { [weak self] (context) in
|
||||
guard let self = self else {
|
||||
return
|
||||
}
|
||||
self.applyOrientation()
|
||||
})
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
func startScanning() {
|
||||
self.zxCapture.start()
|
||||
isScanning = true
|
||||
}
|
||||
|
||||
func stopScanning() {
|
||||
self.zxCapture.stop()
|
||||
isScanning = false
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func update(theme: Theme) {
|
||||
self.theme = theme
|
||||
|
||||
self.closeButton.update(theme: theme)
|
||||
}
|
||||
|
||||
private func registerThemeServiceDidChangeThemeNotification() {
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .themeServiceDidChangeTheme, object: nil)
|
||||
}
|
||||
|
||||
@objc private func themeDidChange() {
|
||||
self.update(theme: ThemeService.shared().theme)
|
||||
}
|
||||
|
||||
private func setupViews() {
|
||||
self.setupQRCodeReaderView()
|
||||
}
|
||||
|
||||
private func setupQRCodeReaderView() {
|
||||
zxCapture.delegate = self
|
||||
zxCapture.camera = zxCapture.back()
|
||||
|
||||
zxCapture.layer.frame = codeReaderContainerView.bounds
|
||||
codeReaderContainerView.layer.addSublayer(zxCapture.layer)
|
||||
}
|
||||
|
||||
private func applyOrientation() {
|
||||
|
||||
let orientation = UIApplication.shared.statusBarOrientation
|
||||
let captureRotation: Double
|
||||
let scanRectRotation: Double
|
||||
|
||||
switch orientation {
|
||||
case .portrait:
|
||||
captureRotation = 0
|
||||
scanRectRotation = 90
|
||||
case .landscapeLeft:
|
||||
captureRotation = 90
|
||||
scanRectRotation = 180
|
||||
case .landscapeRight:
|
||||
captureRotation = 270
|
||||
scanRectRotation = 0
|
||||
case .portraitUpsideDown:
|
||||
captureRotation = 180
|
||||
scanRectRotation = 270
|
||||
default:
|
||||
captureRotation = 0
|
||||
scanRectRotation = 90
|
||||
}
|
||||
|
||||
applyRectOfInterest(orientation: orientation)
|
||||
|
||||
let angleRadius = captureRotation / 180.0 * Double.pi
|
||||
let captureTranform = CGAffineTransform(rotationAngle: CGFloat(angleRadius))
|
||||
|
||||
zxCapture.transform = captureTranform
|
||||
zxCapture.rotation = CGFloat(scanRectRotation)
|
||||
zxCapture.layer.frame = codeReaderContainerView.frame
|
||||
}
|
||||
|
||||
private func applyRectOfInterest(orientation: UIInterfaceOrientation) {
|
||||
guard var transformedVideoRect = codeReaderContainerView?.frame,
|
||||
let cameraSessionPreset = zxCapture.sessionPreset
|
||||
else { return }
|
||||
|
||||
var scaleVideoX, scaleVideoY: CGFloat
|
||||
var videoHeight, videoWidth: CGFloat
|
||||
|
||||
// Currently support only for 1920x1080 || 1280x720
|
||||
if cameraSessionPreset == AVCaptureSession.Preset.hd1920x1080.rawValue {
|
||||
videoHeight = 1080.0
|
||||
videoWidth = 1920.0
|
||||
} else {
|
||||
videoHeight = 720.0
|
||||
videoWidth = 1280.0
|
||||
}
|
||||
|
||||
if orientation == UIInterfaceOrientation.portrait {
|
||||
scaleVideoX = self.view.frame.width / videoHeight
|
||||
scaleVideoY = self.view.frame.height / videoWidth
|
||||
|
||||
// Convert CGPoint under portrait mode to map with orientation of image
|
||||
// because the image will be cropped before rotate
|
||||
// reference: https://github.com/TheLevelUp/ZXingObjC/issues/222
|
||||
let realX = transformedVideoRect.origin.y
|
||||
let realY = self.view.frame.size.width - transformedVideoRect.size.width - transformedVideoRect.origin.x
|
||||
let realWidth = transformedVideoRect.size.height
|
||||
let realHeight = transformedVideoRect.size.width
|
||||
transformedVideoRect = CGRect(x: realX, y: realY, width: realWidth, height: realHeight)
|
||||
|
||||
} else {
|
||||
scaleVideoX = self.view.frame.width / videoWidth
|
||||
scaleVideoY = self.view.frame.height / videoHeight
|
||||
}
|
||||
|
||||
captureSizeTransform = CGAffineTransform(scaleX: 1.0/scaleVideoX, y: 1.0/scaleVideoY)
|
||||
|
||||
guard let _captureSizeTransform = captureSizeTransform else {
|
||||
return
|
||||
}
|
||||
|
||||
let transformRect = transformedVideoRect.applying(_captureSizeTransform)
|
||||
zxCapture.scanRect = transformRect
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
@IBAction private func closeButtonAction(_ sender: Any) {
|
||||
self.delegate?.qrCodeReaderViewControllerDidCancel(self)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ZXCaptureDelegate
|
||||
extension QRCodeReaderViewController: ZXCaptureDelegate {
|
||||
|
||||
func captureCameraIsReady(_ capture: ZXCapture!) {
|
||||
isScanning = true
|
||||
}
|
||||
|
||||
func captureResult(_ capture: ZXCapture!, result: ZXResult!) {
|
||||
guard let zxResult = result, isScanning == true else {
|
||||
return
|
||||
}
|
||||
|
||||
guard zxResult.barcodeFormat == kBarcodeFormatQRCode else {
|
||||
return
|
||||
}
|
||||
|
||||
self.stopScanning()
|
||||
|
||||
if let bytes = result.resultMetadata.object(forKey: kResultMetadataTypeByteSegments.rawValue) as? NSArray,
|
||||
let byteArray = bytes.firstObject as? ZXByteArray {
|
||||
|
||||
let data = Data(bytes: UnsafeRawPointer(byteArray.array), count: Int(byteArray.length))
|
||||
|
||||
self.delegate?.qrCodeReaderViewController(self, didFound: data)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user