Ios 如何修改Rosywriter演示以显示1080p视频的所有内容
我正在使用RosyWriter demo的一个测试应用程序,根据原始代码,当我们拍摄1920*1080维视频时,实际上我们只能在预览中看到1620*1080区域(宽高比为4/3960*640),我想修改代码以显示视频的所有内容(窗口大小应为960*540),并在视图上下显示黑色帧。我可以看出,以下代码实现了这一结果:Ios 如何修改Rosywriter演示以显示1080p视频的所有内容,ios,opengl-es,Ios,Opengl Es,我正在使用RosyWriter demo的一个测试应用程序,根据原始代码,当我们拍摄1920*1080维视频时,实际上我们只能在预览中看到1620*1080区域(宽高比为4/3960*640),我想修改代码以显示视频的所有内容(窗口大小应为960*540),并在视图上下显示黑色帧。我可以看出,以下代码实现了这一结果: - (CGRect)textureSamplingRectForCroppingTextureWithAspectRatio:(CGSize)textureAspectRatio
- (CGRect)textureSamplingRectForCroppingTextureWithAspectRatio:(CGSize)textureAspectRatio toAspectRatio:(CGSize)croppingAspectRatio
{
CGRect normalizedSamplingRect = CGRectZero;
CGSize cropScaleAmount = CGSizeMake(croppingAspectRatio.width / textureAspectRatio.width, croppingAspectRatio.height / textureAspectRatio.height);
CGFloat maxScale = fmax(cropScaleAmount.width, cropScaleAmount.height);
CGSize scaledTextureSize = CGSizeMake(textureAspectRatio.width * maxScale, textureAspectRatio.height * maxScale);
if ( cropScaleAmount.height > cropScaleAmount.width ) {
normalizedSamplingRect.size.width = croppingAspectRatio.width / scaledTextureSize.width;
normalizedSamplingRect.size.height = 1.0;
}
else {
normalizedSamplingRect.size.height = croppingAspectRatio.height / scaledTextureSize.height;
normalizedSamplingRect.size.width = 1.0;
}
// Center crop
normalizedSamplingRect.origin.x = (1.0 - normalizedSamplingRect.size.width)/2.0;
normalizedSamplingRect.origin.y = (1.0 - normalizedSamplingRect.size.height)/2.0;
return normalizedSamplingRect;
}
- (void)displayPixelBuffer:(CVImageBufferRef)pixelBuffer
{
if (frameBufferHandle == 0) {
BOOL success = [self initializeBuffers];
if ( !success ) {
NSLog(@"Problem initializing OpenGL buffers.");
}
}
if (videoTextureCache == NULL)
return;
// Create a CVOpenGLESTexture from the CVImageBuffer
size_t frameWidth = CVPixelBufferGetWidth(pixelBuffer);
size_t frameHeight = CVPixelBufferGetHeight(pixelBuffer);
CVOpenGLESTextureRef texture = NULL;
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
videoTextureCache,
pixelBuffer,
NULL,
GL_TEXTURE_2D,
GL_RGBA,
frameWidth,
frameHeight,
GL_BGRA,
GL_UNSIGNED_BYTE,
0,
&texture);
if (!texture || err) {
NSLog(@"CVOpenGLESTextureCacheCreateTextureFromImage failed (error: %d)", err);
return;
}
glBindTexture(CVOpenGLESTextureGetTarget(texture), CVOpenGLESTextureGetName(texture));
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferHandle);
// Set the view port to the entire view
glViewport(0, 0, renderBufferWidth, renderBufferHeight);
//NSLog(@"render width:%d height:%d",renderBufferWidth,renderBufferHeight);
static const GLfloat squareVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
// The texture vertices are set up such that we flip the texture vertically.
// This is so that our top left origin buffers match OpenGL's bottom left texture coordinate system.
CGRect textureSamplingRect = [self textureSamplingRectForCroppingTextureWithAspectRatio:CGSizeMake(frameWidth, frameHeight) toAspectRatio:self.bounds.size];
GLfloat textureVertices[] = {
CGRectGetMinX(textureSamplingRect), CGRectGetMaxY(textureSamplingRect),//0.078125, 1.0
CGRectGetMaxX(textureSamplingRect), CGRectGetMaxY(textureSamplingRect),//0.921875, 1.0
CGRectGetMinX(textureSamplingRect), CGRectGetMinY(textureSamplingRect),//0.078125, 1.0
CGRectGetMaxX(textureSamplingRect), CGRectGetMinY(textureSamplingRect),//0.921875, 0.0
};
// Draw the texture on the screen with OpenGL ES 2
[self renderWithSquareVertices:squareVertices textureVertices:textureVertices];
//[oglContext presentRenderbuffer:GL_RENDERBUFFER];
glBindTexture(CVOpenGLESTextureGetTarget(texture), 0);
// Flush the CVOpenGLESTexture cache and release the texture
CVOpenGLESTextureCacheFlush(videoTextureCache, 0);
CFRelease(texture);
}
我想我必须修改textureSamplingRect和textureVertices部分以获得我的效果,但我是opengl es的新手,因此,有人能详细了解如何做到这一点吗?如果过滤视频同时保留原始纵横比是你想要的,我这里有:。您可以使用
kgpuimagefillmodepreserveapectratio的GPUImageView的fillMode
在那里为您处理此问题。谢谢您,Brad。我注意到你在SO中无处不在,随时可以寻求帮助,谢谢。如果你只想在过滤视频的同时保留原始纵横比,我在这里有:。您可以使用kgpuimagefillmodepreserveapectratio的GPUImageView的fillMode
在那里为您处理此问题。谢谢您,Brad。我注意到你到处都是,随时准备帮助,谢谢。