视频捕捉、获取静态图片(自定义相机)
其實(shí)這相當(dāng)于是一個(gè)自定義相機(jī)的功能了
?
github下載鏈接:https://github.com/SSYSSK/camera2
?
1、自定義相機(jī)
2、視頻的捕獲預(yù)覽
3、視頻的錄制
4、拍照
5、解決了拍照照片翻轉(zhuǎn)90度的問(wèn)題
6、解決了前置攝像頭照片顛倒的問(wèn)題
?
關(guān)鍵點(diǎn):
1、自從iOS10之后,獲取圖片的輸出由AVCaptureStillImageOutput變成了AVCapturePhotoOutput,所以需要做兩套適配,輸出配置代碼:
//6、靜態(tài)圖片輸出if (@available(iOS 10.0, *)) {self.imageOutput = [[AVCapturePhotoOutput alloc]init];//輸出連接 判斷是否可用,可用則添加到輸出連接中去if ([self.captureSession canAddOutput:self.imageOutput]){[self.captureSession addOutput:self.imageOutput];}}else {//AVCaptureStillImageOutput 實(shí)例 從攝像頭捕捉靜態(tài)圖片self.stillImageOutput = [[AVCaptureStillImageOutput alloc]init];//配置字典:希望捕捉到JPEG格式的圖片self.stillImageOutput.outputSettings = @{AVVideoCodecKey:AVVideoCodecTypeJPEG};if ([self.captureSession canAddOutput:self.stillImageOutput]){[self.captureSession addOutput:self.stillImageOutput];}}?
?
2、拍照功能,要區(qū)分iOS10和iOS10之前:
?[output capturePhotoWithSettings:settings delegate:self];
// 從流中獲取照片 -(void)getPhoto {// 拍照if (@available(iOS 10.0, *)) {AVCapturePhotoOutput * output = (AVCapturePhotoOutput *)self.imageOutput;AVCapturePhotoSettings * settings = [AVCapturePhotoSettings photoSettings];// 這一句代碼才是執(zhí)行拍照[output capturePhotoWithSettings:settings delegate:self];}else{AVCaptureStillImageOutput * stillImageOutput = (AVCaptureStillImageOutput *)self.stillImageOutput;//從 AVCaptureStillImageOutput 中取得 AVCaptureConnectionAVCaptureConnection *connection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo];[stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef _Nullable imageDataSampleBuffer, NSError * _Nullable error) {if (imageDataSampleBuffer != nil) {NSData * data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];UIImage *image = [[UIImage alloc]initWithData:data];//重點(diǎn):捕捉圖片成功后,將圖片傳遞出去UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);}}];}}?
3、拍照完成的代理回調(diào)(iOS10之后才會(huì)走這里),這里要注意一個(gè)照片翻轉(zhuǎn)的問(wèn)題:
? ? ? ? ? ? //前置攝像頭拍照會(huì)旋轉(zhuǎn)180解決辦法
? ? ? ? ? ? if (self.activeVideoInput.device.position == AVCaptureDevicePositionFront) {
? ? ? ? ? ? ? ? UIImageOrientation imgOrientation = UIImageOrientationLeftMirrored;
? ? ? ? ? ? ? ? image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? UIImageOrientation imgOrientation = UIImageOrientationRight;
? ? ? ? ? ? ? ? image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
? ? ? ? ? ? }
?
總結(jié)
以上是生活随笔為你收集整理的视频捕捉、获取静态图片(自定义相机)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: html5实现单点登录,图文并茂,为你揭
- 下一篇: 宋浩概率论与数理统计笔记(一)