gpt4 book ai didi

ios - 如何切换IOS相机

转载 作者:行者123 更新时间:2023-11-29 05:35:08 25 4
gpt4 key购买 nike

我已经在 swift 中实现了一个相机应用程序,但从前置摄像头切换到后置摄像头无法正常工作

这是我已经实现的代码

import UIKit
import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var ImageView: UIView!

var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var backCamera = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
var frontCamera = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .front)
var capturePhotoOut : AVCapturePhotoOutput?

override func viewDidLoad() {
super.viewDidLoad()

if #available(iOS 10.2, *){
let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
do{
let input = try AVCaptureDeviceInput(device: captureDevice!)
captureSession = AVCaptureSession()
captureSession?.addInput(input)
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.frame = view.layer.bounds
ImageView.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()
}catch{
print("error")
}
}

capturePhotoOut = AVCapturePhotoOutput()
capturePhotoOut?.isHighResolutionCaptureEnabled = true
captureSession?.sessionPreset = .photo
captureSession?.addOutput(capturePhotoOut!)
capturePhotoOut!.isDepthDataDeliveryEnabled = capturePhotoOut!.isDepthDataDeliverySupported
capturePhotoOut!.isPortraitEffectsMatteDeliveryEnabled = capturePhotoOut!.isPortraitEffectsMatteDeliverySupported
}

func switchToFrontCamera(){
if frontCamera?.isConnected == true {
captureSession?.stopRunning()
let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .front)
do{

let input = try AVCaptureDeviceInput(device: captureDevice!)
captureSession = AVCaptureSession()
captureSession?.addInput(input)
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.frame = view.layer.bounds
ImageView.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()

}catch{
print("Error")
}
}else{
print("noFrontCamera")
}

}

func switchToBackCamera(){
if backCamera?.isConnected == true {
captureSession?.stopRunning()
let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
do{
let input = try AVCaptureDeviceInput(device: captureDevice!)
captureSession = AVCaptureSession()
captureSession?.addInput(input)
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.frame = view.layer.bounds
ImageView.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()

}catch{
print("Error")
}
}else{
print("noFrontCamera")
}

}


@IBAction func rotateCamera(_ sender: Any) {
guard let currentCameraInput: AVCaptureInput = captureSession?.inputs.first else {
return
}

if let input = currentCameraInput as? AVCaptureDeviceInput{

if input.device.position == .back {
switchToFrontCamera()
}

if input.device.position == .front{
switchToBackCamera()
}

}

}


}

应用程序开始工作时会找到前置摄像头,但按下旋转按钮时不会切换到后置摄像头

最佳答案

You can use the following code as i edited your code:

class MyView: UIView {
//MARK: overriding the layerClass to return `AVCaptureVideoPreviewLayer`.
override class var layerClass: AnyClass {
return AVCaptureVideoPreviewLayer.self
}
override var layer: AVCaptureVideoPreviewLayer {
return super.layer as! AVCaptureVideoPreviewLayer
}
}

class ViewController: UIViewController {

@IBOutlet weak var ImageView: MyView!
var captureSession: AVCaptureSession?
var capturePhotoOut : AVCapturePhotoOutput?
var captureDevice : AVCaptureDevice?

var videoPreviewLayer: AVCaptureVideoPreviewLayer {
return ImageView.layer
}

override func viewDidLoad() {
super.viewDidLoad()
captureDevice = AVCaptureDevice.default(for: .video)
doInitialSetup(with: captureDevice!)
}

func doInitialSetup(with videoCaptureDevice : AVCaptureDevice) {
if #available(iOS 10.2, *){
do{
let input = try AVCaptureDeviceInput(device: videoCaptureDevice)
captureSession = AVCaptureSession()
captureSession?.addInput(input)
videoPreviewLayer.frame = ImageView.bounds
videoPreviewLayer.session = captureSession
captureSession?.startRunning()
}catch{
print("error")
}
}

capturePhotoOut = AVCapturePhotoOutput()
capturePhotoOut?.isHighResolutionCaptureEnabled = true
captureSession?.sessionPreset = .photo
captureSession?.addOutput(capturePhotoOut!)
capturePhotoOut!.isDepthDataDeliveryEnabled = capturePhotoOut!.isDepthDataDeliverySupported
if #available(iOS 12.0, *) {
capturePhotoOut!.isPortraitEffectsMatteDeliveryEnabled = capturePhotoOut!.isPortraitEffectsMatteDeliverySupported
} else {
// Fallback on earlier versions
}
}

@IBAction func rotateCamera(_ sender: Any) {
captureSession?.stopRunning()
if self.captureDevice?.position == .back {
self.captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
}else if self.captureDevice?.position == .front{
self.captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
}
doInitialSetup(with: self.captureDevice!)
}
}

关于ios - 如何切换IOS相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57108253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com