QCAR配置视频背景的大小

 
相机长宽比 = 相机宽 / 相机长;
屏幕长宽比 = 屏幕长 / 屏幕宽;

竖屏
if(相机长宽比 < 屏幕长宽比)
配置宽 = 相机长 *(屏幕长 / 相机宽);
配置长 = 屏幕长;
else
配置宽 = 屏幕宽;
配置长 = 相机宽 *(屏幕宽 / 相机长);

横屏
if(相机长宽比 < 屏幕长宽比)
配置宽 = 屏幕宽;
配置长 = 相机长 *(屏幕宽 / 相机宽);
else
配置宽 = 相机宽 *(屏幕长 / 相机长);
配置长 = 屏幕长;


@implementation SampleApplicationSession

// Configure QCAR with the video background size

- (void)configureVideoBackgroundWithViewWidth:(float)viewWidth andHeight:(float)viewHeight

{

    // Get the default video mode

    QCAR::CameraDevice& cameraDevice =QCAR::CameraDevice::getInstance();

    QCAR::VideoMode videoMode = cameraDevice.getVideoMode(QCAR::CameraDevice::MODE_DEFAULT);

    

    // Configure the video background

    QCAR::VideoBackgroundConfig config;

    config.mEnabled =true;

    config.mSynchronous =true;

    config.mPosition.data[0] =0.0f;

    config.mPosition.data[1] =0.0f;

    

    // Determine the orientation of the view.  Note, this simple test assumes

    // that a view is portrait if its height is greater than its width.  This is

    // not always true: it is perfectly reasonable for a view with portrait

    // orientation to be wider than it is high.  The test is suitable for the

    // dimensions used in this sample

    if (self.mIsActivityInPortraitMode) {

        // --- View is portrait ---

        

        // Compare aspect ratios of video and screen.  If they are different we

        // use the full screen size while maintaining the video's aspect ratio,

        // which naturally entails some cropping of the video

       float aspectRatioVideo = (float)videoMode.mWidth / (float)videoMode.mHeight;

       float aspectRatioView = viewHeight / viewWidth;

        

       if (aspectRatioVideo < aspectRatioView) {

            // Video (when rotated) is wider than the view: crop left and right

            // (top and bottom of video)

            

            // --============--

            // - =          = _

            // - =          = _

            // - =          = _

            // - =          = _

            // - =          = _

            // - =          = _

            // - =          = _

            // - =          = _

            // --============--

            

            config.mSize.data[0] = (int)videoMode.mHeight * (viewHeight / (float)videoMode.mWidth);

            config.mSize.data[1] = (int)viewHeight;

        }

       else {

            // Video (when rotated) is narrower than the view: crop top and

            // bottom (left and right of video).  Also used when aspect ratios

            // match (no cropping)

            

            // ------------

            // -          -

            // -          -

            // ============

            // =          =

            // =          =

            // =          =

            // =          =

            // =          =

            // =          =

            // =          =

            // =          =

            // ============

            // -          -

            // -          -

            // ------------

            

            config.mSize.data[0] = (int)viewWidth;

            config.mSize.data[1] = (int)videoMode.mWidth * (viewWidth / (float)videoMode.mHeight);

        }

    }

   else {

        // --- View is landscape ---

       float temp = viewWidth;

        viewWidth = viewHeight;

        viewHeight = temp;

        

        // Compare aspect ratios of video and screen.  If they are different we

        // use the full screen size while maintaining the video's aspect ratio,

        // which naturally entails some cropping of the video

       float aspectRatioVideo = (float)videoMode.mWidth / (float)videoMode.mHeight;

       float aspectRatioView = viewWidth / viewHeight;

        

       if (aspectRatioVideo < aspectRatioView) {

            // Video is taller than the view: crop top and bottom

            

            // --------------------

            // ====================

            // =                  =

            // =                  =

            // =                  =

            // =                  =

            // ====================

            // --------------------

            

            config.mSize.data[0] = (int)viewWidth;

            config.mSize.data[1] = (int)videoMode.mHeight * (viewWidth / (float)videoMode.mWidth);

        }

       else {

            // Video is wider than the view: crop left and right.  Also used

            // when aspect ratios match (no cropping)

            

            // ---====================---

            // -  =                  =  -

            // -  =                  =  -

            // -  =                  =  -

            // -  =                  =  -

            // ---====================---

            

            config.mSize.data[0] = (int)videoMode.mWidth * (viewHeight / (float)videoMode.mHeight);

            config.mSize.data[1] = (int)viewHeight;

        }

    }

    

    // Calculate the viewport for the app to use when rendering

   viewport.posX = ((viewWidth - config.mSize.data[0]) /2) + config.mPosition.data[0];

   viewport.posY = (((int)(viewHeight - config.mSize.data[1])) / (int) 2) + config.mPosition.data[1];

    viewport.sizeX = config.mSize.data[0];

    viewport.sizeY = config.mSize.data[1];

 

#ifdef DEBUG_SAMPLE_APP

    NSLog(@"VideoBackgroundConfig: size: %d,%d", config.mSize.data[0], config.mSize.data[1]);

    NSLog(@"VideoMode:w=%d h=%d", videoMode.mWidth, videoMode.mHeight);

    NSLog(@"width=%7.3f height=%7.3f", viewWidth, viewHeight);

    NSLog(@"ViewPort: X,Y: %d,%d Size X,Y:%d,%d", viewport.posX,viewport.posY,viewport.sizeX,viewport.sizeY);

#endif

    

    // Set the config

    QCAR::Renderer::getInstance().setVideoBackgroundConfig(config);

}


- (void) ScalingXY

{

    CGRect rect = [[UIScreenmainScreen]bounds];

    CGSize size = rect.size;//屏幕的尺寸大小

    CGFloat scale = [[UIScreenmainScreen]scale];//分辨率与屏幕尺寸的比


    // Window Coordinates to Normalized Device Coordinates

    QCAR::VideoBackgroundConfig config =QCAR::Renderer::getInstance().getVideoBackgroundConfig();

        

   int posx = ((size.width*scale - config.mSize.data[0]) /2) + config.mPosition.data[0];

   int posy = (((int)(size.height*scale - config.mSize.data[1])) / (int) 2) + config.mPosition.data[1];

    float scalingX;

   float scalingY;

   if (posx ==0) {

       scalingX = size.width*0.5;

    }

   else

       scalingX = size.width*(-posx/(size.width*scale)+0.5);

    

   if (posy ==0) {

       scalingY = size.height*0.5;

    }

   else

       scalingY = size.height*(-posy/(size.height*scale)+0.5);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值