gpt4 book ai didi

ios - 带相机的UIImageOrientation

转载 作者:行者123 更新时间:2023-12-01 22:26:41 24 4
gpt4 key购买 nike

这是我的相机的代码,如果使用后置摄像头拍摄照片,则将前置摄像头更改为后置摄像头,照片的方向很好(原始),但是如果我使用前置摄像头拍摄照片,方向不好的图片。

class TakeSelfieViewController: UIViewController, AVCapturePhotoCaptureDelegate {

var captureSession = AVCaptureSession()
var photoOutput = AVCapturePhotoOutput()
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?

var sessionOutputSetting = AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecJPEG])

var toggle = false

@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var tempImageView: UIImageView!
@IBOutlet weak var adorButton: UIButton!

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
previewLayer?.frame = cameraView.bounds

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = adorButton.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
adorButton.addSubview(blurEffectView)

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

pickCamera(which: toggle)

}

func pickCamera(which: Bool) {

if (which == true) {

let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)

print("back camera")

startCamera(deviceDesc: deviceDescovery!)

toggle = true

} else if (which == false) {

let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)

print("front camera")

startCamera(deviceDesc: deviceDescovery!)

toggle = false

}

}


func startCamera(deviceDesc: AVCaptureDeviceDiscoverySession!) {

for device in (deviceDesc.devices)! {


if device.position == AVCaptureDevicePosition.back {

do {

let input = try AVCaptureDeviceInput(device: device)
if captureSession.canAddInput(input) {
captureSession.addInput(input)

if captureSession.canAddOutput(photoOutput) {
captureSession.addOutput(photoOutput)


previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait


cameraView.layer.addSublayer(previewLayer!)
captureSession.startRunning()
print("ADD Back")

} else { print("Cannot add input - back") }

}

} catch {

print("Error")

}

} else if (device.position == AVCaptureDevicePosition.front) {

do {

let input = try AVCaptureDeviceInput(device: device)
print(input)
if captureSession.canAddInput(input) {

captureSession.addInput(input)

if captureSession.canAddOutput(photoOutput) {
captureSession.addOutput(photoOutput)


previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait


cameraView.layer.addSublayer(previewLayer!)
captureSession.startRunning()
print("ADD Front")

}

} else { print("Cannot add input - front") }

} catch {

print(error)

}
}
}
}

func didPressTakePhoto() {
if let videoConnection = photoOutput.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecJPEG])
photoOutput.capturePhoto(with: settings, delegate: self)

}
}

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

let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer)
let dataProvider = CGDataProvider(data: imageData as! CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)

let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
self.tempImageView.image = image
self.tempImageView.isHidden = false
self.yellowButton.isHidden = true
self.toggleAction.isHidden = true
self.adorButton.isHidden = true
print("Hola")
}

var didTakePhoto = Bool()

@IBOutlet weak var yellowButton: UIButton!
@IBOutlet weak var toggleAction: UIButton!

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if didTakePhoto {
tempImageView.isHidden = true
yellowButton.isHidden = false
toggleAction.isHidden = false
adorButton.isHidden = false
didTakePhoto = false
print("🍊")

}
}

@IBAction func yellowPressed(_ sender: UIButton) {
captureSession.startRunning()
didTakePhoto = true
didPressTakePhoto()
print("🐶")
}

@IBAction func toggleCamera(_ sender: Any) {

if (toggle == false) {

print("Changing to back camera")

let currentCameraInput: AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput

captureSession.removeInput(currentCameraInput)

toggle = true

pickCamera(which: toggle)

} else if (toggle == true) {

print("Changing to front camera")

let currentCameraInput: AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput

captureSession.removeInput(currentCameraInput)

toggle = false

pickCamera(which: toggle)

}

}

override var prefersStatusBarHidden: Bool {

return true

}
}

我该如何解决? 🤔

最佳答案

用前置摄像头拍摄的图像会被镜像,当您拍摄照片时,图像方向将在其EXIF字典内拍摄,或在元数据字典内传递。
在大多数情况下,当您将其作为JPG或PNG传递时,如果不直接处理,则不会考虑此值。
如果您在风景中拍照,您应该遇到类似的问题。
在捕获方法中,似乎您应该在应注意的情况下将方向强制为固定值。

关于ios - 带相机的UIImageOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41973524/

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