首页 > 代码库 > iOS自定义相机Demo

iOS自定义相机Demo

//  Created by David_Tian on 14-9-24.//  Copyright (c) 2014年 All rights reserved.//  未做适配,有兴趣的童鞋可以扩展#import "cameraViewController.h"#import <AVFoundation/AVFoundation.h>#import <ImageIO/ImageIO.h>#import <QuartzCore/QuartzCore.h>typedef enum{    isFrontCamera = 0,    isBackCamera} cameraType;typedef enum{    AutoFlash = 0,    CloseFlash,    OpenFlash} flashModel;@interface cameraViewController ()< AVCaptureAudioDataOutputSampleBufferDelegate, UIImagePickerControllerDelegate >{    UIImage *_photo;    BOOL _isFontCamera;}@property (strong, nonatomic) AVCaptureSession           *session;       // 捕获会话@property (nonatomic, strong) AVCaptureStillImageOutput  *captureOutput; // 输出设备@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;  // 取景器 @end@implementation cameraViewController- (AVCaptureSession *)session{    if (_session == nil) {        _session = [[AVCaptureSession alloc] init];    }    return _session;}- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor grayColor];
UIButton
*btnCam = [[UIButton alloc]initWithFrame:CGRectMake(20, 44, 80, 30)]; [btnCam setTitle:@"拍照" forState:UIControlStateNormal]; [btnCam setTitle:@"重照" forState:UIControlStateSelected];//重拍没做 [btnCam setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btnCam addTarget:self action:@selector(capture) forControlEvents:UIControlEventTouchUpInside]; [btnCam setBackgroundColor:[UIColor whiteColor]]; [self.view addSubview:btnCam]; UIButton *btnSelectCam = [[UIButton alloc]initWithFrame:CGRectMake(110, 44, 80, 30)]; [btnSelectCam setTitle:@"后置相机" forState:UIControlStateNormal]; [btnSelectCam setTitle:@"前置相机" forState:UIControlStateSelected]; [btnSelectCam setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btnSelectCam addTarget:self action:@selector(switchCamera:) forControlEvents:UIControlEventTouchUpInside]; [btnSelectCam setBackgroundColor:[UIColor whiteColor]]; [self.view addSubview:btnSelectCam]; UIButton *btnFlash = [[UIButton alloc]initWithFrame:CGRectMake(200, 44, 80, 30)]; [btnFlash setTitle:@"闪光灯" forState:UIControlStateNormal]; [btnFlash setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btnFlash addTarget:self action:@selector(switchFlashMode:) forControlEvents:UIControlEventTouchUpInside]; [btnFlash setBackgroundColor:[UIColor whiteColor]]; [self.view addSubview:btnFlash]; [self switchCamera:nil];}
#pragma mark - action// 拍照- (void)capture{ if (![self.session isRunning]) { [self getPhoto]; }else{ [_session stopRunning]; }}// 切换摄像头- (void)switchCamera:(UIButton *)sender{ [self.session stopRunning]; [_previewLayer removeFromSuperlayer]; sender.selected = !sender.selected; _isFontCamera = !_isFontCamera; [self setupCamera];}// 获取图片- (void)getPhoto{ AVCaptureConnection *videoConnection = nil; for (AVCaptureConnection *connection in _captureOutput.connections) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqual:AVMediaTypeVideo] ) { videoConnection = connection; break; } } if (videoConnection) { break; } } [_captureOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, nil); if (exifAttachments) { // Do something with the attachments. } // 获取图片数据 NSData *imageData =http://www.mamicode.com/ [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; UIImage *t_image = [[UIImage alloc] initWithData:imageData]; _photo = [[UIImage alloc]initWithCGImage:t_image.CGImage scale:1.5 orientation:UIImageOrientationRight]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:_previewLayer.frame]; [_previewLayer removeFromSuperlayer]; imageView.image = _photo; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.clipsToBounds = YES; [self.view addSubview:imageView]; }];} // 切换闪光灯模式- (void)switchFlashMode:(UIButton*)sender { Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (!captureDeviceClass) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"您的设备没有拍照功能" delegate:nil cancelButtonTitle:NSLocalizedString(@"Sure", nil) otherButtonTitles: nil]; [alert show]; return; } NSString *imgName = @""; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; if ([device hasFlash]) { if (device.flashMode == AVCaptureFlashModeOff) { device.flashMode = AVCaptureFlashModeOn; imgName = @""; } else if (device.flashMode == AVCaptureFlashModeOn) { device.flashMode = AVCaptureFlashModeAuto; imgName = @""; } else if (device.flashMode == AVCaptureFlashModeAuto) { device.flashMode = AVCaptureFlashModeOff; imgName = @""; } if (sender) { [sender setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal]; } } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"您的设备没有闪光灯功能" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil]; [alert show]; } [device unlockForConfiguration];} // 初始化相机- (void)setupCamera{ AVCaptureDevice *captureDevice = [self getVideoInputCamera:(_isFontCamera ? isFrontCamera : isBackCamera)]; AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil]; if ([self.session canAddInput:deviceInput]){ [_session addInput:deviceInput]; } // 预览视图(取景器) _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; [_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; CALayer *preLayer = [[self view] layer]; [preLayer setMasksToBounds:YES]; [_previewLayer setFrame:CGRectMake(10, 100, 300, 300)]; [preLayer insertSublayer:_previewLayer atIndex:0]; [_session startRunning]; // 创建一个输出设备,并将它添加到会话 _captureOutput = [[AVCaptureStillImageOutput alloc] init]; NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]; _captureOutput.outputSettings = outputSettings; [_session addOutput:_captureOutput];} #pragma mark VideoCapture- (AVCaptureDevice *)getVideoInputCamera:(cameraType )cameraType{ //获取(后置/前置)摄像头设备 NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in cameras){ switch (cameraType) { case isFrontCamera: if (device.position == AVCaptureDevicePositionFront) return device; break; case isBackCamera: if (device.position == AVCaptureDevicePositionBack) return device; break; default: break; } } return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];} @end

 

iOS自定义相机Demo