最近碰到需求,要拍照时候没有声音,用AVCaptureStillImageOutput拍照会有快门声,查了网上的使用了反向声音的方式,但是因为要连拍会导致有些时候声音不同步,还是会有漏网的快门声,最后研究了一下使用了AVCaptureVideoDataOutput来对图像进行输出,没有了快门声音,代码如下:

//初始化
- (void)initAVCaptureSession{AVCaptureSession *session = [[AVCaptureSession alloc] init];NSError *error;AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];//更改这个设置的时候必须先锁定设备,修改完后再解锁,否则崩溃[device lockForConfiguration:nil];//设置闪光灯为自动if ([device isFlashModeSupported:AVCaptureFlashModeOff]) {[device setFlashMode:AVCaptureFlashModeOff];};[device unlockForConfiguration];self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];if (error) {NSLog(@"%@",error);}// Create a VideoDataOutput and add it to the sessionself.imageOutput = [[AVCaptureVideoDataOutput alloc] init];[self.session addOutput:self.imageOutput];// Configure your output.dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);[self.imageOutput setSampleBufferDelegate:self queue:queue];// Specify the pixel formatself.imageOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]forKey:(id)kCVPixelBufferPixelFormatTypeKey];// Start the session running to start the flow of data[session startRunning];// Assign session to an ivar.[self setSession:session];if ([self.session canAddInput:self.videoInput]) {[self.session addInput:self.videoInput];}if ([self.session canAddOutput:self.imageOutput]) {[self.session addOutput:self.imageOutput];}AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionFront;for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {if ([d position] == desiredPosition) {[self.previewLayer.session beginConfiguration];AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];for (AVCaptureInput *oldInput in self.previewLayer.session.inputs) {[[self.previewLayer session] removeInput:oldInput];}[self.previewLayer.session addInput:input];[self.previewLayer.session commitConfiguration];break;}}}
//避免前置摄像头反转
- (AVCaptureVideoOrientation)avOrientationForDeviceOrientation:(UIDeviceOrientation)deviceOrientation
{AVCaptureVideoOrientation result = (AVCaptureVideoOrientation)deviceOrientation;if ( deviceOrientation == UIDeviceOrientationLandscapeLeft )result = AVCaptureVideoOrientationLandscapeRight;else if ( deviceOrientation == UIDeviceOrientationLandscapeRight )result = AVCaptureVideoOrientationLandscapeLeft;return result;
}
//使用前置摄像头
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//照片获取,以及其他处理
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBufferfromConnection:(AVCaptureConnection *)connection
{if(sampleBuffer == NULL){return ;}// Create a UIImage from the sample buffer dataUIImage *oldImage = [self imageFromSampleBuffer:sampleBuffer];NSData *newData = UIImageJPEGRepresentation(oldImage, 0.3);[dataArray addObject:newData];NSString *imageName = [NSString stringWithFormat:@"%@.jpg",[self getCurrentTimeInterval]];        //保存图片本地NSString *savedImagePath = [LOCATION_IMAGES_PATH stringByAppendingPathComponent:imageName];//        NSLog(@"%@",savedImagePath);[newData writeToFile:savedImagePath atomically:NO];
}
// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{// Get a CMSampleBuffer's Core Video image buffer for the media dataCVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);// Lock the base address of the pixel bufferCVPixelBufferLockBaseAddress(imageBuffer, 0);// Get the number of bytes per row for the pixel buffervoid *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);// Get the number of bytes per row for the pixel buffersize_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);// Get the pixel buffer width and heightsize_t width = CVPixelBufferGetWidth(imageBuffer);size_t height = CVPixelBufferGetHeight(imageBuffer);// Create a device-dependent RGB color spaceCGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();// Create a bitmap graphics context with the sample buffer dataCGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);// Create a Quartz image from the pixel data in the bitmap graphics contextCGImageRef quartzImage = CGBitmapContextCreateImage(context);// Unlock the pixel bufferCVPixelBufferUnlockBaseAddress(imageBuffer,0);// Free up the context and color spaceCGContextRelease(context);CGColorSpaceRelease(colorSpace);// Create an image object from the Quartz imageUIImage *image = [UIImage imageWithCGImage:quartzImage];// Release the Quartz imageCGImageRelease(quartzImage);return (image);
}

ios使用AVCaptureVideoDataOutput实现连续拍照消除快门声音相关推荐

  1. Android 10.0 Camera2 静音时拍照去掉快门声音

    1.概述 在10.0的系统产品开发中,对于Camera2相机的产品定制化中,发现在Camera2中发现一个问题 当媒体音量静音时,点击拍照还是有拍照声音,产品对这个不满意,所以要修改这个问题,所以针对 ...

  2. Android 9.0 Camera2 静音时拍照去掉快门声音

    1.概述 在9.0的系统产品rom定制化开发中,在原生的camera2的相关功能中,在静音拍照的情况下会听到快门的声音,这个是系统原生camera2的bug,但是在产品定制化的 过程中,显得产品体验不 ...

  3. Android 11.0 12.0Camera2 静音时拍照去掉快门声音

    目录 1.概述 2.Camera2静音拍照去掉快门声音核心代码 3.Camera2静音拍照去掉快门声音核心代码功能分析

  4. Android 12.0Camera2 静音时拍照去掉快门声音

    1.概述 在12.0定制化开发时,在Camera2静音情况下有快门拍照声音,这就不符合使用规范了 静音的情况下拍照也不应该发出声音,所以在静音拍照流程中要求去掉快门声音 2.Camera2静音拍照去掉 ...

  5. iOS - 调用相机连续拍照

    跟朋友讨论如题中的功能怎么实现,后来用系统的方法实现了这一功能,说起来有点取巧,就是在确定拍照后的图片后重新进入相机界面,前面我发过一篇Object-C的和Swift的上传头像,以这个为基础实现,具体 ...

  6. 腾讯优测-优社区干货精选 |安卓适配之Camera拍照时快门咔嚓声

    文/腾讯优测研发工程师 吴宇焕 优测小优有话说: android机型适配的坑自然是不少,不想掉坑快来优测优社区~ 现在Android手机一般都会带有照相功能,有很多朋友就发现手机照相时快门声音很响,想 ...

  7. 【Android适配问题集锦-相机类】Camera拍照时快门咔嚓声适配问题

    现在Android手机一般都会带有照相功能,有很多朋友就发现手机照相时快门声音很响,想关又关不掉.....不过话说回来,静音拍照你想做啥? 来到主题上,因为网络上有很多关于"删除或修改快门声 ...

  8. 腾讯优测-优社区干货精选 |  那些年,我们在Android机型适配上遇到的坑之Camera拍照时快门咔嚓声...

    文/腾讯优测研发工程师 吴宇焕 优测小优有话说: android机型适配的坑自然是不少,不想掉坑快来优测优社区~ 现在Android手机一般都会带有照相功能,有很多朋友就发现手机照相时快门声音很响,想 ...

  9. Android机型适配问题——Camera拍照时快门咔嚓声

    文/腾讯优测 吴宇焕 现在Android手机一般都会带有照相功能,有很多朋友就发现手机照相时快门声音很响,想关又关不掉.....不过话说回来,静音拍照你想做啥? 来到主题上,因为网络上有很多关于&qu ...

最新文章

  1. Linux下利用phpize安装php扩展
  2. Qt的Android应用消除启动黑屏
  3. TCGA免疫浸润评价数据库,TIMER 2.0 使用指南
  4. c语言函数的程序设计,C语言程序设计第3版,第6章函数程序设计.ppt
  5. 怎么让php文件请求一次连接,php 如何锁定接口,让一个接口,同一时间只处理同一人的一次请求?...
  6. acs for PEAP-MSCHAPV2
  7. JSK-24 函数规律【基础】
  8. 维特比算法(Viterbi Algorithm)
  9. implements Serializable有什么作用
  10. 兰州市智能交通实现智慧城市 智能化立体车库有效缓解停车难
  11. 计算机组成原理(LogisimVerilog部分)-笔记
  12. OK插件安装常见问题集锦(PowerPoint版)|OneKeyTools Lite安装说明
  13. 拿来就能用的前端酷炫登录注册模板
  14. 世界史上的6大古帝国
  15. mzy,struts学习(二):struts.xml的配置
  16. html标签的记忆巧法,小学记忆单词的方法
  17. 一些过时的免杀技巧 仅供参考
  18. 爱奇艺电视果连不到WiFi的解决办法
  19. 查询rssi指令_h3c无线控制器常用命令(wx)
  20. Js 根据经纬度坐标计算方位角

热门文章

  1. python绘制雪花图形
  2. Vue中的跨域解决方案
  3. jenkins切换git分支未生效
  4. (转)利用MTCNN和facenet实现人脸检测和人脸识别
  5. Android中常用设计模式
  6. nginx 反向代理 某个目录下 带特定后缀名的文件
  7. yourshelf是什么意思中文_be yourself是什么意思
  8. 三星Note系列刷入Kali Nethunter
  9. 用Python语言实现共享单车的数据分析
  10. win10下安装anaconda + NVIDIA显卡驱动 + pytorch_gpu