gpt4 book ai didi

ios - Swift 3 非翻转前置摄像头图片

转载 作者:行者123 更新时间:2023-11-30 12:33:33 24 4
gpt4 key购买 nike

我的前置摄像头有问题,由于某种原因它会翻转照片。我尝试使用 imageView.transform = CGAffineTransform(scaleX: -1, y: 1) 来避免这个问题并且它有效。但我意识到我也在翻转后置摄像头。有什么解决办法吗?

ImagePreviewViewController:

    @IBAction func close(_ sender: Any) {

let cameraViewController = storyboard?.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController
navigationController?.pushViewController(cameraViewController, animated: true)

}
@IBOutlet weak var close: UIButton!
var capturedImage : UIImage?

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
imageView.image = capturedImage
close.setImage(UIImage(named: "Close.png"), for: UIControlState.normal)
//imageView.transform = CGAffineTransform(scaleX: -1, y: 1)
}

CameraViewController:

@IBOutlet var cameraView: UIView!
let session = AVCaptureSession()
var stillImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
var camera : AVCaptureDevice?
var cameraPreviewLayer : AVCaptureVideoPreviewLayer?
var cameraCaptureOutput : AVCapturePhotoOutput?
var frontCamera: Bool = false

override func viewDidLoad() {
super.viewDidLoad()
session.sessionPreset = AVCaptureSessionPresetPhoto
frontCamera(frontCamera)
if camera != nil{

beginSession()

}
initializeCaptureSession()
shoot.setImage(UIImage(named: "Shoot.png"), for: UIControlState.normal)
// Do any additional setup after loading the view, typically from a nib.
}

func beginSession(){

cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
self.cameraView.layer.addSublayer(cameraPreviewLayer!)
cameraPreviewLayer?.frame = self.cameraView.layer.bounds
cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
session.startRunning()
stillImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
if session.canAddOutput(stillImageOutput){
session.addOutput(stillImageOutput)

}
}

func frontCamera(_ front: Bool){

let devices = AVCaptureDevice.devices()

do{

try session.removeInput(AVCaptureDeviceInput(device: camera))

} catch {

print("error")
}

for device in devices!{

if((device as AnyObject).hasMediaType(AVMediaTypeVideo)) {
if front{
if (device as AnyObject).position == AVCaptureDevicePosition.front {

camera = device as? AVCaptureDevice

do{
try session.addInput(AVCaptureDeviceInput(device: camera))
} catch {


}
break

}
} else {


}
}



}
}

func displayCapturedPhoto(capturedPhoto : UIImage) {

let imagePreviewViewController = storyboard?.instantiateViewController(withIdentifier: "ImagePreviewViewController") as! ImagePreviewViewController
imagePreviewViewController.capturedImage = capturedPhoto
navigationController?.pushViewController(imagePreviewViewController, animated: true)
}


@IBOutlet weak var shoot: UIButton!
@IBAction func takePicture(_ sender: Any) {

takePicture()

}

func initializeCaptureSession() {

session.sessionPreset = AVCaptureSessionPresetHigh
camera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

do {
let cameraCaptureInput = try AVCaptureDeviceInput(device: camera!)
cameraCaptureOutput = AVCapturePhotoOutput()

session.addInput(cameraCaptureInput)
session.addOutput(cameraCaptureOutput)

} catch {
print(error.localizedDescription)
}

cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
cameraPreviewLayer?.frame = view.bounds
cameraPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait

view.layer.insertSublayer(cameraPreviewLayer!, at: 0)

session.startRunning()
}

func takePicture() {
let settings = AVCapturePhotoSettings()
settings.flashMode = .off
cameraCaptureOutput?.capturePhoto(with: settings, delegate: self as AVCapturePhotoCaptureDelegate)
}


@IBOutlet weak var flash: UIButton!
@IBAction func flash(_ sender: Any) {



}

@IBAction func switchCamera(_ sender: Any) {

frontCamera = !frontCamera
session.beginConfiguration()
let inputs = session.inputs as! [AVCaptureInput]
for oldInput: AVCaptureInput in inputs{
session.removeInput(oldInput)

}
frontCamera(frontCamera)

session.commitConfiguration()
}

}

扩展CameraViewController:AVCapturePhotoCaptureDelegate {

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

if let unwrappedError = error {
print(unwrappedError.localizedDescription)
} else {

if let sampleBuffer = photoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) {

if let finalImage = UIImage(data: dataImage) {

displayCapturedPhoto(capturedPhoto: finalImage)
}
}
}
}

override var prefersStatusBarHidden: Bool {
return true
}

}

最佳答案

尝试将连接的 isVideoMirrored 属性设置为 true。这将翻转图像。

func initializeCaptureSession() {

...

cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
cameraPreviewLayer?.frame = view.bounds
cameraPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewLayer?.connection.isVideoMirrored = true

view.layer.insertSublayer(cameraPreviewLayer!, at: 0)

session.startRunning()

}

关于ios - Swift 3 非翻转前置摄像头图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146255/

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