Barcode on swift 4

I figured it out but Apple didn’t make it so obvious. The callback function from the delegate AVCaptureMetadataOutputObjectsDelegate has been renamed and the parameter names are different! So, replace func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) to func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) My view controller is now … Read more

How to capture picture with AVCaptureSession in Swift?

AVCaptureSession Sample import UIKit import AVFoundation class ViewController: UIViewController { let captureSession = AVCaptureSession() let stillImageOutput = AVCaptureStillImageOutput() var error: NSError? override func viewDidLoad() { super.viewDidLoad() let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) && $0.position == AVCaptureDevicePosition.Back } if let captureDevice = devices.first as? AVCaptureDevice { captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error)) captureSession.sessionPreset = AVCaptureSessionPresetPhoto captureSession.startRunning() stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG] … Read more

Turn on torch/flash on iPhone

Here’s a shorter version you can now use to turn the light on or off: AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) { [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn]; // use AVCaptureTorchModeOff to turn off [device unlockForConfiguration]; } UPDATE: (March 2015) With iOS 6.0 and later, you can control the brightness or level of the torch … Read more

How to crop an image from AVCapture to a rect seen on the display

In Swift 3: private func cropToPreviewLayer(originalImage: UIImage) -> UIImage { let outputRect = previewLayer.metadataOutputRectConverted(fromLayerRect: previewLayer.bounds) var cgImage = originalImage.cgImage! let width = CGFloat(cgImage.width) let height = CGFloat(cgImage.height) let cropRect = CGRect(x: outputRect.origin.x * width, y: outputRect.origin.y * height, width: outputRect.size.width * width, height: outputRect.size.height * height) cgImage = cgImage.cropping(to: cropRect)! let croppedUIImage = UIImage(cgImage: cgImage, … Read more

Why AVCaptureSession output a wrong orientation?

Take a look at the header AVCaptureSession.h. There is a definition for an enum called AVCaptureVideoOrientation that defines various video orientations. On the AVCaptureConnection object there is a property called videoOrientation that is a AVCaptureVideoOrientation. You should be able to set this to change the orientation of the video. You probably want AVCaptureVideoOrientationLandscapeRight or AVCaptureVideoOrientationLandscapeLeft. … Read more

Switch cameras with avcapturesession

You first need to remove the existing AVCameraInput from the AVCaptureSession and then add a new AVCameraInput to the AVCaptureSession. The following works for me (under ARC): -(IBAction)switchCameraTapped:(id)sender { //Change camera source if(_captureSession) { //Indicate that some changes will be made to the session [_captureSession beginConfiguration]; //Remove existing input AVCaptureInput* currentCameraInput = [_captureSession.inputs objectAtIndex:0]; [_captureSession … Read more

AVCaptureVideoPreviewLayer orientation – need landscape

Swift 5.5, Xcode 13.2 private func updatePreviewLayer(layer: AVCaptureConnection, orientation: AVCaptureVideoOrientation) { layer.videoOrientation = orientation self.previewLayer?.frame = view.bounds } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() if let connection = self.previewLayer?.connection { let currentDevice = UIDevice.current let orientation: UIDeviceOrientation = currentDevice.orientation let previewLayerConnection: AVCaptureConnection = connection if previewLayerConnection.isVideoOrientationSupported { switch orientation { case .portrait: self.updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait) … Read more

Trying to understand CMTime

A CMTime struct represents a length of time that is stored as rational number (see CMTime Reference). CMTime has a value and a timescale field, and represents the time value/timescale seconds . CMTimeMake is a function that returns a CMTime structure, for example: CMTime t1 = CMTimeMake(1, 10); // 1/10 second = 0.1 second CMTime … Read more