需要添加的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
分享到:
相关推荐
1. 直接调用摄像头拍照 2. 从相册中选择 3. 从图库中选择 UIImagePickerController 是系统提供的用来获取图片和视频的接口; 用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤: 1. 初始化...
利用 AVCaptureDevice获取iOS端的硬件资源并取得相机资源,支持深度信息(AVDepthData)获取、分辨率改变、前后摄像头切换、多方向旋转、摄像头预览改变等多种强大功能。最低支持iOS7的系统 软件开发设计:PHP、QT、...
1. 直接调用摄像头拍照 2. 从相册中选择 3. 从图库中选择 UIImagePickerController 是系统提供的用来获取图片和视频的接口; 用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤: 1. 初始化...
目的:打开、关闭前置摄像头,绘制图像,并获取摄像头的二进制数据。 需要的库 AVFoundation.framework 、CoreVideo.framework 、CoreMedia.framework 、QuartzCore.framework 该摄像头捕抓必须编译真机的版本,...
资源中的打开相机和相册均是原生Java代码实现,在我自己手机和安卓7.0版本上面稳定可靠,测试在魅族5.0版本左右打开相机会死掉,由Qt调用,demo可学习Qt如何与原生Java的交互通信,代码均有注释,里面还有微信等接口...
它使用WebRTC标准读取网络摄像头数据,而用于检测该数据中的QR码。 为了优化速度和体验,网络工作人员被用于在单独的过程中卸载繁重的QR码算法。 Web Worker是内联的,并在创建组件时加载。演示版已知的问题服务器...
使用AVCaptureDevice类获取并设置后置摄像头作为输入设备。 创建一个AVCaptureVideoPreviewLayer对象,并将其添加到视图层级中,以显示相机预览界面。 实时图像处理 为了寻找边缘,你可以使用...
在这个项目中,我们将学习如何将AVFoundation的AVCameraSession与SwiftUI集成在一起,以便为需要使用设备摄像头的应用创建可重用,可扩展的现代组件。 这是教程中开发和使用的软件包。 特征 相机供稿。 点击以...
二维码扫描需要获取摄像头并读取照片信息,因此我们需要导入系统的AVFoundation框架,创建视频会话。我们需要用到一下几个类: AVCaptureSession 会话对象。此类作为硬件设备输入输出信息的桥梁,承担实时获取设备...
QNRTCKit 是七牛云推出的一款适用于 iOS 平台的实时音视频 SDK,提供了包括音视频通话、美颜、滤镜、水印等多种功能,提供灵活的接口,支持高度定制以及二次开发。 2 功能列表 基本的音视频通话功能 支持内置音视频...
无纸化办公的普及以及大中小型企业建设财务共享中心的需求,企业面临大量原始财务数据信息的采集和录入,传统财务发票依靠人工整理、图像采集、录入、审核等耗费大量财务人员精力,并且在庞大的工作量下,工作的准确...
8.1.1 使用urlrequest获取数据 229 8.1.2 监视urlrequest对象 230 8.1.3 创建maintaining data app示例 231 8.2 使用sqlite存储数据 238 8.3 小结 276 8.3.1 练习 276 8.3.2 本章所学内容 277 第9章 使用...
使用 Flutter 做的一个 APP,使用 HTTP 协议获取后端数据,由于目前开发碰到一难点, 不会调用摄像头扫条形码,所以 APP 不能投入使用。我们学校正在用微信端网页来做资产盘点。这个 APP 是同款阉割版 APP 只能当 ...