【IOS开发】实时显示摄像头内容

转自互联网:http://blog.csdn.net/dean19900504/article/details/8101522,有做修改。


ViewController:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController<AVCaptureVideoDataOutputSampleBufferDelegate>


@end


#import "ViewController.h"
#import "EnvConstant.h"

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

@interface ViewController ()

@end

@implementation ViewController{
    AVCaptureSession *_captureSession;
    UIImageView *_outputImageView;
    AVCaptureVideoPreviewLayer *_captureLayer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    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];
    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
    [captureOutput setVideoSettings:videoSettings];
    _captureSession = [[AVCaptureSession alloc] init];
    [_captureSession addInput:captureInput];
    [_captureSession addOutput:captureOutput];
    [_captureSession startRunning];
    
    //实时显示摄像头内容
    _captureLayer = [AVCaptureVideoPreviewLayer layerWithSession: _captureSession];
    _captureLayer.frame = CGRectMake(10, START_POSITION+10, WIDTH-20, 200);
    _captureLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer: _captureLayer];
    
    //显示输出图片
    _outputImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_captureLayer.frame)+10, CGRectGetWidth(_captureLayer.frame), CGRectGetHeight(_captureLayer.frame))];
    [self.view addSubview:_outputImageView];
    
    
    UIButton *stopButton = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_outputImageView.frame)+30, CGRectGetWidth(_outputImageView.frame)/2-10, 50)];
    stopButton.backgroundColor = [UIColor blackColor];
    [stopButton setTitle:@"stop" forState:UIControlStateNormal];
    [stopButton setTitle:@"stop" forState:UIControlStateHighlighted];
    [stopButton addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopButton];
    
    UIButton *startButton = [[UIButton alloc]initWithFrame:CGRectMake(10+CGRectGetWidth(_outputImageView.frame)/2, CGRectGetMaxY(_outputImageView.frame)+30, CGRectGetWidth(_outputImageView.frame)/2-10, 50)];
    startButton.backgroundColor = [UIColor blackColor];
    [startButton setTitle:@"start" forState:UIControlStateNormal];
    [startButton setTitle:@"start" forState:UIControlStateHighlighted];
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startButton];
}

#pragma mark AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
    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);
    
    UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
    CGImageRelease(newImage);
    [_outputImageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}

-(void)stop{
    [_captureSession stopRunning];
}
-(void)start{
    [_captureSession startRunning];
}

@end

-(UIImage *)imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer {
    CVImageBufferRef imageBuffer =  CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
    CIContext *temporaryContext = [CIContext contextWithOptions:nil];
    CGImageRef videoImage = [temporaryContext
                             createCGImage:ciImage
                             fromRect:CGRectMake(0, 0,
                                                 CVPixelBufferGetWidth(imageBuffer),
                                                 CVPixelBufferGetHeight(imageBuffer))];
    
    UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
    CGImageRelease(videoImage);
    
    return image;
}



EnvConstant.h:

/**
 *  常量类
 */
#define IS_IOS_7 ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0 ? YES : NO)
#define IS_IOS_8 ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0 ? YES : NO)
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define START_POSITION ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0 ? 20 : 0)


需要引入 CoreMedia, CoreVideo QuartzCore, AVFoundation 四个framework






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 桌球悬浮窗辅助线是一款为2023年iOS设备开发的桌球辅助工具。虽然没有具体的产品信息,但我们可以假设它是一种通过虚拟现实或增强现实技术来提供实时辅助线的应用程序。 这款桌球悬浮窗辅助线应用程序可能会利用设备的摄像头和计算机视觉算法,实时跟踪桌球运动,根据球的位置和角度生成辅助线。这样,使用此应用的玩家可以更准确地计算击球力度和方向,提高球技水平。 桌球悬浮窗辅助线应用程序可能会具备一些实用的功能,如可根据用户的技术水平设定不同的辅助程度,从完全无辅助到辅助线的显示频率和精度不同,以满足不同玩家的需求。 此外,该应用程序可能还提供一些额外的功能,如实时分析和跟踪球技进步情况,记录历史数据以及与其他玩家进行对战或挑战等。 总之,桌球悬浮窗辅助线2023 iOS是一款可能有助于提高玩家桌球水平的应用程序。它利用先进的技术为玩家提供实时的辅助线,提供了一种更精准和准确的击球方式。 ### 回答2: 桌球悬浮窗辅助线是一种在桌球游戏中使用的辅助工具。在2023年的iOS操作系统中,该功能设计得更加智能化和先进。 桌球是一项技巧和策略相结合的运动,需要运动员在有限的时间内将球击进袋中,并在此过程中避开其他球员的干扰。桌球悬浮窗辅助线旨在帮助玩家更好地掌握击球角度和力度,提供更精确的击球指导。 通过2023年iOS系统中的桌球悬浮窗辅助线功能,玩家可以轻松地预测击球后球的运动轨迹。悬浮窗将根据球的位置、玩家调整的击球力度和角度等参数提供实时的辅助线,并显示球经过该线时可能的路径。 桌球悬浮窗辅助线不仅可以帮助新手玩家快速入门,并提供一种学习的机会,还能帮助经验丰富的选手提高自己的技术水平。同时,该功能的智能设计确保了它不会过度干扰游戏过程,而是更像是一个有益的参考工具。 随着技术的进步,游戏辅助工具的功能也在不断优化和升级。桌球悬浮窗辅助线作为一项创新功能,会为桌球爱好者带来更好的游戏体验和更高的挑战。无论是初学者还是专业选手,都可以通过这一功能更好地享受桌球运动的魅力。 ### 回答3: 桌球悬浮窗辅助线是一种辅助桌球游戏的功能,在2023年的iOS操作系统上使用。 桌球游戏是一种需要精准控制力度和角度的运动,以将球击入目标洞口。然而,对于初学者来说,掌握正确的击球力度和角度往往是一项挑战。这就是桌球悬浮窗辅助线的用途。 悬浮窗辅助线是在游戏画面显示的一条虚拟直线,根据玩家的球杆位置和目标洞口的位置计算得出。它可以帮助玩家准确判断击球的力度和角度,提供一个参考线来指导击球。 当玩家准备击球时,悬浮窗辅助线会自动显示在游戏画面上,显示从球杆到目标洞口的直线。玩家可以根据这条线调整击球力度和角度,以尽量准确地将球击入目标洞口。同时,悬浮窗辅助线还会根据球杆的移动和角度变化实时更新,帮助玩家更好地掌握击球技巧。 使用桌球悬浮窗辅助线有助于提高初学者的击球准确性和技巧,缩短学习周期。它可以帮助玩家更快地了解和掌握击球的力度和角度,提升游戏体验,并激发更大的游戏兴趣。 总之,桌球悬浮窗辅助线2023ios是一项能在桌球游戏中提供帮助的功能,它可以帮助初学者提高击球准确性和技巧,提升游戏体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值