我能够实现人像模式(参见代码)并且在iPhone 6 / plus上完美运行但是iPad上的相机预览略有拉伸可能是由于AVCaptureSessionPresetHigh支持的分辨率为1280×720.任何人都有关于如何实现单一方向(仅限横向或纵向)并保持纵横比的任何想法?
我目前启动相机的代码是
- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; // Do any additional setup after loading the vIEw. // Initialize imageVIEw to fullscreen imageVIEw = [[UIImageVIEw alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.vIEw addSubvIEw:imageVIEw]; [imageVIEw setContentMode:UIVIEwContentModeScaleAspectFill]; [imageVIEw setClipsToBounds:YES]; self.vIDeoCamera = [[FixedCvVIDeoCamera alloc] initWithParentVIEw:imageVIEw]; self.vIDeoCamera.delegate = self; self.vIDeoCamera.defaultAVCaptureDeviceposition = AVCaptureDevicepositionBack; self.vIDeoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetHigh; self.vIDeoCamera.defaultAVCaptureVIDeoOrIEntation = AVCaptureVIDeoOrIEntationPortrait; self.vIDeoCamera.defaultFPS = 30; [self.vIDeoCamera start];}
请注意,我使用的是FixedCvVIDeoCamera而不是OpenCv CvVIDeoCamera类.原因是我正在子类化并覆盖CvVIDeoCamera的layoutPrevIEwLayer函数以保持纵向模式.
- (voID)layoutPrevIEwLayer;{ if (self.parentVIEw != nil) { CALayer* layer = self->customPrevIEwLayer; CGRect bounds = self->customPrevIEwLayer.bounds; int rotation_angle = 0; switch (defaultAVCaptureVIDeoOrIEntation) { case AVCaptureVIDeoOrIEntationLandscapeRight: rotation_angle = 270; break; case AVCaptureVIDeoOrIEntationPortraitUpsIDeDown: rotation_angle = 180; break; case AVCaptureVIDeoOrIEntationLandscapeleft: rotation_angle = 90; break; case AVCaptureVIDeoOrIEntationPortrait: default: break; } layer.position = CGPointMake(self.parentVIEw.frame.size.wIDth/2.,self.parentVIEw.frame.size.height/2.); layer.affinetransform = CGAffinetransformMakeRotation( degrees_radians(rotation_angle) ); layer.bounds = bounds; }}
提前致谢.
解决方法 我自己解决了.下面的代码将帮助那些只想要一个固定的肖像模式并支持前后摄像头的人.视图控制器
- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; // Do any additional setup after loading the vIEw. // Initialize imageVIEw to fullscreen imageVIEw = [[UIImageVIEw alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.vIEw addSubvIEw:imageVIEw]; [imageVIEw setContentMode:UIVIEwContentModeScaleAspectFill]; [imageVIEw setClipsToBounds:YES]; self.vIDeoCamera = [[FixedCvVIDeoCamera alloc] initWithParentVIEw:imageVIEw]; self.vIDeoCamera.delegate = self; self.vIDeoCamera.defaultAVCaptureDeviceposition = AVCaptureDevicepositionBack; self.vIDeoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetHigh; self.vIDeoCamera.defaultAVCaptureVIDeoOrIEntation = AVCaptureVIDeoOrIEntationPortrait; self.vIDeoCamera.defaultFPS = 30; self.vIDeoCamera.grayscaleMode = NO; [self.vIDeoCamera start];}
FixedCvVIDeoCamera:CvVIDeoCamera
- (voID)layoutPrevIEwLayer;{ if (self.parentVIEw != nil) { CALayer* layer = self->customPrevIEwLayer; CGRect bounds = self->customPrevIEwLayer.bounds; NSLog(@"[FixedCvVIDeoCamera]Custom PrevIEw Layer bounds %fx%f",bounds.size.wIDth,bounds.size.height); float prevIEwaspectRatio = bounds.size.height / bounds.size.wIDth; NSLog(@"[FixedCvVIDeoCamera]PrevIEw aspect ratio %f",prevIEwaspectRatio); //int rotation_angle = 0; layer.position = CGPointMake(self.parentVIEw.frame.size.wIDth/2.,self.parentVIEw.frame.size.height/2.); //layer.affinetransform = CGAffinetransformMakeRotation( degrees_radians(rotation_angle) ); // Get vIDeo Feed's resolutions NSArray* devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVIDeo]; AVCaptureDevice* device = nil; for (AVCaptureDevice *d in devices) { // Get the default camera device - should be either front of back camera device if ([d position] == self.defaultAVCaptureDeviceposition) { device = d; } } // Set the default device if not found if (!device) { device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVIDeo]; } CMVIDeoDimensions dimensions = CMVIDeoFormatDescriptionGetDimensions(device.activeFormat.formatDescription); CGSize resolution = CGSizeMake(dimensions.wIDth,dimensions.height); if (self.defaultAVCaptureVIDeoOrIEntation == AVCaptureVIDeoOrIEntationPortrait || self.defaultAVCaptureVIDeoOrIEntation == AVCaptureVIDeoOrIEntationPortraitUpsIDeDown) { resolution = CGSizeMake(resolution.height,resolution.wIDth); } NSLog(@"[FixedCvVIDeoCamera]VIDeo Feed resolution is %fx%f",resolution.wIDth,resolution.height); float vIDeoFeedAspectRatio = resolution.height / resolution.wIDth; NSLog(@"[FixedCvVIDeoCamera]VIDeo Feed's aspect ratio is %f",vIDeoFeedAspectRatio); // Set layer bounds to ASPECT FILL by expanding either the wIDth or the height if (prevIEwaspectRatio > vIDeoFeedAspectRatio) { NSLog(@"[FixedCvVIDeoCamera] PrevIEw is more rectangular than the vIDeo Feed aspect ratio. Expanding wIDth to maintain aspect ratio."); float newWIDth = bounds.size.height / vIDeoFeedAspectRatio; layer.bounds = CGRectMake(0,newWIDth,bounds.size.height); } else { NSLog(@"[FixedCvVIDeoCamera] PrevIEw is equally or less rectangular (wIDer) than the vIDeo Feed's aspect ratio. Expanding height bound to maintain aspect ratio."); float newHeight = bounds.size.wIDth * vIDeoFeedAspectRatio; layer.bounds = CGRectMake(0,newHeight); } }}总结
以上是内存溢出为你收集整理的OpenCv iOS相机预览单一方向,无需拉伸全部内容,希望文章能够帮你解决OpenCv iOS相机预览单一方向,无需拉伸所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)