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).


iOS 获取照相机实时预览图片

iOS 获取照相机实时预览
  • ccflying88
  • ccflying88
  • 2016年02月29日 09:42
  • 4189

IOS Dev Intro - Camera Capture The iPhone has shipped w...
  • fanbird2008
  • fanbird2008
  • 2016年06月16日 09:09
  • 398

<input type="file">accept属性 调起手机摄像头 支持Safari Chrome和大部分手机内置浏览器

之前在网上找了很多资料用html调取手机的摄像头,一般在Safari浏览器上都不好用,其实后来发现,标签的accept属性就可以很好的解决这个问题。 1.调起相册  2.调起摄像头...
  • gaomingyangc
  • gaomingyangc
  • 2016年08月18日 15:25
  • 5395


  • sinat_21595363
  • sinat_21595363
  • 2015年03月13日 21:53
  • 1460

QtMultiMedia 之 CameraCapture

QtMultiMedia 之 CameraCapture
  • qyvlik
  • qyvlik
  • 2016年02月19日 23:40
  • 524

用<input type="file" accept="image/*" capture="camera">之后,把图片放到页面上

调用照相机,拍照 调用摄像机,视频 调用录音机,录音
  • Call_me_small_pure
  • Call_me_small_pure
  • 2017年10月23日 13:57
  • 616

在webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能

## 在webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能 在iOS6下开发webapp,使用inputz之file,很有用 ...
  • wmy94827
  • wmy94827
  • 2017年07月20日 21:27
  • 2048

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

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

推荐一个JAVA的拍照Camera Capture调用包

推荐一个JAR包 FOR 64位windows7 的调用摄像头。
  • yizdream
  • yizdream
  • 2014年04月20日 13:56
  • 2100

Remote Packet Capture for iOS Devices

Remote Packet Capture for iOS Devices,Remote Virtual Interfaces
  • AddyChen
  • AddyChen
  • 2015年03月26日 12:53
  • 1243
您举报文章:ios学习--camera capture