iPhone摄像头设备获取(分离简化版) (在iOS代码库中浏览本帖)

http://www.cocoachina.com/bbs/read.php?tid=66400

这次把视频捕获的彻底的分出来了。应该每个都看得懂怎么用吧。
贴上代码略说明下
需要的库
AVFoundation.framework 、CoreVideo.framework 、CoreMedia.framework
该摄像头捕抓必须编译真机的版本且要sdk4.0以上,模拟器下编译不了。

[font=]文件说明:
[font=] CameraHelp.h/.m 为主要文件即摄像头捕获
[font=] VideoController.h/.m为调用示例

//
//  CameraHelp.h
//  
//
//  Created by Zhuang Chuan Xian. on 11-6-28.
//  Copyright 2011  . All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.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)VideoDataOutputBuffer:(char*)videoBuffer dataSize:(int)size;
@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


实现的自己下载例子,该例可以在编译运行,函数不懂的看这  iPhone摄像头设备获取


下面是调用说明很简单就句话
//
//  VideoController.m
//  
//
//  Created by zcx. on 11-6-28.
//  Copyright 2011  . All rights reserved.
//


#import "VideoController.h"
#import "CameraHelp.h"


@implementation VideoController
@synthesize videoView;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
// Custom initialization.
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
return self;
}






// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
[self setTitle:@"Video Capture"];
//捕获很简单就下面这几句话
//设置输出的View
[[CameraHelp shareCameraHelp] setPreview:videoView];
//捕获数据输出到本地
[[CameraHelp shareCameraHelp] setVideoDataOutputBuffer:self];
//开始捕获
[[CameraHelp shareCameraHelp] startVideoCapture];
}
//如果要获取捕获的数据记得重写这个接口哦 不然蹦了,不要怪人哦。
-(void)VideoDataOutputBuffer:(char*)videoBuffer dataSize:(int)size
{
NSLog(@"Recv Data size=%d",size);
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}




- (void)dealloc {
//停止捕获
[[CameraHelp shareCameraHelp] stopVideoCapture];
//关闭输出
[[CameraHelp shareCameraHelp] setVideoDataOutputBuffer:nil];
    [super dealloc];
}


- (IBAction) onButtonEndClick: (id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
@end


最后程序关闭时记得调用 
[CameraHelp closeCamera];
不然会内存泄露的
最后 觉得好的话顶下哈。。。。








[ 此帖被chuanxian396在2011-07-13 14:33重新编辑 ]
附件:  TestVideoCapture.zip (27 K) 下载次数:2130
posted @ 2018-07-30 21:26  sundaysios  阅读(159)  评论(0)    收藏  举报