iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了OpenCv iOS相机预览单一方向,无需拉伸大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在为iOS编写一个OpenCv Cordova插件.我需要全屏显示相机预览并保持所有iOS设备(iPhone,iPad)的宽高比. 我能够实现人像模式(参见代码)并且在iPhone 6 / plus上完美运行但是iPad上的相机预览略有拉伸可能是由于AVCaptureSessionPresetHigh支持的分辨率为1280×720.任何人都有关于如何实现单一方向(仅限横向或纵向)并保持纵横比的
我正在为iOS编写一个OpenCv Cordova插件.我需要全屏显示相机预览并保持所有iOS设备(iPhone,iPad)的宽高比.

我能够实现人像模式(参见代码)并且在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相机预览单一方向,无需拉伸所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。