录制视频(帧数版本) IOS

时间:2022-09-09 15:52:57
//
//   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
<AVCaptureVideoDataOutput SampleBufferDelegate>
#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 defaultDeviceWithMediaTy pe:AVMediaTypeVideo];
}

- (void)startPreview{
       if(mCaptureSession && mPreview && mStarted){
              AVCaptureVideoPreviewLay er* previewLayer = [AVCaptureVideoPreviewLay er layerWithSession: mCaptureSession];
              previewLayer.frame = mPreview.bounds;
              previewLayer.videoGravity = AVLayerVideoGravityResiz eAspectFill;
//              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: [AVCaptureVideoPreviewLay er class]])