我正在用Swift构建一个二维码扫描器,一切都在这方面有效.我遇到的问题是我试图使整个可见的一小部分区域AVCaptureVideoPreviewLayer能够扫描QR码.我发现,为了指定屏幕的哪个区域能够读取/捕获QR码,我必须使用AVCaptureMetadataOutput被调用的属性rectOfInterest.麻烦的是,当我将其分配给CGRect时,我无法扫描任何内容.在网上做了更多的研究后,我发现有些人建议我需要使用一个方法来调用metadataOutputRectOfInterestForRectCGRect,将其转换为属性rectOfInterest实际可以使用的正确格式.但是,metadataoutputRectOfInterestForRect我现在遇到的一个大问题是,当我使用这种方法时,我收到一个错误CGAffineTransformInvert: singular matrix.谁能告诉我为什么我会收到这个错误?我相信我根据Apple开发人员文档正确使用这种方法,我相信我需要根据我在网上找到的所有信息来实现我的目标.我将包括到目前为止我找到的文档的链接以及我用来扫描QR码的函数的代码示例
代码示例
func startScan() {
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
// as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
var error:NSError?
let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)
if (error != nil) {
// If any error occurs, …Run Code Online (Sandbox Code Playgroud) 我正在构建一个UIView具有覆盖框的盒子,最终目标是让 QR 码阅读器仅在 QR 落在盒子内时才触发。我知道我需要将 设置.rectOfInterest()为与黄色框相同,但是在当前实现中(下面的代码),读取器不起作用。
public override init(frame: CGRect) {
super.init(frame: frame)
if let captureDevice = AVCaptureDevice.default(for: .video) {
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
session.addInput(input)
} catch {
print("Error")
}
let scannerRect = CGRect(x: self.center.x - (self.frame.width * 0.667 / 2), y: self.frame.width * 0.667 / 4, width: self.frame.width * 0.667, height: self.frame.width * 0.667)
let output = AVCaptureMetadataOutput()
output.rectOfInterest = scannerRect
session.addOutput(output)
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
output.metadataObjectTypes = [.qr]
video = AVCaptureVideoPreviewLayer(session: …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我正在使用的App设计条形码扫描仪.我希望扫描仪预览能够填满设备的整个屏幕,并提供一个较小的框架来指向条形码.所有的工作都是我想要的,但我不能让感兴趣的框架工作.
这是条形码扫描仪的实现:
#import "GEScannerViewController.h"
@import AVFoundation;
@interface GEScannerViewController () <AVCaptureMetadataOutputObjectsDelegate> {
AVCaptureSession *_session;
AVCaptureDevice *_device;
AVCaptureDeviceInput *_input;
AVCaptureMetadataOutput *_output;
AVCaptureVideoPreviewLayer *_prevLayer;
UIView *_greyView;
UIView *_highlightView;
UIView *_scopeView;
UILabel *_label;
}
@end
@implementation GEScannerViewController
- (void)viewDidLoad {
[super viewDidLoad];
_label = [[UILabel alloc] init];
_label.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40);
_label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
_label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
_label.text = @"(none)";
[self.view addSubview:_label];
NSError *error = nil;
_session = [[AVCaptureSession alloc] init]; …Run Code Online (Sandbox Code Playgroud)