- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用 Swift 构建一个二维码扫描仪,在这方面一切正常。我遇到的问题是,我试图让整个可见 AVCaptureVideoPreviewLayer
的一小部分区域能够扫描二维码。我发现,为了指定屏幕的哪个区域能够读取/捕获 QR 码,我必须使用名为 rectOfInterest
的 AVCaptureMetadataOutput
属性。问题是当我将它分配给 CGRect 时,我无法扫描任何东西。在网上做了更多研究后,我发现一些建议我需要使用一种名为 metadataOutputRectOfInterestForRect
的方法将 CGRect 转换为属性 rectOfInterest
可以实际使用的正确格式。但是,我现在遇到的一个大问题是,当我使用此方法时 metadataoutputRectOfInterestForRect
我收到一个错误,指出 CGAffineTransformInvert: singular matrix
。谁能告诉我为什么会出现此错误?我相信我正在根据 Apple 开发人员文档正确使用此方法,并且我相信我需要根据我在网上找到的所有信息来使用此方法来实现我的目标。我将包含到目前为止找到的文档的链接以及我用来扫描二维码的函数的代码示例
代码示例
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
}
// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input as! AVCaptureInput)
// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)
// calculate a centered square rectangle with red border
let size = 300
let screenWidth = self.view.frame.size.width
let xPos = (CGFloat(screenWidth) / CGFloat(2)) - (CGFloat(size) / CGFloat(2))
let scanRect = CGRect(x: Int(xPos), y: 150, width: size, height: size)
// create UIView that will server as a red square to indicate where to place QRCode for scanning
scanAreaView = UIView()
scanAreaView?.layer.borderColor = UIColor.redColor().CGColor
scanAreaView?.layer.borderWidth = 4
scanAreaView?.frame = scanRect
view.addSubview(scanAreaView!)
// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
captureMetadataOutput.rectOfInterest = videoPreviewLayer!.metadataOutputRectOfInterestForRect(scanRect)
view.layer.addSublayer(videoPreviewLayer)
// Start video capture.
captureSession?.startRunning()
// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()
qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
qrCodeFrameView?.layer.borderWidth = 2
view.addSubview(qrCodeFrameView!)
view.bringSubviewToFront(qrCodeFrameView!)
// Add a button that will be used to close out of the scan view
videoBtn.setTitle("Close", forState: .Normal)
videoBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
videoBtn.backgroundColor = UIColor.grayColor()
videoBtn.layer.cornerRadius = 5.0;
videoBtn.frame = CGRectMake(10, 30, 70, 45)
videoBtn.addTarget(self, action: "pressClose:", forControlEvents: .TouchUpInside)
view.addSubview(videoBtn)
view.bringSubviewToFront(scanAreaView!)
}
请注意导致错误的兴趣行是这样的:captureMetadataOutput.rectOfInterest = videoPreviewLayer!.metadataOutputRectOfInterestForRect(scanRect)
我尝试过的其他事情是直接将 CGRect 作为参数传递,这导致了同样的错误。我还传入了 scanAreaView!.bounds
作为参数,因为它确实是我正在寻找的确切大小/区域,并且也会导致同样的错误。我已经在其他人的在线代码示例中看到过这样做,他们似乎没有我遇到的错误。以下是一些示例:
Xcode AVCapturesession scan Barcode in specific frame (rectOfInterest is not working)
Apple 文档
metadataOutputRectOfInterestForRect
我正在用作指定区域的 scanAreaView 的图像我试图使视频预览层的唯一可扫描区域:
最佳答案
我真的无法阐明 metadataOutputRectOfInterestForRect 的问题,但是,您也可以直接设置该属性。您需要预先指定视频的宽度和高度分辨率。我很快使用了 640*480 设置。如文档中所述,这些值必须是
"extending from (0,0) in the top left to (1,1) in the bottom right, relative to the device’s natural orientation".
下面是我试过的代码
var x = scanRect.origin.x/480
var y = scanRect.origin.y/640
var width = scanRect.width/480
var height = scanRect.height/640
var scanRectTransformed = CGRectMake(x, y, width, height)
captureMetadataOutput.rectOfInterest = scanRectTransformed
我刚刚在 iOS 设备上对其进行了测试,它似乎可以正常工作。
编辑
至少我已经解决了 metadataOutputRectOfInterestForRect 问题。我相信您必须在相机已正确设置并正在运行后执行此操作,因为相机的分辨率尚不可用。
首先在viewDidLoad()中添加一个通知观察者方法
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("avCaptureInputPortFormatDescriptionDidChangeNotification:"), name:AVCaptureInputPortFormatDescriptionDidChangeNotification, object: nil)
然后添加如下方法
func avCaptureInputPortFormatDescriptionDidChangeNotification(notification: NSNotification) {
captureMetadataOutput.rectOfInterest = videoPreviewLayer.metadataOutputRectOfInterestForRect(scanRect)
}
然后您可以在此处重置 rectOfInterest 属性。然后,在您的代码中,您可以在 didOutputMetadataObjects 函数中显示 AVMetadataObject
var rect = videoPreviewLayer.rectForMetadataOutputRectOfInterest(YourAVMetadataObject.bounds)
dispatch_async(dispatch_get_main_queue(),{
self.qrCodeFrameView.frame = rect
})
我试过了,矩形总是在指定区域内。
关于ios - 如何使用 metadataOutputRectOfInterestForRect 方法和 rectOfInterest 属性扫描特定区域? (二维码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32401364/
使用 C# (VS2008) 和 WIA - 扫描到 TIFF 格式; 当我在平板或文档进纸器上使用扫描仪扫描 1 页时,该方法执行没有任何问题。当我将多个表单加载到进纸器时,扫描第一页后执行停止(保
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
给定一个列表 :: [(Foo, Bar)] ,我想在 Bar 上执行 scanl1 s,但保留他们的 Foo “标签”。 IE。我想要一个类型为 :: [(a, b)] -> ([b] -> [c]
我有一个 HBase 表,我需要从多个范围获取结果。例如,我可能需要从不同范围获取数据,例如第 1-6 行、100-150..... 我知道对于每次扫描,我可以定义开始行和停止行。但是如果我有 6 个
我看到了这段代码。我是 C 语言的新手,所以请原谅。 while下面的循环将继续循环 if i = SIZE,则 == 是无关紧要的,因为它根本不会被执行。如果 i 小于 SIZE 那么 scanf(
这是一个关于编译过程的相当技术性的问题ABAP代码。 我知道有ABAP解析器和扫描器类实际上调用 C 内核函数来完成实际工作。然后就是代码补全事务的功能,该事务以 ABAP 列表或 XML 的形式返回
给定以下程序: int main(){ float x = non_det_float(); float y = NAN; if (isnan(y) && x == 1.0f){
我在工作中使用由供应商生成的二维码。实际上我需要通过网站手动记录所有这些项目。 QR 码包含所有这些数据,所以我想创建一个自动执行操作的应用。 例如,二维码表示“AAA|BBB|CCC|123”。我想
我有一个像这样的字符串:@"ololo width: 350px jijiji width:440px ... text=12... "我想将@"width: "之后的所有数字替换为280。所以在扫描
我在玩 scanf 时遇到了一个小问题……更具体地说,我想读取整个输入,然后忽略其余部分。让我告诉你我的意思: #include int main(void) { int number_of
我正在使用 matlab/octave 创建扫描/线性调频信号,我的结束信号似乎以错误的频率结束。我该如何修复它,以便信号以正确的频率结束。 PS:我不能在 Octave 音程中使用 chirp 命令
我正在寻找一个可以扫描 WiFi 网络并打印所有 SSID 的程序。我试过 scapy 但我失败了。我正在使用 pyCharm 编辑器。 我试过这段代码: from scapy.all import
概述 Linux 完全是用于大型服务器的最流行和最安全的操作系统之一。尽管它被广泛使用,但它仍然容易受到网络攻击。黑客以服务器为目标,窃取有价值的信息。所以迫切需要开发反黑客方法来应对安全漏洞和恶
如何获取我的 Git 存储库的某种统计信息? 我目前在 BitBucket 中托管 Git 存储库,想查找以下详细信息: 提交总数 使用过的编程语言 每种编程语言的总代码行数 您认为这可以实现吗?还是
我目前正在使用以下代码来扫描作为申请表的一部分上传的文件: $safe_path = escapeshellarg($dir . $file); $command = '/usr/bin/clamsc
我在存储库中有十几个项目。存储库结构如下所示: / ------- + project1 +------- trunk +------- tags +----
我正在使用 Dynamo DB 并想使用过滤器扫描一个表。例如,是否可以使用全局二级索引仅扫描表中的特定行? 最佳答案 这不可能!扫描始终针对基表中的所有行,当您扫描索引表作为响应时,您将仅获得该索引
我正在尝试从这里使用 SOLStumbler:Accessing & Using the MobileWiFi.framework扫描 wifi 网络。我知道苹果不支持这一点,但它是用于教育目的和实验
我知道 iPhone 蓝牙功能在 3.0 之前无法通过 SDK 访问,但是需要多长时间才能找到该区域的设备?它取决于该区域的设备数量吗?如果范围内有大约 5 个设备,扫描发现所有设备是否需要花费 30
我正在使用Elasticsearch 6.2,并且有一些查询可以分析大量文档。我正在对索引内的一个字段进行排序。 Elasticsearch检查10.000个文档(默认配置值),然后将它们分页返回。
我是一名优秀的程序员,十分优秀!