When I use ios system camera, I can select the square camera type. I want to implement this on swift, but how can I do that? Below is the code I use for camera. The initialSession() is used to create the AVCaptureSession object. setUpCameraLayer() method is to show the camera on the view. How can I use square camera on below code?
func initialSession(){
self.session = AVCaptureSession()
do {
try self.videoInput = AVCaptureDeviceInput(device: self.backCamera())
} catch {
}
self.stillImageOutput = AVCaptureStillImageOutput()
self.stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if self.session!.canAddInput(self.videoInput){
self.session?.addInput(self.videoInput)
}
if self.session!.canAddOutput(self.stillImageOutput){
self.session?.addOutput(self.stillImageOutput)
}
}
func setUpCameraLayer(){
if self.previewLayer == nil {
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
self.backView.frame = CGRectMake(0, 0, self.view.bounds.width,self.view.bounds.width)
let view:UIView = self.backView
let viewLayer:CALayer = view.layer
viewLayer.masksToBounds = true
let bounds:CGRect = view.bounds
self.previewLayer?.frame = bounds
self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill//AVLayerVideoGravityResizeAspect
//viewLayer.insertSublayer(self.previewLayer!.below: viewLayer.sublayers![0])
view.layer.addSublayer(self.previewLayer!)
self.picture.frame = self.backView.frame
self.backView.addSubview(self.picture)
self.picture.hidden = true
}
}
Related
I'm currently working on a camera app. Everything worked fine, but when I tried to change the constraints of the Vision View the log suddenly printed this error.
[warning]the specified colorspace format is not supported. falling back on libyuv.
I have no Idea where it comes from and what I should change. Below I'll past the relevant code where I set up the camera.
func initializeCameraSession() {
// Set up Values
//1: Create a new AV Session
// , xf , AVCaptureVideoDataOutputSampleBufferDelegate // Get camera devices
let devices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .front).devices
//2: Select a capture device
avSession.sessionPreset = .low
do {
if let captureDevice = devices.first {
let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
}
avSession.beginConfiguration()
if avSession.canAddInput(captureDeviceInput) {
avSession.addInput(captureDeviceInput)
self.videoDeviceInput = captureDeviceInput
} else {
print("Couldn't add video device input to the session.")
avSession.commitConfiguration()
return
}
avSession.commitConfiguration()
}
} catch {
print(error.localizedDescription)
}
//3: Show output on a preview layer
let captureOutput = AVCaptureVideoDataOutput()
captureOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as String) : NSNumber(value: kCVPixelFormatType_32BGRA )]
avSession.addOutput(captureOutput)
let previewLayer = AVCaptureVideoPreviewLayer(session: avSession)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.connection?.videoOrientation = .portrait
previewLayer.frame = visionView.bounds
visionView.layer.addSublayer(previewLayer)
view.bringSubviewToFront(visionView)
visionView.isHidden = true
visionView.alpha = 0.0
avSession.startRunning()
}
}
I am trying to add a button on top of my AVCapturesession preview layer. this button is to capture images (fire the capture function)..however i can not seem to set one up.
func configureCaptureSession() {
captureSession.sessionPreset = AVCaptureSession.Preset.photo
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .unspecified)
for device in deviceDiscoverySession.devices {
if device.position == .back {
rearCam = device
} else if device.position == .front {
frontCam = device
}
}
currentDevice = rearCam
guard let captureDeviceInput = try? AVCaptureDeviceInput(device: currentDevice) else {
return
}
// wheelOverlay.image = UIImage(named: "wheel")
// wheelOverlayLayer.contents = wheelOverlay
stillPhotoOutput = AVCapturePhotoOutput()
captureSession.addInput(captureDeviceInput)
captureSession.addOutput(stillPhotoOutput)
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.frame = view.layer.frame
print("Capture session setup")
// view.layer.addSublayer(wheelOverlayLayer)
// view.layer.insertSublayer(wheelOverlayLayer, above: cameraPreviewLayer)
captureSession.startRunning()
}
where abouts do i need to add the subview?
You need to add it to the view after you add the cameraLayer like this
view.layer.addSublayer(cameraPreviewLayer)
let bt = UIButton(type:.system)
bt.setTitle("Record",for:.normal)
bt.frame = /////
view.addSubview(bt)
I'm trying to first just get my app to show the camera preview of the wide angle camera.
But what I do it just doesn't show up. (the app has video and audio permission and is being tested on an iPhone 7 Plus)
Here's my session configuration code:
func configureSession() throws {
session.beginConfiguration()
// configure inputs
let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
let audioDevice = AVCaptureDevice.default(.builtInMicrophone, for: .audio, position: .unspecified)
guard let videoInput = try? AVCaptureDeviceInput(device: videoDevice!), let audioInput = try? AVCaptureDeviceInput(device: audioDevice!), session.canAddInput(videoInput), session.canAddInput(audioInput) else {
throw CameraError.configurationFailed
}
// configure outputs
let output = AVCaptureMovieFileOutput()
let previewOutput = AVCaptureVideoDataOutput()
previewOutput.alwaysDiscardsLateVideoFrames = true
previewOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32BGRA)]
guard session.canAddOutput(output) && session.canAddOutput(previewOutput) else {
throw CameraError.configurationFailed
}
session.sessionPreset = .high
session.addOutput(output)
session.addOutput(previewOutput)
session.commitConfiguration()
}
and this is the code for the previewLayer:
// adds given view as previewView to the session
func configureAsPreview(_ view: UIView) {
let previewLayer = AVCaptureVideoPreviewLayer(session: session)
view.layer.addSublayer(previewLayer)
previewLayer.frame = view.layer.frame
}
and it is being called in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if cameraManager.hasPermissionForVideo {
try! cameraManager.configureSession()
cameraManager.configureAsPreview(self.previewView)
cameraManager.startSession()
} else {
performSegue(withIdentifier: "presentPermissions", sender: nil)
}
}
What am I doing wrong?
I really can't figure it out and it's probably just something stupidly small.
Thanks.
Try update layer frame when needed, and use bounds instead of frame. Layers dont support autoresizing, you need to manage it manually.
private var __previewLayer = AVCaptureVideoPreviewLayer? = nil
func configureAsPreview(_ view: UIView) {
let previewLayer = AVCaptureVideoPreviewLayer(session: session)
self.view.layer.addSublayer(previewLayer)
previewLayer.frame = self.view.bounds
self.__previewLayer = previewLayer
}
override viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.__previewLayer?.frame = self.view.bounds
}
I'm trying to get a camera view for a camera app to show. After messing with the setup camera view code without success. I think it has something to do with my GLKView setup code. The UIView in the storyboard shows up on the app but its blank (white). I don't get any errors.
I'm building this for at least iOS 7.0.
I added both the createGLKView and the setupCameraView blocks just incase!
EDIT: I forgot to set an outlet to the UIView. It shows now, but the camera view is black. My background is white so I know that the camera view is showing up black.
func createGLKView() {
if (self.context != nil) {
return
}
self.context = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
//.init(API:EAGLRenderingAPI.OpenGLES2)
var view: GLKView = GLKView(frame: self.bounds)
//.init(frame: bounds.self)
view.autoresizingMask = UIViewAutoresizing(2) | UIViewAutoresizing(5)
view.translatesAutoresizingMaskIntoConstraints()
view.context = self.context
view.contentScaleFactor = 1.0
view.drawableDepthFormat = GLKViewDrawableDepthFormat.Format24
self.insertSubview(view, atIndex: 0)
glkView = view
glGenRenderbuffers(1, &renderBuffer)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), renderBuffer)
coreImageContext = CIContext(EAGLContext: self.context)
EAGLContext.setCurrentContext(self.context)
println("GLKView setup")
}
func setupCameraView() {
self.createGLKView()
var possibleDevices: NSArray = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
//var device: AVCaptureDevice = possibleDevices.firstObject as! AVCaptureDevice
var device: AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
/*
if device != true {
println("device returned!")
return
}
*/
let devices = AVCaptureDevice.devices()
// Loop through all the capture devices on this phone
for camera in devices {
// Make sure this particular device supports video
if (camera.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(camera.position == AVCaptureDevicePosition.Back) {
device = camera as! AVCaptureDevice
}
}
}
imageDetectionConfidence = 0.0
var session: AVCaptureSession = AVCaptureSession()
self.captureSession = session
session.beginConfiguration()
self.captureDevice = device
/*
for camera in possibleDevices {
if camera.position == AVCaptureDevicePosition.Back {
device = camera as! AVCaptureDevice
println("back camera selected!")
break
}
}
*/
var error: NSError?
/*
var input: AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as! AVCaptureDeviceInput
session.sessionPreset = AVCaptureSessionPresetPhoto
session.addInput(input)
*/
if let input: AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as? AVCaptureDeviceInput {
session.sessionPreset = AVCaptureSessionPresetPhoto
session.addInput(input)
}
var dataOutput: AVCaptureVideoDataOutput = AVCaptureVideoDataOutput()
dataOutput.alwaysDiscardsLateVideoFrames = true
//dataOutput.videoSettings = NSDictionary(object: Int(kCVPixelFormatType_32BGRA), forKey: kCVPixelBufferPixelFormatTypeKey as String) as [NSObject: AnyObject]
dataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA]
dataOutput.setSampleBufferDelegate(self, queue: dispatch_get_main_queue())
session.addOutput(dataOutput)
self.stillImageOutput = AVCaptureStillImageOutput()
session.addOutput(self.stillImageOutput)
var connection: AVCaptureConnection = (dataOutput.connections as NSArray).firstObject as! AVCaptureConnection
connection.videoOrientation = AVCaptureVideoOrientation.Portrait
if device.flashAvailable {
device.lockForConfiguration(nil)
device.flashMode = AVCaptureFlashMode.Off
device.unlockForConfiguration()
if device.isFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus) {
device.lockForConfiguration(nil)
device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus
device.unlockForConfiguration()
}
}
session.commitConfiguration()
}
Assuming that the code is correct, you could try to bring your subview to the front. I've found that sometimes when adding a new subview it isn't always shown above the others and gets hidden behind them.
self.bringSubviewToFront(view)
EDIT: It might help for others to know what iOS version you are programming for.
In an app I'm developing the user is required to take a "selfie" (Yes, I know, but the app is for private use only).
I've got everything working with the camera showing in the circular UIView region, however I cannot get it to scale and fill the circle properly. Here's what it's doing now:
And here's what I want it to be doing:
Here's the code for my UIView:
var cameraView = UIView()
cameraView.frame = CGRectMake(100, self.view.center.y-260, 568, 568)
cameraView.backgroundColor = UIColor(red:26/255, green:188/255, blue:156/255, alpha:1)
cameraView.layer.cornerRadius = 284
cameraView.layer.borderColor = UIColor.whiteColor().CGColor
cameraView.layer.borderWidth = 15
cameraView.contentMode = UIViewContentMode.ScaleToFill
cameraView.layer.masksToBounds = true
I have tried a few different contentMode options, include ScaleToFill, ScaleAspectFill, and ScaleAspectFit. They all generate the same exact result.
As it turns out, the camera's "self.previewLayer" has a property that determines how the camera's content fills a View.
In the following code I changed "self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect" to "self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill"
extension SelfieViewController: AVCaptureVideoDataOutputSampleBufferDelegate{
func setupAVCapture(){
session.sessionPreset = AVCaptureSessionPreset640x480
let devices = AVCaptureDevice.devices();
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the front camera
if(device.position == AVCaptureDevicePosition.Front) {
captureDevice = device as? AVCaptureDevice
if captureDevice != nil {
beginSession()
break
}
}
}
}
}
func beginSession(){
var err : NSError? = nil
var deviceInput:AVCaptureDeviceInput = AVCaptureDeviceInput(device: captureDevice, error: &err)
if err != nil {
println("error: \(err?.localizedDescription)")
}
if self.session.canAddInput(deviceInput){
self.session.addInput(deviceInput)
}
self.videoDataOutput = AVCaptureVideoDataOutput()
var rgbOutputSettings = [NSNumber(integer: kCMPixelFormat_32BGRA):kCVPixelBufferPixelFormatTypeKey]
self.videoDataOutput.alwaysDiscardsLateVideoFrames=true
self.videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL)
self.videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue)
if session.canAddOutput(self.videoDataOutput){
session.addOutput(self.videoDataOutput)
}
self.videoDataOutput.connectionWithMediaType(AVMediaTypeVideo).enabled = true
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
var rootLayer :CALayer = self.cameraView.layer
rootLayer.masksToBounds=true
self.previewLayer.frame = rootLayer.bounds
rootLayer.addSublayer(self.previewLayer)
session.startRunning()
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
// do stuff here
}
// clean up AVCapture
func stopCamera(){
session.stopRunning()
}
}