diff --git a/Sources/Internal/RCConstants.swift b/Sources/Internal/RCConstants.swift index 99a2259..27301b0 100644 --- a/Sources/Internal/RCConstants.swift +++ b/Sources/Internal/RCConstants.swift @@ -31,6 +31,6 @@ struct RCConstants { static let dotSizeScale: CGFloat = 0.08 static let dotPatterns: [CGFloat] = [6, 4, 2] static let dotPointRange = (Float(1.6)...Float(2.2)) + static let pixelThreshold = 180 static let emptySymbols: [Character] = ["\u{0540}", "\u{0531}"] - } diff --git a/Sources/Internal/RCImageDecoder.swift b/Sources/Internal/RCImageDecoder.swift index 96b5645..2b69848 100644 --- a/Sources/Internal/RCImageDecoder.swift +++ b/Sources/Internal/RCImageDecoder.swift @@ -21,13 +21,15 @@ // SOFTWARE. import UIKit -import CoreImage final class RCImageDecoder { - - private var context = CIContext() - - func process(pointer: UnsafeMutablePointer, size: Int) throws -> [RCBit] { + + var size = 0 + +} + +extension RCImageDecoder { + func process(pointer: UnsafeMutablePointer) throws -> [RCBit] { let bufferData = UnsafeMutableBufferPointer(start: pointer, count: size * size) let data = RCMatrix(rows: size, items: bufferData) let sectionSize = size / 5 @@ -56,17 +58,17 @@ final class RCImageDecoder { let point = try scanControlPoint(region: (sector.x, sector.y, sectionSize), data: data, side: sector.side) points.append(point) } - let image = try fixPerspective(pointer, points: points.map({CIVector(x: $0.x, y: CGFloat(size) - $0.y)}), size: size) + let image = try fixPerspective(pointer, points: points) let bits = decode(image) return bits } - func decode(_ image: UIImage) throws -> [RCBit] { - let size = image.cgImage!.height + func decode(_ image: UIImage, size: Int) throws -> [RCBit] { + self.size = size let pixelData = UnsafeMutableRawPointer.allocate(byteCount: size * size, alignment: MemoryLayout.alignment) - let context = CGContext(data: pixelData, width: size, height: size, bitsPerComponent: 8, bytesPerRow: size, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue) + let context = generateContext(data: pixelData) context?.draw(image.cgImage!, in: CGRect(origin: .zero, size: CGSize(width: size, height: size))) - let bits = try process(pointer: pixelData.assumingMemoryBound(to: UInt8.self), size: size) + let bits = try process(pointer: pixelData.assumingMemoryBound(to: UInt8.self)) pixelData.deallocate() return bits } @@ -75,14 +77,14 @@ final class RCImageDecoder { extension RCImageDecoder { private func scanControlPoint(region: (x: Int, y: Int, size: Int), data: RCMatrix, side: Side) throws -> CGPoint { var points = [CGPoint]() - var lastPattern = RCPixelPattern(bit: data[region.x, region.y] > 180 ? RCBit.zero : RCBit.one, row: region.y, column: region.x, count: 0) + var lastPattern = RCPixelPattern(bit: data[region.x, region.y] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one, row: region.y, column: region.x, count: 0) var pixelPatterns = [lastPattern] var count = 0 let maxSize = region.size * region.size while count < maxSize { let rowIndex = count / region.size + region.y let columnIndex = count % region.size + region.x - let bit = data[columnIndex, rowIndex] > 180 ? RCBit.zero : RCBit.one + let bit = data[columnIndex, rowIndex] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one if lastPattern.row == rowIndex, lastPattern.bit == bit { lastPattern.count += 1 pixelPatterns[pixelPatterns.count - 1] = lastPattern @@ -140,35 +142,35 @@ extension RCImageDecoder { } } - private func fixPerspective(_ data: UnsafeMutablePointer, points: [CIVector], size: Int) throws -> CGImage { - let imageData = Data(bytes: UnsafeRawPointer(data), count: size * size) - var image = CIImage(bitmapData: imageData, bytesPerRow: size, size: CGSize(width: size, height: size), format: .L8, colorSpace: CGColorSpaceCreateDeviceGray()) - let transformFilter = CIFilter(name: "CIAffineTransform")! - transformFilter.setValue(image, forKey: "inputImage") - transformFilter.setValue(CGAffineTransform(rotationAngle: CGFloat.pi / 4), forKey: "inputTransform") - image = transformFilter.outputImage! - let translation = CGAffineTransform(translationX: -image.extent.origin.x, y: -image.extent.origin.y) - transformFilter.setValue(image, forKey: "inputImage") - transformFilter.setValue(translation, forKey: "inputTransform") - image = transformFilter.outputImage! - transformFilter.setValue(image, forKey: "inputImage") - transformFilter.setValue(CGAffineTransform(scaleX: 1 / sqrt(2), y: 1 / sqrt(2)), forKey: "inputTransform") - image = transformFilter.outputImage! - let perspectiveFilter = CIFilter(name: "CIPerspectiveCorrection")! - perspectiveFilter.setValue(image, forKey: "inputImage") - perspectiveFilter.setValue(points[0], forKey: "inputTopLeft") - perspectiveFilter.setValue(points[1], forKey: "inputTopRight") - perspectiveFilter.setValue(points[2], forKey: "inputBottomRight") - perspectiveFilter.setValue(points[3], forKey: "inputBottomLeft") - image = perspectiveFilter.outputImage! - let newSize = min(image.extent.width, image.extent.height) - guard let renderedImage = context.createCGImage(image, from: CGRect(origin: .zero, size: CGSize(width: newSize, height: newSize))) else { throw RCError.decoding } - return renderedImage + private func fixPerspective(_ data: UnsafeMutablePointer, points: [CGPoint]) throws -> CGImage { + guard let context = generateContext(data: data), let cgImage = context.makeImage() else { + throw RCError.decoding + } + let image = UIGraphicsImageRenderer(size: CGSize(width: CGFloat(size), height: CGFloat(size))).image { context in + let baseView = UIView(frame: context.format.bounds) + let imageView = UIImageView(frame: context.format.bounds) + baseView.addSubview(imageView) + imageView.image = UIImage(cgImage: cgImage) + imageView.layer.anchorPoint = .zero + imageView.layer.frame = context.format.bounds + let perspective = RCTransformation(points) + let destination = RCTransformation([CGPoint(x: context.format.bounds.minX, y: context.format.bounds.midY), + CGPoint(x: context.format.bounds.midX, y: context.format.bounds.minY), + CGPoint(x: context.format.bounds.midX, y: context.format.bounds.maxY), + CGPoint(x: context.format.bounds.maxX, y: context.format.bounds.midY)]) + let transform = perspective.perspectiveTransform(to: destination) + imageView.transform3D = transform + baseView.drawHierarchy(in: context.format.bounds, afterScreenUpdates: true) + } + guard let renderImage = image.cgImage else { + throw RCError.decoding + } + return renderImage } private func decode(_ image: CGImage) -> [RCBit] { let pixelData = UnsafeMutableRawPointer.allocate(byteCount: image.width * image.height, alignment: MemoryLayout.alignment) - let context = CGContext(data: pixelData, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: image.width, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue) + let context = generateContext(data: pixelData) context?.draw(image, in: CGRect(origin: .zero, size: CGSize(width: image.width, height: image.height))) let buffer = UnsafeMutableBufferPointer(start: pixelData.assumingMemoryBound(to: UInt8.self), count: image.width * image.height) let data = RCMatrix(rows: image.height, items: buffer) @@ -197,6 +199,15 @@ extension RCImageDecoder { } } +extension RCImageDecoder { + private func generateContext(data: UnsafeMutableRawPointer? = nil) -> CGContext? { + return CGContext(data: data, width: self.size, height: self.size, bitsPerComponent: 8, bytesPerRow: self.size, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue) + } + + + +} + extension RCImageDecoder { struct RCPixelPattern: CustomStringConvertible { @@ -214,7 +225,7 @@ extension RCImageDecoder { enum Side: CaseIterable { case left case top - case right case bottom + case right } } diff --git a/Sources/Internal/RCTransformation.swift b/Sources/Internal/RCTransformation.swift new file mode 100644 index 0000000..883e688 --- /dev/null +++ b/Sources/Internal/RCTransformation.swift @@ -0,0 +1,57 @@ +// MIT License + +// Copyright (c) 2020 Haik Aslanyan + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import simd +import QuartzCore + +final class RCTransformation { + + private let vectors: [SIMD3] + private static let zeroColumnBeforeLast: double4x3 = { + let identity = double3x3(diagonal: SIMD3(repeating: 1)) + var columns = (0 ... (SIMD3.scalarCount - 1)).map {identity[$0]} + columns.insert(SIMD3(repeating: 0), at: SIMD3.scalarCount - 1) + return .init(columns) + }() + private static let zeroRowBeforeLast: double3x4 = zeroColumnBeforeLast.transpose + + init(_ points: [CGPoint]) { + self.vectors = points.map {SIMD3(Double($0.x), Double($0.y), 1)} + } + + func perspectiveTransform(to destination: RCTransformation) -> CATransform3D { + let destinationBaseVectors = double3x3(Array(destination.vectors[0 ... (SIMD3.scalarCount - 1)])) + let destinationVectors = destinationBaseVectors.inverse * destination.vectors[SIMD3.scalarCount] + let destinationScale = double3x3(diagonal: destinationVectors) + let destinationPoints = destinationBaseVectors * destinationScale + let currentBaseVectors = double3x3(Array(vectors[0 ... (SIMD3.scalarCount - 1)])) + let currentVectors = currentBaseVectors.inverse * vectors[SIMD3.scalarCount] + let currentScale = double3x3(diagonal: currentVectors) + let currentPoints = currentBaseVectors * currentScale + let perspective = destinationPoints * currentPoints.inverse + let zNormalized = 1 / perspective[SIMD3.scalarCount - 1, SIMD3.scalarCount - 1] * perspective + var matrix = (RCTransformation.zeroRowBeforeLast * zNormalized) * RCTransformation.zeroColumnBeforeLast + + matrix[SIMD3.scalarCount - 1, SIMD3.scalarCount - 1] = 1 + return unsafeBitCast(matrix, to: CATransform3D.self) + } +} diff --git a/Sources/Public/RCCameraViewController.swift b/Sources/Public/RCCameraViewController.swift index e9b65f7..ea93539 100644 --- a/Sources/Public/RCCameraViewController.swift +++ b/Sources/Public/RCCameraViewController.swift @@ -52,13 +52,6 @@ public final class RCCameraViewController: UIViewController, UIImagePickerContro cameraView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true return cameraView }() - lazy var timeLabel: UILabel = { - let label = UILabel.init(frame: CGRect.init(x: 20, y: 20, width: 100, height: 30)) - label.textColor = .white - self.view.addSubview(label) - return label - }() - //MARK: Inits public required init?(coder: NSCoder) { @@ -153,11 +146,12 @@ extension RCCameraViewController { guard let captureDevice = AVCaptureDevice.default(for: .video) else { return } do { captureSession.sessionPreset = .hd1280x720 + coder.set(size: 720) let input = try AVCaptureDeviceInput(device: captureDevice) input.device.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 30) captureSession.addInput(input) let videoOutput = AVCaptureVideoDataOutput() - videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: .background)) + videoOutput.setSampleBufferDelegate(self, queue: .main) videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)] captureSession.addOutput(videoOutput) } catch { @@ -182,7 +176,6 @@ extension RCCameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate { connection.videoOrientation = .portrait guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly) - let date = Date() let bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0) let bufferHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0) let bufferWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0) @@ -191,16 +184,9 @@ extension RCCameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate { let lumaBaseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)?.advanced(by: bytesPerRow * origin) let lumaCopy = UnsafeMutableRawPointer.allocate(byteCount: bytesPerRow * size, alignment: MemoryLayout.alignment) lumaCopy.copyMemory(from: lumaBaseAddress!, byteCount: bytesPerRow * size) - if let message = try? coder.decode(buffer: lumaCopy.assumingMemoryBound(to: UInt8.self), size: size) { - DispatchQueue.main.async {[weak self] in - guard let weakSelf = self else { return } - weakSelf.dismiss(animated: true) { - weakSelf.delegate?.cameraViewController(weakSelf, didFinishPickingScanning: message) - } - } - } - DispatchQueue.main.async { - self.timeLabel.text = "\((1 / Date().timeIntervalSince(date)).rounded())" + if let message = try? coder.decode(buffer: lumaCopy.assumingMemoryBound(to: UInt8.self)) { + delegate?.cameraViewController(self, didFinishPickingScanning: message) + dismiss(animated: true) } lumaCopy.deallocate() CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly) diff --git a/Sources/Public/RCCoder.swift b/Sources/Public/RCCoder.swift index 9271679..33c7362 100644 --- a/Sources/Public/RCCoder.swift +++ b/Sources/Public/RCCoder.swift @@ -43,7 +43,7 @@ public extension RCCoder { func decode(_ image: UIImage) throws -> String { guard image.size.width == image.size.height else { throw RCError.wrongImageSize } - let bits = try imageDecoder.decode(image) + let bits = try imageDecoder.decode(image, size: image.cgImage!.height) let message = try bitCoder.decode(bits) return message } @@ -55,9 +55,13 @@ public extension RCCoder { extension RCCoder { - func decode(buffer: UnsafeMutablePointer, size: Int) throws -> String { - let bits = try imageDecoder.process(pointer: buffer, size: size) + func decode(buffer: UnsafeMutablePointer) throws -> String { + let bits = try imageDecoder.process(pointer: buffer) let message = try bitCoder.decode(bits) return message } + + func set(size: Int) { + imageDecoder.size = size + } }