转载

iOS-原生扫码登录

AVCaptureDevice

An AVCaptureDevice represents a physical device that provides realtime input media data, such as video and audio.

翻译:一个AVCaptureDevice代表一个物理设备,它提供一个实时的输入媒体数据,例如:视频,音频;

Each instance of AVCaptureDevice corresponds to a device, such as a camera or microphone. Instances of AVCaptureDevice cannot be created directly. An array of all currently available devices can also be obtained using the AVCaptureDeviceDiscoverySession. Devices can provide one or more streams of a given media type. Applications can search for devices matching desired criteria by using AVCaptureDeviceDiscoverySession, or may obtain a reference to the default device matching desired criteria by using +[AVCaptureDevice defaultDeviceWithDeviceType:mediaType:position:].

翻译:每个AVCaptureDevice实例对象都代表着一个设备,如:相机,麦克风;一个AVCaptureDevice实例对象不能被直接创建,可以通过AVCaptureDeviceDiscoverySession获取当前所有可使用的设备;设备可以提供一个或者多个给定的的媒体类型的流;应用程序可以搜索获取设备通过使用AVCaptureDeviceDiscoverySession匹配所需的标准,或可能获得一个引用默认设备通过使用匹配所需的标准通过+[AVCaptureDevice defaultDeviceWithDeviceType:mediaType:position:

AVCaptureSession

AVCaptureSession是AVFoundation的核心类,用于捕捉视频和音频,协调视频和音频的输入和输出流;

To perform a real-time capture, a client may instantiate AVCaptureSession and add appropriate AVCaptureInputs, such as AVCaptureDeviceInput, and outputs, such as AVCaptureMovieFileOutput. [AVCaptureSession startRunning] starts the flow of data from the inputs to the outputs, and [AVCaptureSession stopRunning] stops the flow. A client may set the sessionPreset property to customize the quality level or bitrate of the output.

翻译:执行实时捕获,一个客户可以实例化AVCaptureSession并添加适当AVCaptureInputs,如AVCaptureDeviceInput和输出,如AVCaptureMovieFileOutput。[AVCaptureSession startRunning]启动从输入到输出的数据流,[AVCaptureSession stopRunning]停止流。客户端可以设置sessionPreset属性来定制输出的质量级别或比特率。

AVCaptureInput

AVCaptureInput它是一个抽象类,提供一个实例对象将捕获到的输入源链接到AVCaptureSession上。既然是抽象类我们显然是无法直接使用的,所以我们只能用其子类AVCaptureDeviceInput、AVCaptureScreenInput和AVCaptureMetadataInput来创建;

  • AVCaptureDeviceInput:使用该对象从AVCaptureDevice设备获取媒体数据,该对象将会被添加给AVCaptureSession管理。

  • AVCaptureScreenInput:使用该对象从屏幕获取数据(用于录制屏幕).该对象将会被添加给AVCaptureSeesion管理。

  • AVCaptureMetaDataInput:获取元数据(很少使用).

AVCaptureOutput

AVCaptureOutput:相对应于AVCaptureInput,它也是一个抽象类,用来接收各种输出数据,为AVCaptureSession提供一个输出目标接口。所以我们还是只能使用其子类

      AVCaptureStillImageOutput    //使用AVCapturePhotoOutput替代了
      AVCaptureVideoDataOutput     //可以用来处理被捕获的视频中未压缩或压缩的帧。
      AVCaptureAudioDataOutput     //可以用来处理从音频捕获的未压缩或压缩的样本
      AVCaptureMetadataOutput      //可以用来处理附加连接中的元数据对象
      AVCaptureFileOutput          //文件输出可以开始记录到一个新的文件使用startRecordingToOutputFileURL:recordingDelegate:方法
      AVCapturePhotoOutput         //它支持照片捕捉生活,preview-sized图像传递,广泛的颜色,原生,原生+JPG和 原生+ DNG格式

AVCaptureVideoPreviewLayer

AVCaptureVideoPreviewLayer视频预览层,其实可以把它想象成一个画布,我们通过摄像头拍摄到的画面就显示在这个画布上。AVCaptureVideoPreviewLayer所呈现的画面是连续的,并非单张的静态影像,当然你也可以略过设定 AVCaptureVideoPreviewLayer 的步骤,不显示摄影机所拍摄到的画面,这并不会有任何影响。它是CALayer的子类,它可以实时查看拍照或视频录制效果,需要指定对应的 AVCaptureSession对象;

基础使用

//获取摄像机
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //创建会话对象
    self.session = [[AVCaptureSession alloc] init];
    //设置会话采集率
    self.session.sessionPreset = AVCaptureSessionPresetHigh;
    //创建设备输入流
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//    AVCaptureScreenInput
    //创建数据输出流
    AVCaptureMetadataOutput *metadataOuput = [[AVCaptureMetadataOutput alloc] init];
//    AVCaptureStillImageOutput
//    AVCaptureVideoDataOutput
//    AVCaptureAudioDataOutput
//    AVCaptureMetadataOutput
//    AVCaptureFileOutput
//    AVCapturePhotoOutput
    [metadataOuput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //创建设备输出流
//    AVCaptureVideoDataOutput *videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
//    [videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    //设置扫描范围(每一个取值0~1,以屏幕右上角为坐标原点)
    metadataOuput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6);
    //添加设备输入流到会话对象
    if ([self.session canAddInput:deviceInput]) {
        [self.session addInput:deviceInput];
    }
    //添加设备输出流到会话对象
    if ([self.session canAddOutput:metadataOuput]) {
        [self.session addOutput:metadataOuput];
    }
//    [self.session addOutput:videoDataOutput];
    //设置设备输出类型
    metadataOuput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    //实例化预览图层, 传递_session是为了告诉图层将来显示什么内容
    self.videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    // 保持纵横比;填充层边界
    self.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.videoPreviewLayer.frame = CGRectMake(0, 0, K_Screen_Width, K_Screen_Height);
    [self.view.layer insertSublayer:self.videoPreviewLayer atIndex:0];
    
    // 启动会话
    [self.session startRunning];
//此代理方法为扫码之后获取的二维码信息,在这里可以请求登录
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
}


正文到此结束
Loading...