gpt4 book ai didi

ios - AVCaptureStillImageOutput 和 UIImagePNGRepresentation

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

我在做一些我认为不应该那么困难的事情上遇到了困难,所以我想我一定是从错误的角度看待问题。为了了解 AVCaptureStillImageOutput 和相机的工作原理,我制作了一个小应用程序。

这个应用程序能够拍摄照片并将其保存为 PNG 文件(我不需要 JPEG)。下次启动应用程序时,它会检查文件是否存在,如果存在,则将文件中存储的图像用作应用程序的背景 View 。这个想法相当简单。

问题是它不起作用。如果有人能告诉我我做错了什么,那将会非常有帮助。

我希望图片以拍摄时在显示屏上的方式显示为背景,但它被旋转或比例错误......等等。

这是相关代码(如果需要,我可以提供更多信息)。

viewDidLoad 方法:

override func viewDidLoad() {
super.viewDidLoad()

// For the photo capture:
captureSession.sessionPreset = AVCaptureSessionPresetHigh

// Select the appropriate capture devices:
for device in AVCaptureDevice.devices() {
// Make sure this particular device supports video.
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera.
if(device.position == AVCaptureDevicePosition.Back) {
captureDevice = device as? AVCaptureDevice
}
}
}

tapGesture = UITapGestureRecognizer(target: self, action: Selector("takePhoto"))
self.view.addGestureRecognizer(tapGesture)

let filePath = self.toolBox.getDocumentsDirectory().stringByAppendingPathComponent("BackGroundImage@2x.png")

if !NSFileManager.defaultManager().fileExistsAtPath(filePath) {return}

let bgImage = UIImage(contentsOfFile: filePath),
bgView = UIImageView(image: bgImage)
self.view.addSubview(bgView)
}

处理拍照的方法:

func takePhoto() {
if !captureSession.running {
beginPhotoCaptureSession()
return
}

if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
if error == nil {
var localImage = UIImage(fromSampleBuffer: imageDataSampleBuffer)
UIGraphicsBeginImageContext(localImage!.size)
CGContextRotateCTM (UIGraphicsGetCurrentContext(), CGFloat(M_PI_2))
//localImage!.drawAtPoint(CGPointZero)
localImage!.drawAtPoint(CGPoint(x: -localImage!.size.height, y: -localImage!.size.width))
//localImage!.drawAtPoint(CGPoint(x: -localImage!.size.width, y: -localImage!.size.height))
localImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
localImage = resizeImage(localImage!, toSize: self.view.frame.size)

if let data = UIImagePNGRepresentation(localImage!) {
let bitMapName = "BackGroundImage@2x"
let filename = self.toolBox.getDocumentsDirectory().stringByAppendingPathComponent("\(bitMapName).png")
data.writeToFile(filename, atomically: true)
print("Picture saved: \(bitMapName)\n\(filename)")
}
} else {print("Error on taking a picture:\n\(error)")}
}
}

captureSession.stopRunning()
previewLayer!.removeFromSuperlayer()
}

启动AVCaptureSession的方法:

func beginPhotoCaptureSession() {
do {let input = try AVCaptureDeviceInput(device: captureDevice)
captureSession.addInput(input)
} catch let error as NSError {
// Handle any errors:
print(error)
}

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.frame = self.view.layer.frame
self.view.layer.addSublayer(previewLayer!)
captureSession.startRunning()

stillImageOutput.outputSettings = [kCVPixelBufferPixelFormatTypeKey:Int(kCVPixelFormatType_32BGRA)]
if captureSession.canAddOutput(stillImageOutput) {
captureSession.addOutput(stillImageOutput)
}
}

以下是使用该应用拍摄的照片的示例:

现在这是我重新启动应用程序时得到的背景:

如果工作正常,两张图片将会相似。

最佳答案

我在屏幕截图中看不到旋转。但是比例是一个问题,当您在 function takePhoto 中绘制它时,它与您的代码有关。你可以试试

func takePhoto() {
if !captureSession.running {
beginPhotoCaptureSession()
return
}
if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
if error == nil {
if let data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) {
let bitMapName = "BackGroundImage@2x"
let filename = self.toolBox.getDocumentsDirectory().stringByAppendingPathComponent("\(bitMapName).png")
data.writeToFile(filename, atomically: true)
print("Picture saved: \(bitMapName)\n\(filename)")
}
} else {print("Error on taking a picture:\n\(error)")}
}
}

captureSession.stopRunning()
previewLayer!.removeFromSuperlayer()
}

关于ios - AVCaptureStillImageOutput 和 UIImagePNGRepresentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34646982/

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