gpt4 book ai didi

swift - 如何自定义大小 AVcapture

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

我需要捕获我的条形码但是我的代码捕获是全屏的。如何自定义尺寸或固定为小尺寸。请告诉我自定义尺寸的想法或代码,谢谢。

我的代码捕获是全屏的。

import UIKit
import AVFoundation

protocol BarcodeDelegate {
func barcodeReaded(barcode: String)
}


class barcodeCapViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

var delegate: BarcodeDelegate?
var captureSession: AVCaptureSession!
var code: String?


override func viewDidLoad() {
super.viewDidLoad()

self.captureSession = AVCaptureSession();

let videoCaptureDevice: AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

do {

let videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)

if self.captureSession.canAddInput(videoInput) {
self.captureSession.addInput(videoInput)
} else {
print("Could not add video input")
}

let metadataOutput = AVCaptureMetadataOutput()
if self.captureSession.canAddOutput(metadataOutput) {
self.captureSession.addOutput(metadataOutput)

metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]
} else {
print("Could not add metadata output")
}

let previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
previewLayer.frame = self.view.layer.bounds
self.view.layer .addSublayer(previewLayer)
self.captureSession.startRunning()
} catch let error as NSError {
print("Error while creating vide input device: \(error.localizedDescription)")
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
for metadata in metadataObjects {
let readableObject = metadata as! AVMetadataMachineReadableCodeObject
let code = readableObject.stringValue
if !code.isEmpty {
self.captureSession.stopRunning()
self.dismissViewControllerAnimated(true, completion: nil)
self.delegate?.barcodeReaded(code)
}
}
}

}

当我添加 CGRectMake(20, 40, 200, 50) show this

添加 CGRectMake(20, 40, 500, 100) show this

我不知道为什么宽度和高度不按照代码加起来。

最佳答案

更改 AVCaptureVideoPreviewLayer 的帧大小:

let previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
previewLayer.frame = CGRectMake(10, 20, 100, 50) // something else!

如果你正在使用自动布局,你可能不想处理 CALayer 帧,所以你应该创建一个 UIView 子类,添加你的 AVCaptureVideoPreviewLayer 并在 layoutSubviews 中设置图层的 frame:

override func layoutSubviews() {
super.layoutSubviews()
self.previewLayer.frame = self.frame
}

关于swift - 如何自定义大小 AVcapture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39278476/

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