录制视频(帧数版本) IOS
来源:互联网 发布:a5淘宝客助手 编辑:程序博客网 时间:2024/04/27 23:45
//
// CameraHelp.h
//
//
// Created by Zhuang Chuan Xian. on 11-6-28.
// Copyright 2011 . All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#undef PRODUCER_HAS_VIDEO_CAPTURE
#define PRODUCER_HAS_VIDEO_CAPTURE (__IPHONE_OS_VERSION_MIN_REQUIRED >= 40000 && TARGET_OS_EMBEDDED)
@protocol CameraHelpDelegate
-(void) getSampleBufferImage:(UIImage *) v_image ;
@end
@interface CameraHelp : NSObject
#if PRODUCER_HAS_VIDEO_CAPTURE
<AVCaptureVideoDataOutputSampleBufferDelegate>
#endif
{
@private
int mWidth;
int mHeight;
int mFps;
BOOL mFrontCamera;
BOOL mFirstFrame;
BOOL mStarted;
UIView* mPreview;
id<CameraHelpDelegate> outDelegate;
#if PRODUCER_HAS_VIDEO_CAPTURE
AVCaptureSession* mCaptureSession;
AVCaptureDevice *mCaptureDevice;
#endif
}
//单例模式
+ (CameraHelp*)shareCameraHelp;
+ (void)closeCamera;
//设置前置摄像头
- (BOOL)setFrontCamera;
//设置后置摄像头
- (BOOL)setBackCamera;
//开始前设置捕获参数
- (void)prepareVideoCapture:(int) width andHeight: (int)height andFps: (int) fps andFrontCamera:(BOOL) bfront andPreview:(UIView*) view;
//开始捕获
- (void)startVideoCapture;
//停止捕获
- (void)stopVideoCapture;
//设置要显示到得View
- (void)setPreview: (UIView*)preview;
//设置数据输出
- (void)setVideoDataOutputBuffer:(id<CameraHelpDelegate>)delegate;
@end
-------------------------------------------------------------------------
//
// CameraHelp.m
//
//
// Created by zcx. on 11-6-28.
// Copyright 2011 . All rights reserved.
//
#import "CameraHelp.h"
//
// Private
//
@interface CameraHelp (Private)
#if PRODUCER_HAS_VIDEO_CAPTURE
+(AVCaptureDevice *)cameraAtPosition:(AVCaptureDevicePosition)position;
- (void)startPreview;
- (void)stopPreview;
#endif
@end
@implementation CameraHelp (Private)
#if PRODUCER_HAS_VIDEO_CAPTURE
+ (AVCaptureDevice *)cameraAtPosition:(AVCaptureDevicePosition)position{
NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in cameras){
if (device.position == position){
return device;
}
}
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
- (void)startPreview{
if(mCaptureSession && mPreview && mStarted){
AVCaptureVideoPreviewLayer* previewLayer = [AVCaptureVideoPreviewLayer layerWithSession: mCaptureSession];
previewLayer.frame = mPreview.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// if(previewLayer.orientationSupported){
// previewLayer.orientation = mOrientation;
// }
[mPreview.layer addSublayer: previewLayer];
if(![mCaptureSession isRunning]){
[mCaptureSession startRunning];
}
}
}
- (void)stopPreview{
if(mCaptureSession){
if([mCaptureSession isRunning]){
[mCaptureSession stopRunning];
// remove all sublayers
if(mPreview){
for(CALayer *ly in mPreview.layer.sublayers){
if([ly isKindOfClass: [AVCaptureVideoPreviewLayer class]])
//
//
//
//
//
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#undef PRODUCER_HAS_VIDEO_CAPTURE
#define PRODUCER_HAS_VIDEO_CAPTURE (__IPHONE_OS_VERSION_MIN_REQUIRED >= 40000 && TARGET_OS_EMBEDDED)
@protocol CameraHelpDelegate
-(void) getSampleBufferImage:(UIImage *) v_image ;
@end
@interface CameraHelp : NSObject
#if PRODUCER_HAS_VIDEO_CAPTURE
<AVCaptureVideoDataOutput
#endif
{
@private
#if PRODUCER_HAS_VIDEO_CAPTURE
#endif
}
//单例模式
+ (CameraHelp*)shareCameraHelp;
+ (void)closeCamera;
//设置前置摄像头
- (BOOL)setFrontCamera;
//设置后置摄像头
- (BOOL)setBackCamera;
//开始前设置捕获参数
- (void)prepareVideoCapture:(int) width andHeight: (int)height andFps: (int) fps andFrontCamera:(BOOL) bfront andPreview:(UIView*) view;
//开始捕获
- (void)startVideoCapture;
//停止捕获
- (void)stopVideoCapture;
//设置要显示到得View
- (void)setPreview: (UIView*)preview;
//设置数据输出
- (void)setVideoDataOutputBuffer
@end
-------------------------------------------------------------------------
//
//
//
//
//
//
//
#import "CameraHelp.h"
//
//
//
@interface CameraHelp (Private)
#if PRODUCER_HAS_VIDEO_CAPTURE
+(AVCaptureDevice *)cameraAtPosition:(AVCaptureDevicePosition)position;
- (void)startPreview;
- (void)stopPreview;
#endif
@end
@implementation CameraHelp (Private)
#if PRODUCER_HAS_VIDEO_CAPTURE
+ (AVCaptureDevice *)cameraAtPosition:(AVCaptureDevicePosition)position{
}
- (void)startPreview{
//
//
//
}
- (void)stopPreview{