Skip to content

Commit

Permalink
added bytesPerRow parameter for decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
aslanyanhaik committed Apr 11, 2020
1 parent 7d89d01 commit e982753
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
16 changes: 9 additions & 7 deletions Sources/Internal/RCImageDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class RCImageDecoder {

var size: Int
var padding = 0
lazy var bytesPerRow = self.size
let configuration: RCCoderConfiguration
private let sectionSize: Int

Expand All @@ -38,8 +39,8 @@ final class RCImageDecoder {

extension RCImageDecoder {
func process(pointer: UnsafeMutablePointer<UInt8>) throws -> [RCBit] {
let bufferData = UnsafeMutableBufferPointer<UInt8>(start: pointer, count: size * size)
let data = PixelContainer(rows: size, items: bufferData)
let bufferData = UnsafeMutableBufferPointer<UInt8>(start: pointer, count: size * bytesPerRow)
let data = PixelContainer(rows: bytesPerRow, items: bufferData)
var points = [CGPoint]()
for side in Side.allCases {
switch side {
Expand All @@ -64,8 +65,9 @@ extension RCImageDecoder {

func decode(_ image: UIImage, size: Int) throws -> [RCBit] {
self.size = size
self.bytesPerRow = size
let pixelData = UnsafeMutableRawPointer.allocate(byteCount: size * size, alignment: MemoryLayout<UInt8>.alignment)
let context = generateContext(data: pixelData, size: size)
let context = generateContext(data: pixelData, size: size, bytesPerRow: self.bytesPerRow)
context?.draw(image.cgImage!, in: CGRect(origin: .zero, size: CGSize(width: size, height: size)))
let bits = try process(pointer: pixelData.assumingMemoryBound(to: UInt8.self))
pixelData.deallocate()
Expand Down Expand Up @@ -168,7 +170,7 @@ extension RCImageDecoder {
}

private func fixPerspective(_ data: UnsafeMutablePointer<UInt8>, points: [CGPoint]) throws -> CGImage {
guard let context = generateContext(data: data, size: size), let cgImage = context.makeImage() else {
guard let context = generateContext(data: data, size: size, bytesPerRow: bytesPerRow), let cgImage = context.makeImage() else {
throw RCError.decoding
}
let image = UIGraphicsImageRenderer(size: CGSize(width: CGFloat(size), height: CGFloat(size))).image { context in
Expand All @@ -195,7 +197,7 @@ extension RCImageDecoder {

private func decode(_ image: CGImage) -> [RCBit] {
let pixelData = UnsafeMutableRawPointer.allocate(byteCount: image.width * image.height, alignment: MemoryLayout<UInt8>.alignment)
let context = generateContext(data: pixelData, size: image.width)
let context = generateContext(data: pixelData, size: image.width, bytesPerRow: image.width)
context?.draw(image, in: CGRect(origin: .zero, size: CGSize(width: image.width, height: image.height)))
let buffer = UnsafeMutableBufferPointer<UInt8>(start: pixelData.assumingMemoryBound(to: UInt8.self), count: image.width * image.height)
let data = PixelContainer(rows: image.height, items: buffer)
Expand Down Expand Up @@ -225,8 +227,8 @@ extension RCImageDecoder {
}

extension RCImageDecoder {
private func generateContext(data: UnsafeMutableRawPointer?, size: Int) -> CGContext? {
return CGContext(data: data, width: size, height: size, bitsPerComponent: 8, bytesPerRow: size, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue)
private func generateContext(data: UnsafeMutableRawPointer?, size: Int, bytesPerRow: Int) -> CGContext? {
return CGContext(data: data, width: size, height: size, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue)
}
}

Expand Down
19 changes: 9 additions & 10 deletions Sources/Public/RCCameraViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ public final class RCCameraViewController: UIViewController, UIImagePickerContro

//MARK: Public properties
public weak var delegate: RCCameraViewControllerDelegate?
public var configuration = RCCoderConfiguration.defaultConfiguration
public var configuration = RCCoderConfiguration.shortConfiguration
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
.portrait
}
public override var prefersStatusBarHidden: Bool {
true
}
//Private properties
private lazy var coder: RCCoder = {
RCCoder(configuration: self.configuration)
}()
private lazy var coder: RCCoder = RCCoder(configuration: self.configuration)
private var captureSession = AVCaptureSession()
private var videoPreviewLayer: AVCaptureVideoPreviewLayer?
private var maskLayer = CAShapeLayer()
Expand Down Expand Up @@ -147,7 +145,7 @@ extension RCCameraViewController {
guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
do {
captureSession.sessionPreset = .hd1280x720
coder.set(size: 720)
coder.size = 720
calculateScanArea()
let input = try AVCaptureDeviceInput(device: captureDevice)
input.device.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 30)
Expand All @@ -162,11 +160,11 @@ extension RCCameraViewController {
}

private func calculateScanArea() {
let actualWidth = view.frame.height / 16 * 9
var sideArea: CGFloat = min(view.bounds.width, view.bounds.height) * 0.9 * 0.2
sideArea += (actualWidth - min(view.bounds.width, view.bounds.height) * 0.9) / 2
let area = sideArea / min(view.bounds.width, view.bounds.height) * 720
coder.set(scanArea: Int(area))
// let actualWidth = view.frame.height / 16 * 9
// var sideArea: CGFloat = min(view.bounds.width, view.bounds.height) * 0.9 * 0.2
// sideArea += (actualWidth - min(view.bounds.width, view.bounds.height) * 0.9) / 2
// let area = sideArea / min(view.bounds.width, view.bounds.height) * 720
// coder.set(scanArea: Int(area))
}

private func configureVideoPreview(orientation: AVCaptureVideoOrientation = .portrait) {
Expand Down Expand Up @@ -194,6 +192,7 @@ extension RCCameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
let lumaBaseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)?.advanced(by: bytesPerRow * origin)
let lumaCopy = UnsafeMutableRawPointer.allocate(byteCount: bytesPerRow * size, alignment: MemoryLayout<UInt8>.alignment)
lumaCopy.copyMemory(from: lumaBaseAddress!, byteCount: bytesPerRow * size)
coder.bytesPerRow = bytesPerRow
if let message = try? coder.decode(buffer: lumaCopy.assumingMemoryBound(to: UInt8.self)) {
delegate?.cameraViewController(self, didFinishPickingScanning: message)
dismiss(animated: true)
Expand Down
20 changes: 12 additions & 8 deletions Sources/Public/RCCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public final class RCCoder {
private lazy var imageDecoder = RCImageDecoder(size: 720, configuration: self.configuration)
private lazy var imageEncoder = RCImageEncoder(configuration: self.configuration)
private lazy var bitCoder = RCBitCoder(configuration: self.configuration)
internal var size: Int {
get { imageDecoder.size}
set { imageDecoder.size = newValue }
}
internal var padding: Int {
get { imageDecoder.padding }
set { imageDecoder.padding = newValue }
}
internal var bytesPerRow: Int {
get { imageDecoder.bytesPerRow }
set { imageDecoder.bytesPerRow = newValue }
}

public init(configuration: RCCoderConfiguration = .shortConfiguration) {
self.configuration = configuration
Expand Down Expand Up @@ -60,12 +72,4 @@ extension RCCoder {
let message = try bitCoder.decode(bits)
return message
}

func set(size: Int) {
imageDecoder.size = size
}

func set(scanArea: Int) {
imageDecoder.padding = scanArea
}
}

0 comments on commit e982753

Please sign in to comment.