- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为我目前正在开发的应用程序设计条形码扫描仪。我希望扫描仪预览填满设备的整个屏幕,并提供一个较小的框架来指向条形码。一切都按照我想要的方式工作,但我无法让感兴趣的框架工作。
下面是条码扫描器的实现:
#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];
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[_device lockForConfiguration:&error];
if (error) {
NSLog(@"Error: %@", error);
}
_device.focusPointOfInterest = CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height / 2) - 80);
_input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
if (_input) {
[_session addInput:_input];
} else {
NSLog(@"Error: %@", error);
}
_output = [[AVCaptureMetadataOutput alloc] init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
_output.rectOfInterest = CGRectMake((self.view.frame.size.width / 2) - 160, (self.view.frame.size.height / 2) - 160, 320, 160);
[_session addOutput:_output];
_output.metadataObjectTypes = [_output availableMetadataObjectTypes];
_prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_prevLayer.frame = self.view.bounds;
_prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:_prevLayer];
_greyView = [[UIView alloc] initWithFrame:self.view.frame];
_greyView.bounds = self.view.bounds;
_greyView.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
[self.view.layer addSublayer:_greyView.layer];
_scopeView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width / 2) - 160, (self.view.frame.size.height / 2) - 160, 320, 160)];
_scopeView.backgroundColor = [UIColor clearColor];
_scopeView.layer.borderColor = [UIColor greenColor].CGColor;
_scopeView.layer.borderWidth = 1;
_scopeView.clipsToBounds = YES;
[self.view addSubview:_scopeView];
_highlightView = [[UIView alloc] init];
_highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
_highlightView.layer.borderColor = [UIColor greenColor].CGColor;
_highlightView.layer.borderWidth = 3;
[_scopeView addSubview:_highlightView];
[_session startRunning];
[self.view bringSubviewToFront:_highlightView];
[self.view bringSubviewToFront:_label];
}
我正在使用 _output.rectOfInterest 将框架指定为与 _scopeView 的框架相同。不幸的是,这是行不通的。如果我这样做,则不再识别条形码。
最佳答案
一拿到手,就很清楚了:
AVCaptureMetadataOutput 由像素定义,因此要将显示器的坐标映射到输出中的坐标,我必须使用 metadataOutputRectOfInterestForRect:
From AVCaptureOutput.h:
/*!
@method metadataOutputRectOfInterestForRect:
@abstract
Converts a rectangle in the receiver's coordinate space to a rectangle of interest in the coordinate space of an AVCaptureMetadataOutput
whose capture device is providing input to the receiver.
@param rectInOutputCoordinates
A CGRect in the receiver's coordinates.
@result
A CGRect in the coordinate space of the metadata output whose capture device is providing input to the receiver.
@discussion
AVCaptureMetadataOutput rectOfInterest is expressed as a CGRect where {0,0} represents the top left of the picture area,
and {1,1} represents the bottom right on an unrotated picture. This convenience method converts a rectangle in
the coordinate space of the receiver to a rectangle of interest in the coordinate space of an AVCaptureMetadataOutput
whose AVCaptureDevice is providing input to the receiver. The conversion takes orientation, mirroring, and scaling into
consideration. See -transformedMetadataObjectForMetadataObject:connection: for a full discussion of how orientation and mirroring
are applied to sample buffers passing through the output.
*/
- (CGRect)metadataOutputRectOfInterestForRect:(CGRect)rectInOutputCoordinates NS_AVAILABLE_IOS(7_0);
在使用它来设置 rectOfInterest 后,它起作用了。
关于ios - Xcode AVCapturesession 扫描特定帧中的条码(rectOfInterest 不工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26634036/
我正在开发一个视频录制应用程序,需要能够使用蓝牙麦克风作为音频输入(如果已连接)。 我有以下代码来配置 AVCaptureSession 的音频输入: self.captureSession.uses
对于这个“愚蠢”的问题,我提前表示歉意,但我觉得我已经用尽了所有资源。我对 Swift 和一般编码几乎没有经验,但根据过去的经验和基于对象的编程(如 MAX MSP)的使用,我了解了很多。 我正在尝试
我正在尝试将捕获的帧记录为视频,同时并行地对帧执行图像处理任务。 我有一个 AVCaptureSession,我已将两个单独的输出添加到 - AVCaptureVideoDataOutput AVCa
当我尝试构建我的应用程序时,XCode 向我显示此错误 AVCaptureSession 之前应有“(” 有人可以帮我解决这个警告吗?这是我的代码: ViewController.h #import
我能够根据 http://developer.apple.com/iphone/library/qa/qa2010/qa1702.html 使用 AVCaptureSession 从相机捕获视频帧。但
我正在开发一个处理高清照片的应用程序。我正在使用 AVCaptureSession 拍照,停止它,然后对该照片应用效果。 让我疯狂的是,一切都运行良好,仪器告诉我,我正确且按时地释放了我使用的所有内存
我正在构建一个应用程序,允许用户使用 iPhone 相机拍照,并在可用时使用 AVFoundation (iOS4),以便用户可以使用点击对焦功能,即使使用自定义叠加层也是如此。 我遇到的问题是cap
所以这是一个普遍问题,但我们正在寻找有关如何让实时相机 View 能够在顶部添加图像并拍照的线索。所以基本上你可以选择像“帽子”一样覆盖在相机上的图像,调整其大小和位置,然后拍照,“帽子”将出现在你拍
我正在做一个 iOS 应用程序,需要使用这样的层次结构对 QR 码进行验证: View ---Scan View ---Image View - cardBG ---Inside View 加载 Vi
我正在尝试使用 AVCaptureSession 制作自定义相机。 代码和一切工作正常。唯一的问题是 VideoOutput 层被缩放。这使我的照片默认缩放。 我尝试了每件事,但无法找到解决方案。这是
我正在使用 AVCaptureSession 创建带有 AVCaptureMetadataOutput 的 QR 码扫描仪。 一切都按预期工作,但是我想在扫描仪上放置图形叠加层。这样做时,我希望扫描仪
弹出 View Controller 时,我在使用 AVCaptureSession 时遇到一些困难。我在导航 Controller 中有一个 View Controller ,用户可以在其中拍照。捕
有人知道如何使用 AVFoundation (AVCaptureStillImageOutput) 在自定义 iOS 相机中设置自定义分辨率吗? 我知道您可以使用 AVCaptureSession 选
我需要做这样的事情。我的应用程序使用 AVCapturesession 进行录制,但它应该能够流式传输带有我播放过的背景音乐的实时提要。 请记住,我可以使用 AVCapturesession 播放背景
当 AVCaptureSession 的 session 运行到本地 NSMutableArray 中捕获图像时,我收到 didReceiveMemoryWarning 调用。稍作测试后,我发现它发生
我正在尝试将 AVCaptureSession 的音频静音和取消静音。开始 session 后,我可以启用和禁用音频连接,但是一旦我播放视频,所有音频部分都会被推回视频的前面,只留下视频的结尾没有声音
我已经为 ios 应用程序添加了二维码阅读器代码现在我想使用可以用作 ios 中的 Today 扩展的相同代码。我已将以下代码用于应用程序和扩展。该应用程序运行良好,但不适用于扩展程序。 我用过htt
如何使用 AVCaptureSession 录制方形视频?如果在录制时不可能,那么我如何在 didFinishRecordingTo 委托(delegate)方法中裁剪它? 提前致谢! 最佳答案 您可
我收到一条警告: (sending "ViewController *const_strong' to parameter of incompatible type 'id' 执行这行代码时 [out
在我的应用程序中,我录制长达 30 秒的视频。我使用以下行来执行此操作。 [imagePicker setVideoMaximumDuration:30]; 一切正常。然后我决定从 UIImagePi
我是一名优秀的程序员,十分优秀!