我正在用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) 我正在创建一个iPad应用程序,其中一个功能是扫描QR码.我有QR扫描部分工作,但我的问题是iPad屏幕非常大,我将扫描一张纸的小QR码,同时可以看到许多QR码.我想指定显示器的较小区域是唯一可以实际捕获QR码的区域,因此用户更容易扫描他们想要的特定QR码.
我目前已经制作了一个带有红色边框的临时UIView,它以页面为中心,作为我希望用户扫描QR码的示例.它看起来像这样:

我已经到处寻找一个答案,我可以如何定位AVCaptureVideoPreviewLayer的特定区域来收集QR码数据,我发现建议使用"rectOfInterest"和AVCaptureMetadataOutput.我试图这样做,但是当我将rectOfInterest设置为与我用于正确显示的UIView相同的坐标和大小时,我无法扫描/识别任何QR码.有人可以告诉我为什么可扫描区域与看到的UIView的位置不匹配,如何让rectOfInterest在我添加到屏幕的红色边框内?
这是我目前使用的扫描功能的代码:
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, simply log the description of it and don't continue any more.
println("\(error?.localizedDescription)")
return
} …Run Code Online (Sandbox Code Playgroud)