MF Camera Manager
● manage camera session
● get image from camera
● crop image in a frame
● scan barcode and get the image asynchronously
iOS 8.0+
Xcode 8.1+
Swift 3.0+ (Support Swift 4)
Demo :
CocoaPods :
pod 'MFCameraManager'
or download CustomCameraManagerClass directory and add it manually to your project
Now in your view add a UIView to storyboard view controller or programmetically initiate an UIView which will be your camera preview. then follow steps to display the camera :
If you just need the camera session and capturing image you have to use CameraManager.swift class, as it has StillImageOutput.
but if you need the camera session to scan barcode beside capturing image you have to use ScanBarcodeCameraManager.swift class, as it has StillImageOutput.
Create a variable of which class you prefer (ScanBarcodeCameraManager.swift or CameraManager.swift) :
var cameraManager = CameraManager()
or
var cameraManager = ScanBarcodeCameraManager()
from now on below 4 steps are similar in each class you instantiated :
1 - on your viewDidLoad setup the camera with your UIView created to show the camera preview inside it, and your camera position which its default is back :
cameraManager.captureSetup(in: self.cameraView, with: .back)
2 - in viewWillApear delegate call this method :
cameraManager.startRunning()
3 - in viewDidDisapear delegate call below method :
cameraManager.stopRunning()
4 - to support landscape transitions add below code to viewWillTransition Delegate :
cameraManager.transitionCamera()
self.cameraManager.getcroppedImage { (UIImage, error) in
//your code here to handle with error or if error == nil , get the UIImage
}
self.cameraManager.getcroppedImage(with: self.rectLayer.frame) { (UIImage, error) in
//your code here to handle with error or if error == nil , get the UIImage
}
should conform to protocol ScanBarcodeCameraManagerDelegate and :
self.cameraManager.delegate = self
then implement the function to get the barcode objects and also the captured image :
func scanBarcodeCameraManagerDidRecognizeBarcode(barcode: Array<AVMetadataMachineReadableCodeObject>, image: UIImage?) {
self.scanBarcodeCameraManager.stopRunning()
print(barcode)
// to whatever you like to the barcode objects and the image
scanBarcodeCameraManager.startRunning()
}
cameraManager.enableTorchMode(level: 1)