`
sinaier
  • 浏览: 34940 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

IOS4直接获取摄像头数据(收藏)

阅读更多
需要添加的framework:CoreMedia,CoreVideo,QuartzCore,AVFoundation
MyAVController.h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreVideo/CoreVideo.h>
#import <CoreMedia/CoreMedia.h>

@interface MyAVController : UIViewController <
AVCaptureVideoDataOutputSampleBufferDelegate> {
    AVCaptureSession *_captureSession;
    UIImageView *_imageView;
    CALayer *_customLayer;
    AVCaptureVideoPreviewLayer *_prevLayer;
}

@property (nonatomic, retain) AVCaptureSession *captureSession;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) CALayer *customLayer;
@property (nonatomic, retain) AVCaptureVideoPreviewLayer *prevLayer;
- (void)initCapture;

@end

MyAVController.m:

#import "MyAVController.h"

@implementation MyAVController

@synthesize captureSession = _captureSession;
@synthesize imageView = _imageView;
@synthesize customLayer = _customLayer;
@synthesize prevLayer = _prevLayer;

#pragma mark -
#pragma mark Initialization
- (id)init {
    self = [super init];
    if (self) {
        self.imageView = nil;
        self.prevLayer = nil;
        self.customLayer = nil;
    }
    return self;
}

- (void)viewDidLoad {
    [self initCapture];
}

- (void)initCapture {
    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput
                     deviceInputWithDevice:[AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo]  error:nil];
    AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc]
init];
    captureOutput.alwaysDiscardsLateVideoFrames = YES;
    //captureOutput.minFrameDuration = CMTimeMake(1, 10);

    dispatch_queue_t queue;
    queue = dispatch_queue_create("cameraQueue", NULL);
    [captureOutput setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);
    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
    NSNumber* value = [NSNumber
numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
    NSDictionary* videoSettings = [NSDictionary
dictionaryWithObject:value forKey:key];
    [captureOutput setVideoSettings:videoSettings];
    self.captureSession = [[AVCaptureSession alloc] init];
    [self.captureSession addInput:captureInput];
    [self.captureSession addOutput:captureOutput];
    [self.captureSession startRunning];
    self.customLayer = [CALayer layer];
    self.customLayer.frame = self.view.bounds;
    self.customLayer.transform = CATransform3DRotate(
CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);
    self.customLayer.contentsGravity = kCAGravityResizeAspectFill;
    [self.view.layer addSublayer:self.customLayer];
    self.imageView = [[UIImageView alloc] init];
    self.imageView.frame = CGRectMake(0, 0, 100, 100);
     [self.view addSubview:self.imageView];
    self.prevLayer = [AVCaptureVideoPreviewLayer
layerWithSession: self.captureSession];
    self.prevLayer.frame = CGRectMake(100, 0, 100, 100);
    self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer: self.prevLayer];
}

#pragma mark -
#pragma mark AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress,
width, height, 8, bytesPerRow, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);

    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);

    [self.customLayer performSelectorOnMainThread:@selector(setContents:)
withObject: (id) newImage waitUntilDone:YES];

    UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0
orientation:UIImageOrientationRight];

    CGImageRelease(newImage);

    [self.imageView performSelectorOnMainThread:@selector(setImage:)
withObject:image waitUntilDone:YES];

    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    [pool drain];
}

#pragma mark -
#pragma mark Memory management

- (void)viewDidUnload {
    self.imageView = nil;
    self.customLayer = nil;
    self.prevLayer = nil;
}

- (void)dealloc {
    [self.captureSession release];
    [super dealloc];
}

@end
分享到:
评论

相关推荐

    ios应用源码之获取本地视频库和获取摄像头视频流 videoupload 20181210

    需要获取摄像头设备,通常使用`AVCaptureDevice.default(for: .video)`获取默认的后置摄像头。可以设置设备的预览层(`AVCaptureVideoPreviewLayer`),将其添加到视图上,显示实时预览。 6. **处理视频流**: 对于...

    IOS获取本地视频库和获取摄像头视频流

    在iOS开发中,访问本地视频库和获取摄像头视频流是两个关键功能,它们涉及到多媒体处理、用户隐私权限以及UI设计等多个方面。以下是关于这两个主题的详细讲解。 首先,**获取本地视频库** 是iOS应用中常见的一种...

    iOS 摄像头捕获视频流

    总之,iOS中的摄像头捕获视频流涉及了多个步骤,包括初始化`AVCaptureSession`、设置输入和输出设备、处理帧数据,以及可能的录制操作。开发者可以根据需求对这些步骤进行调整,实现定制化的视频处理功能。无论是...

    ios 用摄像头捕捉的图像为背景

    自iOS8开始,应用程序需要获取用户授权才能访问摄像头。使用` AVAuthorizationStatus`检查用户是否已授权,未授权时通过`AVCaptureDevice.requestAccess(forMediaType:completionHandler:)`请求权限。 综上所述,...

    IOS应用源码——获取本地视频库和获取摄像头视频流 VideoUpload.zip

    3. **获取摄像头视频流**: - `AVCaptureSession`:它是AVFoundation的核心,负责管理设备输入和输出之间的数据流。创建一个`AVCaptureSession`实例,添加`AVCaptureDeviceInput`作为输入(使用`AVCaptureDevice....

    iOS 摄像头和相册

    同时,利用AVCaptureMetadataOutput可以获取摄像头捕捉到的元数据,如二维码或条形码。 iOS摄像头支持多种捕获模式,包括照片、视频、慢动作视频和时间流逝。开发者可以通过AVCapturePhotoOutput类来捕获高质量的...

    采集并基于 rtsp 协议推流摄像头(麦克风)数据,并在 web 浏览器渲染。.zip

    例如,使用OpenCV库在Python中可以方便地实现摄像头数据的抓取。 数据集是收集在一起的一组数据,通常用于训练机器学习模型或进行数据分析。在这个项目中,虽然没有明确提及创建数据集,但推流的音视频数据可能被...

    iOS利用摄像头获取环境光感参数的方法

    iOS 开发中,获取环境光感参数是非常重要的一步骤,特别是在拍摄照片或视频时,光感参数的准确性直接决定了图像的质量。以下是 iOS 利用摄像头获取环境光感参数的方法的详细介绍。 AVCaptureSession 和 ...

    ios开发中读取本地照片和视频以及摄像头调用

    以下将详细介绍如何在iOS应用中读取本地照片和视频,以及如何调用摄像头。 一、读取本地照片 1. **导入Photos Framework**:首先,需要在你的Swift或Objective-C项目中导入Photos Framework。在Swift中,通过在...

    FFmpeg-X264-Encode-for-iOS, 利用FFmpeg x264将iOS摄像头实时视频流编码为h264文件.zip

    本项目“FFmpeg-X264-Encode-for-iOS”专注于在iOS平台上,通过FFmpeg和x264库,将iOS设备的摄像头实时捕获的视频流编码为H.264格式的文件。 在iOS开发中,利用FFmpeg进行视频处理有以下几个关键点: 1. **集成...

    最新版海康摄像头iOS_V5.3.6.30_build20180816_CN.zip

    海康摄像头iOS SDK是专为苹果设备设计的开发工具包,用于与海康品牌的摄像头进行交互和集成。这个最新版的SDK,版本号为V5.3.6.30_build20180816_CN,是在2018年8月16日编译完成的中文版本,它提供了开发者所需的...

    iOS 萤石云摄像头视频demo

    10. **隐私政策与合规性**:由于涉及到摄像头的使用,务必确保符合相关的隐私政策和法规,如获取用户明确的同意,并妥善保护用户的个人信息和视频数据。 以上就是集成萤石云摄像头视频的一些核心知识点,实际开发中...

    cg opengl 捕捉摄像头

    它支持多种编程语言,如C++、Python、Java等,使得开发者能够方便地实现摄像头数据的获取和处理。 学习CG和OpenGL编程时,摄像头捕捉是常见的一种应用,比如创建实时的3D预览或者结合CG技术进行实时视频特效。首先...

    IOS二维码获取代码

    在iOS开发中,二维码(QR Code)的获取与解析是一项常用功能,特别是在移动支付、信息交换、应用下载等领域。本文将深入探讨如何在iOS平台上实现二维码的读取和处理。 首先,我们需要了解iOS中用于处理二维码的核心...

    H5 Video标签调用摄像头进行录像,兼容苹果、安卓系统、可在微信浏览器正常使用

    在这个场景中,我们利用`&lt;video&gt;`标签的特定属性来实现调用用户设备的摄像头进行录像,并且确保这个功能在苹果iOS系统(如iPhone和iPad)和安卓Android系统上的浏览器,以及微信内置的浏览器中都能正常工作。...

    AVFoundation ios摄像头步骤图像

    获取默认后置摄像头的代码如下: ```swift guard let captureDevice = AVCaptureDevice.default(for: .video) else { fatalError("无法找到可用的摄像头") } ``` 现在,我们需要创建一个`AVCaptureDeviceInput`,...

    iOS开发中网络图片相册摄像头的访问

    在iOS开发中,访问用户设备的相册和摄像头是常见的功能需求,这涉及到用户的媒体资源管理,包括照片、视频的获取、展示以及拍摄。本文将深入探讨如何在iOS应用中实现这些功能,主要涵盖以下几个方面: 一、权限管理...

    iPhone摄像头设备获取

    我们将围绕一个具体的示例来展开讲解,包括如何获取摄像头设备、开启和关闭摄像头、以及如何处理摄像头捕获的数据等内容。 #### 二、所需库文件 为了实现摄像头的相关功能,我们需要添加以下库文件到项目中: - `...

    iOS利用手机摄像头测心率

    在iOS平台上,利用手机摄像头测量心率是一种创新的技术,它基于光学传感器原理,即光电容积描记术(PPG)。PPG是一种非侵入性的生理信号测量方法,通过分析血液容积随心脏搏动而产生的变化来获取心率信息。在iOS设备中...

Global site tag (gtag.js) - Google Analytics