@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);
}