gpt4 book ai didi

ios - 用于输入和输出的不同摄像机 session 预设

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

我正在创建一个自定义相机 View ,并尝试查看是否有办法为输入和输出设置不同的 session 预设。我希望能够向用户展示高分辨率,但以低分辨率捕获

var captureSession: AVCaptureSession!
var stillImageOutput: AVCapturePhotoOutput!
var videoPreviewLayer: AVCaptureVideoPreviewLayer!
@interface CameraViewController ()
@property (nonatomic) AVCaptureSession *session;
@property (nonatomic) AVCapturePhotoOutput *stillImageOutput;
@property (nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@end

captureSession = AVCaptureSession()
captureSession.sessionPreset = .high // <--show high resolution but capture at low

stillImageOutput = AVCapturePhotoOutput()

if captureSession.canAddInput(input) &&
captureSession.canAddOutput(stillImageOutput) {
captureSession.addInput(input)
captureSession.addOutput(stillImageOutput)
setupLivePreview()
}

func setupLivePreview() {

videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

videoPreviewLayer.videoGravity = .resizeAspect
videoPreviewLayer.connection?.videoOrientation = .portrait
previewView.layer.addSublayer(videoPreviewLayer)
}

最佳答案

也许您可以将捕获的图片调整两次。首先减小图像尺寸,然后将其增大到相同尺寸。分辨率会降低。

示例函数;

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size

let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height

// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}

// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage!
}

关于ios - 用于输入和输出的不同摄像机 session 预设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52449267/

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