@@ -19,35 +19,15 @@ import W3WOcrSdk
1919class W3WCameraImageProcessor : NSObject , AVCaptureVideoDataOutputSampleBufferDelegate {
2020
2121 // MARK: Vars
22-
22+
2323 /// callback for any new images
2424 var onNewImage : ( CGImage ) -> ( ) = { _ in }
25- //var onNewVideoBuffer: (CMSampleBuffer) -> () = { _ in }
26-
27- /// orientaion of the camera
28- var orientation : AVCaptureVideoOrientation = . portrait
29-
30- /// this monitors device orientation
31- lazy var orientationObserver = W3WOcrOrientationObserver ( )
32-
25+
3326 /// resultution of the camera
3427 var resolution : CGSize ?
3528
3629 /// region to crop output images to
37- var crop : CGRect ?
38-
39-
40- // MARK: Init
41-
42- override init ( ) {
43- super. init ( )
44-
45- self . orientation = orientationObserver. currentOrientationForCamera ( )
46-
47- orientationObserver. onNewOrientation = { [ weak self] orientation in
48- self ? . orientation = orientation
49- }
50- }
30+ var crop : CGRect = . zero
5131
5232
5333 /// sets a crop for all returning images in camera coordinates
@@ -63,43 +43,25 @@ class W3WCameraImageProcessor: NSObject, AVCaptureVideoDataOutputSampleBufferDel
6343
6444 /// called when a new image is aviaable from the camera
6545 public func captureOutput( _ output: AVCaptureOutput , didOutput sampleBuffer: CMSampleBuffer , from connection: AVCaptureConnection ) {
66-
67- // make sure the orientation is correct
68- connection. videoOrientation = orientation
69-
70- // crop the incoming image, and send to whomever is interested
71- if let i = image ( from: sampleBuffer, crop: crop) {
72- onNewImage ( i)
73- }
74- }
75-
76-
77- // MARK: Util
78-
79- /// crop an image and return it as a CGImage
80- /// - Parameters:
81- /// - buffer: image buffer from the camera
82- /// - crop: region to crop to
83- /// - Returns: CGImage of the crop
84- private func image( from buffer: CMSampleBuffer , crop: CGRect ? ) -> CGImage ? {
85- var image : CGImage ?
46+ guard let pixelBuffer = CMSampleBufferGetImageBuffer ( sampleBuffer) else { return }
47+ let width = CVPixelBufferGetWidth ( pixelBuffer)
48+ let height = CVPixelBufferGetHeight ( pixelBuffer)
49+
50+ let cropRect = CGRect (
51+ x: crop. origin. x * CGFloat( width) ,
52+ y: crop. origin. y * CGFloat( height) ,
53+ width: crop. width * CGFloat( width) ,
54+ height: crop. height * CGFloat( height)
55+ )
8656
87- if let imageBuffer = CMSampleBufferGetImageBuffer ( buffer) {
88- var ciimage = CIImage ( cvPixelBuffer: imageBuffer)
89- resolution = CGSize ( width: ciimage. extent. size. width, height: ciimage. extent. size. height)
90- if let c = crop? . intersection ( CGRect ( origin: CGPoint . zero, size: resolution!) ) {
91- ciimage = ciimage. cropped ( to: c)
92- }
93-
94- if let cgImage = CIContext . init ( options: nil ) . createCGImage ( ciimage, from: ciimage. extent) {
95- image = cgImage
96- }
57+ let ciImage = CIImage ( cvPixelBuffer: pixelBuffer)
58+ let cropped = ciImage. cropped ( to: cropRect)
59+
60+ if let cgImage = CIContext ( ) . createCGImage ( cropped, from: cropped. extent) {
61+ onNewImage ( cgImage)
9762 }
98-
99- return image
10063 }
10164
102-
10365 // this makes the OCR work even under the iOS simulator
10466 #if targetEnvironment(simulator)
10567 var fakeImages = W3WOcrFakeImages ( )
0 commit comments