首相介紹相關(guān)的類
AVCaptureSession
AVCaptureSession
它是管理捕獲活動(dòng)并協(xié)調(diào)來自輸入設(shè)備的數(shù)據(jù)流以捕獲輸出的對(duì)象启搂。
要執(zhí)行實(shí)時(shí)或離線捕獲,您需要實(shí)例化一個(gè) AVCaptureSession
對(duì)象并添加適當(dāng)?shù)妮斎耄ɡ?AVCaptureDeviceInput
)和輸出(例如 AVCaptureMovieFileOutput
)港柜。 以下代碼片段說明了如何配置捕獲設(shè)備以記錄音頻:
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
if (audioInput) {
[captureSession addInput:audioInput];
}
else {
// Handle the failure.
}
AVCaptureDevice
為捕獲會(huì)話提供輸入(例如音頻或視頻)并提供針對(duì)硬件特定捕獲功能的控制的設(shè)備请契。
// 方法1
// Choose the back dual camera if available, otherwise default to a wide angle camera.
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInDuoCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
if ( ! videoDevice ) {
// If the back dual camera is not available, default to the back wide angle camera.
videoDevice = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
// In some cases where users break their phones, the back wide angle camera is not available. In this case, we should default to the front wide angle camera.
if ( ! videoDevice ) {
videoDevice = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionFront];
}
}
// 新方法1
+ (instancetype)discoverySessionWithDeviceTypes:(NSArray<AVCaptureDeviceType> *)deviceTypes mediaType:(NSString *)mediaType position:(AVCaptureDevicePosition)position;
// 方法2
AVCaptureDevice *device = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureInput
用于向捕獲會(huì)話提供輸入數(shù)據(jù)的對(duì)象的抽象超類。
要將AVCaptureInput對(duì)象與會(huì)話關(guān)聯(lián)夏醉,請(qǐng)?jiān)跁?huì)話上調(diào)用 addInput:
爽锥。
AVCaptureInput對(duì)象具有一個(gè)或多個(gè)端口(AVCaptureInputPort的實(shí)例),它們可以生成每個(gè)數(shù)據(jù)流一個(gè)畔柔。 例如救恨,呈現(xiàn)一個(gè)視頻數(shù)據(jù)流的AVCaptureDevice對(duì)象具有一個(gè)端口。
AVCaptureDeviceInput
AVCaptureDeviceInput
它是捕獲輸入類释树,提供從捕獲設(shè)備到捕獲會(huì)話(AVCaptureSession
)的媒體。
AVCaptureDeviceInput
是AVCaptureInput
的一個(gè)具體子類擎淤,用于從AVCaptureDevice
對(duì)象捕獲數(shù)據(jù)奢啥。
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if ( ! videoDeviceInput ) {
NSLog( @"Could not create video device input: %@", error );
return;
}
if ( [self.session canAddInput:videoDeviceInput] ) {
[self.session addInput:videoDeviceInput];
}
AVCaptureOutput
AVCaptureOutput
是描述 AVCaptureSession
對(duì)象的輸出目標(biāo)的抽象基類。
AVCaptureOutput
提供了一個(gè)抽象接口嘴拢,用于將捕獲輸出目標(biāo)(例如文件和視頻預(yù)覽)連接到捕獲會(huì)話(AVCaptureSession
的實(shí)例)桩盲。 捕獲輸出可以具有由AVCaptureConnection
對(duì)象表示的多個(gè)連接,每個(gè)連接對(duì)象從捕獲輸入(AVCaptureInpu
t的實(shí)例)接收的每個(gè)媒體流席吴。 捕獲輸出在首次創(chuàng)建時(shí)沒有任何連接赌结。 將輸出添加到捕獲會(huì)話時(shí),將創(chuàng)建將媒體數(shù)據(jù)從該會(huì)話的輸入映射到其輸出的連接孝冒。
您可以使用addOutput(_ :)
將具體的AVCaptureOutput
實(shí)例添加到捕獲會(huì)話柬姚。
// 照片輸出
AVCapturePhotoOutput *photoOutput = [[AVCapturePhotoOutput alloc] init];
if ( [self.session canAddOutput:photoOutput] ) {
[self.session addOutput:photoOutput];
}
//視頻輸出
AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
if ( [self.session canAddOutput:movieFileOutput] )
{
[self.session beginConfiguration];
[self.session addOutput:movieFileOutput];
self.session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
if ( connection.isVideoStabilizationSupported ) {
connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}
[self.session commitConfiguration];
}
// 元數(shù)據(jù)
AVCaptureMetadataOutput *output2 = [[AVCaptureMetadataOutput alloc] init];
[_session addOutput:output2];
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output2 setMetadataObjectsDelegate:self queue:queue];
AVCaptureVideoPreviewLayer
核心動(dòng)畫層,可以在捕獲視頻時(shí)顯示庄涡。
AVCaptureVideoPreviewLayer
是CALayer
的子類量承,用于在視頻被輸入設(shè)備捕獲時(shí)用于顯示視頻。
您可以將此預(yù)覽圖層與AV捕獲會(huì)話結(jié)合使用,如以下代碼段所示:
AVCaptureSession *captureSession = <#Get a capture session#>;
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView *aView = <#The view in which to present the layer#>;
previewLayer.frame = aView.bounds; // Assume you want the preview layer to fill the view.
[aView.layer addSublayer:previewLayer];
二維碼掃描
主要代碼:
NSError *error = nil;
// 初始化會(huì)話
_session = [[AVCaptureSession alloc] init];
// 設(shè)置會(huì)話設(shè)置輸出質(zhì)量
_session.sessionPreset = AVCaptureSessionPresetMedium;
// 獲取需要的設(shè)備
AVCaptureDevice *device = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (!input) {
// Handling the error appropriately.
}
[_session addInput:input];
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
//
AVCaptureMetadataOutput *output2 = [[AVCaptureMetadataOutput alloc] init];
[_session addOutput:output2];
[output2 setMetadataObjectsDelegate:self queue:queue];
// 設(shè)置為二維碼類型(AVMetadataObjectTypeQRCode)撕捍,
// 還有其他類型拿穴,可以去接口文件里查看
[output2 setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// Start the session running to start the flow of data
[_session startRunning];
//預(yù)覽層的生成,實(shí)時(shí)獲取攝像頭數(shù)據(jù)
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_session];
self.previewLayer.frame = CGRectMake(0, 64, 300, 304);
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:self.previewLayer];
代理:<AVCaptureMetadataOutputObjectsDelegate>
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
if (metadataObjects.count > 0) {
//獲得掃描數(shù)據(jù)忧风,最后一個(gè)是最新掃描的數(shù)據(jù)
AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
// 結(jié)果 object.stringValue
dispatch_async(dispatch_get_main_queue(), ^{
_label.text = object.stringValue;
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
_label.text = @"沒有掃描到數(shù)據(jù)";
});
}
}