zoukankan      html  css  js  c++  java
  • 使用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、AVCaptureAudioDataOutput
    NSDictionary *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);
    }
  • 相关阅读:
    centos磁盘满了,查找大文件并清理
    centos如何查看磁盘剩余空间
    无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确
    HTTP/1.1新建会话失败 解决方法及分析
    PHP 获取 IE浏览器版本号
    基于Android Studio搭建Android应用开发环境
    打印机重启后才能打印
    洛谷P1141 01迷宫【DFS】
    洛谷P1219 八皇后【DFS】
    UVA133
  • 原文地址:https://www.cnblogs.com/fengmin/p/5523220.html
Copyright © 2011-2022 走看看