最近碰到需求,要拍照时候没有声音,用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. Java并发编程71道面试题及答案
  2. 关于NameError: name ‘sns‘ is not defined->ModuleNotFoundError: No module named ‘seaborn‘错误提示
  3. JZOJ 1321. 灯
  4. Java之HashMap、Hashtable、LinkedHashMap、TreeMap、ConcurrentHashMap简单的区别
  5. Android与服务器通信之socket通信
  6. linux django 指定端口,linux部署django项目流程(全)
  7. Oracle监控的关键指标有哪些
  8. BigDecimal.divide方法
  9. c++ mfc程序 屏幕只显示部分内容_Visual C++ MFC 简明教程
  10. 人人商城小程序服务器根目录,人人商城小程序前端怎么安装?人人商城小程序如何配置实用...
  11. vc++6.0常用插件和界面库
  12. 关于visio 2007导入独立图库
  13. 高仿微信表情控件 -- LQREmojiLibrary
  14. 计算机中英汉字段如何切换,电脑上中英文切换按哪个键
  15. wps教育版支持latex公式啦
  16. C# webBrowser打开网页出现脚本错误解决
  17. 企业生产中,APS系统有哪些具体应用场景?
  18. vscode 源代码管理窗口显示空白
  19. python运行启动报错解决方法_pyspider启动错误解决(Python 3.7)
  20. 怎样运用EDIUS中的色彩平衡滤镜较色

热门文章

  1. Wireshark网络封包分析
  2. 如何批量新建txt并命名?
  3. 不会你还不懂怎么用Python制图吧?师兄教你如何学会绘制漂亮的阶梯图
  4. DIV+CSS实现生气的猫咪老师
  5. 我爱天文 - 你有星空摄影的知识吗?
  6. GY-906-DCI红外测温传感器
  7. SpringSecurity权限管理框架系列(六)-Spring Security框架自定义配置类详解(二)之authorizeRequests配置详解
  8. 使用Markdown修改图片大小
  9. 深度学习笔记其六:现代卷积神经网络和PYTORCH
  10. SpringBoot项目电脑商城项目实战(适合刚学完SpringBoot的初学者)