ios学习--camera capture

原创 2012年03月31日 10:43:10




    a)ACCaptureDevice 用于设备的定义,既camera device。



输入和输出都不是一对一的,如video output可以同时又video+audio的input。



AVCaptureSession *session = <#A capture session#>;
[session beginConfiguration];
[session removeInput:frontFacingCameraDeviceInput];
[session addInput:backFacingCameraDeviceInput];
[session commitConfiguration];

 添加capture input:

To add a capture device to a capture session, you use an instance of AVCaptureDeviceInput (a concrete
subclass of the abstract AVCaptureInput class). The capture device input manages the device’s ports.
NSError *error = nil;
AVCaptureDeviceInput *input =
[AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.


To get output from a capture session, you add one or more outputs. An output is an instance of a concrete
subclass of AVCaptureOutput; you use:
● AVCaptureMovieFileOutput to output to a movie file
● AVCaptureVideoDataOutput if you want to process frames from the video being captured
● AVCaptureAudioDataOutput if you want to process the audio data being captured
● AVCaptureStillImageOutput if you want to capture still images with accompanying metadata

You add outputs to a capture session using addOutput:. You check whether a capture output is compatible
with an existing session using canAddOutput:. You can add and remove outputs as you want while the
session is running.
AVCaptureSession *captureSession = <#Get a capture session#>;
AVCaptureMovieFileOutput *movieInput = <#Create and configure a movie output#>;
if ([captureSession canAddOutput:movieInput]) {
[captureSession addOutput:movieInput];
else {

// Handle the failure.


保存一个video文件,既添加video file output:

You save movie data to a file using an AVCaptureMovieFileOutputobject. (AVCaptureMovieFileOutput
is a concrete subclass of AVCaptureFileOutput, which defines much of the basic behavior.) You can configure
various aspects of the movie file output, such as the maximum duration of the recording, or the maximum file
size. You can also prohibit recording if there is less than a given amount of disk space left.
AVCaptureMovieFileOutput *aMovieFileOutput = [[AVCaptureMovieFileOutput alloc]
CMTime maxDuration = <#Create a CMTime to represent the maximum duration#>;
aMovieFileOutput.maxRecordedDuration = maxDuration;
aMovieFileOutput.minFreeDiskSpaceLimit = <#An appropriate minimum given the quality
of the movie format and the duration#>;


处理preview video frame数据,既每一帧的view finder数据,可以用于后续的一些高级处理,如人脸检测等等:

An AVCaptureVideoDataOutput object uses delegation to vend video frames. You set the delegate using
setSampleBufferDelegate:queue:. In addition to the delegate, you specify a serial queue on which they
delegate methods are invoked. You must use a serial queue to ensure that frames are delivered to the delegate
in the proper order. You should not pass the queue returned by dispatch_get_current_queue since there
is no guarantee as to which thread the current queue is running on. You can use the queue to modify the
priority given to delivering and processing the video frames.

对于frame数据的处理,必须有大小的限制(image size)和处理时间的限制,如果处理了时间过长,则底层sensor将不会送数据给layouter和这个回调。

You should set the session output to the lowest practical resolution for your application. Setting the output
to a higher resolution than necessary wastes processing cycles and needlessly consumes power.
You must ensure that your implementation of
captureOutput:didOutputSampleBuffer:fromConnection: is able to process a sample buffer within
the amount of time allotted to a frame. If it takes too long, and you hold onto the video frames, AV Foundation
will stop delivering frames, not only to your delegate but also other outputs such as a preview layer.



AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc]
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,
AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];


If you want to capture a JPEG image, you should typically not specify your own compression format. Instead,
you should let the still image output do the compression for you, since its compression is hardware-accelerated.
If you need a data representation of the image, you can use jpegStillImageNSDataRepresentation: to
get an NSData object without re-compressing the data, even if you modify the image’s metadata.


Camera preview显示:

You can provide the user with a preview of what’s being recorded using an AVCaptureVideoPreviewLayer
object. AVCaptureVideoPreviewLayer is a subclass ofCALayer (see Core Animation Programming Guide .
You don’t need any outputs to show the preview.

AVCaptureSession *captureSession = <#Get a capture session#>;
CALayer *viewLayer = <#Get a layer from the view in which you want to present the
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer
alloc] initWithSession:captureSession];
[viewLayer addSublayer:captureVideoPreviewLayer];

In general, the preview layer behaves like any other CALayer object in the render tree (see Core Animation
Programming Guide ). You can scale the image and perform transformations, rotations and so on just as you
would any layer. One difference is that you may need to set the layer’s orientation property to specify how
it should rotate images coming from the camera. In addition, on iPhone 4 the preview layer supports mirroring
(this is the default when previewing the front-facing camera).



android6.0源码分析之Camera API2.0下的Capture流程分析



API 21中将原来的camera API弃用转而推荐使用新增的camera2 API,这是一个大的动作,因为新API换了架构,让开发者用起来更难了。 先来看看camera2包架构示意图: 这...
  • lincyang
  • lincyang
  • 2015年05月24日 15:19
  • 50234

Android Camera HAL3中拍照Capture模式下多模块间的交互与帧Result与帧数据回调

前沿:         前面博文大多少总结的是Camera HAL1到HAL3的系统架构,但这些架构对于Camera APP开发来说依旧还是处于Camera API1.0的标准。而随着Camera3...
  • sadamoo
  • sadamoo
  • 2016年06月02日 20:33
  • 872

Camera2 打开相机预览界面

camrea2 api打卡相机预览界面

Android Camera HAL3中拍照Capture模式下多模块间的交互与帧Result与帧数据回调

本文均属自己阅读源码的点滴总结,转账请注明出处谢谢。欢迎和大家交流。qq:1037701636 email:gzzaigcn2009@163.comSoftware:系统源码Android5.1前沿:...


  • 2003年04月30日 00:00
  • 676KB
  • 下载

ios学习--How to capture video frames from the camera as images using AV Foundation

Q:  How do I capture video frames from the camera as images using AV Foundation? A: How do I captur...

V4L2 camera 驱动 capture测试程序

在网上找了一个测试程序, 看了看,是根据capture.c修改的。测试步骤如下 1. gcc -o capture_image capture_image.c 2. ctrl+alt+f1 切换到...
  • kickxxx
  • kickxxx
  • 2011年04月20日 16:07
  • 7786

Capture Image from Camera and Display in Activity

问: I want to write a module where on a click of a button the camera opens and I can click and captu...
  • Koupoo
  • Koupoo
  • 2012年05月04日 11:23
  • 356

.net中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)

前言     随着Windows操作系统的不断演变,用于捕获视频的API接口也在进化,微软提供了VFW、DirectShow和MediaFoundation这三代接口。其中VFW早已被DirectS...
您举报文章:ios学习--camera capture