使用AVCaptureSession捕捉静态图片

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) AVCaptureSession *captureSession;
@property (strong, nonatomic) AVCaptureDeviceInput *videoInput;
@property (strong, nonatomic) AVCaptureStillImageOutput *stillImageOutput;

- (IBAction)capture:(id)sender;

@end
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.captureSession = [[AVCaptureSession alloc] init];
    //Optional: self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError * error = nil;
    self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (self.videoInput)
    {
        [self.captureSession addInput:self.videoInput];
    }
    else
    {
        NSLog(@"Input Error: %@", error);
    }

    self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];//AVCaptureMovieFileOutput、AVCaptureVideoDataOutput、AVCaptureAudioFileOutput、AVCaptureAudioDataOutputNSDictionary *stillImageOutputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; [self.stillImageOutput setOutputSettings:stillImageOutputSettings];[self.captureSession addOutput:self.stillImageOutput]; AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]; UIView *aView = self.view; previewLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-70);[aView.layer addSublayer:previewLayer]; }
- (IBAction)capture:(id)sender
 {
        AVCaptureConnection *stillImageConnection = [self.stillImageOutput.connections objectAtIndex:0];
    if ([stillImageConnection isVideoOrientationSupported])
        [stillImageConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection
                                                         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
     {
         if (imageDataSampleBuffer != NULL)
         {
             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
             ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
             UIImage *image = [[UIImage alloc] initWithData:imageData];
             [library writeImageToSavedPhotosAlbum:[image CGImage]
                                       orientation:(ALAssetOrientation)[image imageOrientation]
                                   completionBlock:^(NSURL *assetURL, NSError *error)
                {
                    UIAlertView *alert;
                    if (!error)
                    {
                        alert = [[UIAlertView alloc] initWithTitle:@"Photo Saved"
                                                           message:@"The photo was successfully saved to your photos library"
                                                          delegate:nil
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil, nil];
                    }
                    else
                    {
                        alert = [[UIAlertView alloc] initWithTitle:@"Error Saving Photo"
                                                           message:@"The photo was not saved to your photos library"
                                                          delegate:nil
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil, nil];
                    }

                    [alert show];
                }
              ];
         }
         else
             NSLog(@"Error capturing still image: %@", error);
     }];

 }
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.captureSession startRunning];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.captureSession stopRunning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
时间: 2024-08-11 01:00:38

使用AVCaptureSession捕捉静态图片的相关文章

访问日志不记录静态文件;访问日志切割;静态图片等元素过期时间

扩展 apache日志记录代理IP以及真实客户端IP  http://www.lishiming.net/thread-960-1-1.html apache只记录指定URI的日志  http://www.lishiming.net/thread-981-1-1.html apache日志记录客户端请求的域名  http://www.lishiming.net/thread-1037-1-1.html apache 日志切割问题  http://www.lishiming.net/thread-

换静态图片---轻开电子商务系统(企业入门级B2C网站)

一共3个文件: 显示及上传文件:site/links/img_one.html 保存图片文件:site/links/img_one_up1.chtml 导航菜单文件:site/links/menu.html 在轻开电子商务系统(企业入门级B2C网站)的site/links/目录下 显示图片信息且上传表单:site/links/img_one.html 显示标题(从menu.html文件传过来的标题) <tr><th colspan=2>.<font class=listTit

基于FPGA的VGA显示静态图片

终于熬到暑假了,记过三四周的突击带考试,终于为我的大二画上了一个完整的句号,接下来终于可以静心去做自己想做的事情了,前一阵子报了一个线上培训班,学学Sobel边缘检测,之前一直在学习图像处理,但是因为一直看人家的代码,到后来难免有点空虚.所以说自己狠下心来,报了一个线上培训班,重新学习一下,自己设计Sobel边缘检测,势要摆脱抄别人代码的魔咒.所以这次图像显示部分和在彩色条纹中显示一副图片的代码,全部是我自己设计的,虽然不是什么大工程,但是还是满满的成就感,这次用的时间比较久,因为使用的是新板子

Android 加载gif图片强大框架(支持预加载、缓存,还支持显示静态图片,一行代码全搞定)

之前项目中没有涉及到显示gif图片的功能,也没有着重研究过,最近项目中要用到显示gif图片,于是就在网上一顿搜,用过之后发现如下几个缺点. 1.加载大的gif图片会出现oom. 2.没有预加载和缓存功能,比较消耗内存. 3.功能比较单一,显示gif了但是不能显示静态图片. 最后无意间发现了一个强大的gif加载框架,能够满足上面的所有缺陷,支持预加载和缓存,同时支持显示静态图片和gif图片.下面是一些源代码: //xml布局就是一个ImageView //在需要的地方直接调用 Glide.with

Cookie-free Domains(为什么将静态图片,js,css存放到单独的域名?)

有不少童鞋对这个无关性域名cookie不理解,这个其实是在我的前端性能测试视频中提到的,地址:http://edu.51cto.com/course/course_id-563.html感兴趣的童鞋可以看看. 这里我统一解释下,具体如下(部分引用网络内容): 对静态文件(如图片,js 脚本文件及css文件)启用一个新的独立域名,可以减少不必要的cookie传送,提高请求速度并减少带宽. 我们知道,当向http服务器请求某个url时,浏览器将该url与客户主机中存储的所有cookie比较,如果发现

Android静态图片人脸识别的完整demo(附完整源码)

Demo功能:利用android自带的人脸识别进行识别,标记出眼睛和人脸位置.点击按键后进行人脸识别,完毕后显示到imageview上. 第一部分:布局文件activity_main.xml [html] view plaincopyprint? <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.co

Servlet拦截静态图片的解决方案

一.现象 建立一个使用Freemarker的Web Project程序. Product.ftl中的代码为: [html] view plain copy <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv=&quo

vue-cil和webpack中本地静态图片的路径问题解决方案

1 本地图片动态绑定img的src属性 一般我们在html中或者vue组件文件中引用图片是这样,这是不需要做特别处理的 我们将图片放入assets中或者重新建立个文件夹img什么的都可以,随意- 但是我们这时候可能会有个需求就是我们要动态绑定一组本地图片 1 将图片当成模块先引进来,再绑定 但是这种做法局限性比较大,模块化差,代码不好看 . 如果我做的是纯静态的网站展示,有许多的本地静态资源需要加载,里面包括本地数据和本地图片混合的好几组,那不是要麻烦死- 1.1 所以我们需要三步 第一步我们在

一、CentOS7下搭建FastDFS+Nginx实现静态图片服务器

在集群环境下,图片存放在本地存在诸多限制,一般采用单独的图片服务器进行管理.FastDFS就是这样一个图片管理服务器. 环境需求,CentOS7下 一.先下载三件套,并上传到服务器中(Nginx自行下载),地址如下 1:libfastcommon https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz 2:fastdfs https://github.com/happyfish100/fastdfs/archive/