gpt4 book ai didi

iphone - OpenGL + 相机 View (iPhone)

转载 作者:搜寻专家 更新时间:2023-10-30 20:07:13 25 4
gpt4 key购买 nike

我已经尝试在 iPhone 上通过相机 View 预览显示 OpenGLES View 已经 2 天了。

单独使用相机预览即可。EAGLView(OpenGLES) 单独工作。

问题是当我尝试将 EAGLView 放在相机预览上时。

我可以同时放置两个 UIView,但相机预览始终在 EAGLView 上方(错误!)。当我将相机预览的 alpha 设置为 0.5 时,我可以看到我想要的两个 UIView,但是它们都是模糊的(这是正常的)。

我试过 [self.view bringSubviewToFront:(EAGLView)],但没有任何变化。

EAGLView 作为一个类在 IB 上。CameraView 由代码添加为 subview 。

我在这里放了一些代码,如果你需要的话我可以上传更多。

谢谢!!!

EAGLView

+ (Class)layerClass {
return [CAEAGLLayer class];
}


//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder {


puntosPintar=(GLfloat*)malloc(sizeof(GLfloat)*8);
puntosPintar[0] = -0.25f;
puntosPintar[1] = -1.22f;
puntosPintar[2] = -0.41f;
puntosPintar[3] = 0.0f;
puntosPintar[4] = 0.35f;
puntosPintar[5] = -1.69f;
puntosPintar[6] = 0.15f;
puntosPintar[7] = 0.0f;

if ((self = [super initWithCoder:coder])) {
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

eaglLayer.opaque = NO;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];

context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
return nil;
}


}
return self;
}




- (void)drawView {
const GLubyte squareColors[] = {
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
};

[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);


glMatrixMode(GL_MODELVIEW);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glClear(GL_COLOR_BUFFER_BIT);

glVertexPointer(2, GL_FLOAT, 0, puntosPintar);
glEnableClientState(GL_VERTEX_ARRAY);

glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);

glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 8);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}


- (void)layoutSubviews {
[EAGLContext setCurrentContext:context];

[self destroyFramebuffer];
[self createFramebuffer];

[self drawView];


}


- (BOOL)createFramebuffer {



glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
if (USE_DEPTH_BUFFER) {
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
}



if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}


return YES;
}

UIViewController 我想同时显示两者加载相机 View

    [CameraImageHelper startRunning];
UIView *fafa;
fafa= [[UIView alloc]initWithFrame:self.view.bounds]; //returns a UIView with the cameraview as a layer of that view. It works well (checked)
fafa = [CameraImageHelper previewWithBounds:self.view.bounds];
fafa.alpha=0.5; //Only way to show both
[self.view addSubview:fafa];
[self.view bringSubviewToFront:fafa];

EAGLView 的加载在我创建的 .h 上

IBOutlet EAGLView *openGLVista

在 View 中加载:

openGLVista=[[EAGLView alloc]init];

enter image description here

CameraImageHelper.h

@interface CameraImageHelper : NSObject <AVCaptureVideoDataOutputSampleBufferDelegate>
{
AVCaptureSession *session;
}
@property (retain) AVCaptureSession *session;

+ (void) startRunning;
+ (void) stopRunning;

+ (UIView *) previewWithBounds: (CGRect) bounds;
@end

CameraImageHelper.m

- (void) initialize
{
NSError *error;
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:&error];
if (!captureInput)
{
NSLog(@"Error: %@", error);
return;
}


self.session = [[[AVCaptureSession alloc] init] autorelease];
[self.session addInput:captureInput];
}

- (id) init
{
if (self = [super init]) [self initialize];
return self;
}

- (UIView *) previewWithBounds: (CGRect) bounds
{
UIView *view = [[[UIView alloc] initWithFrame:bounds] autorelease];

AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession: self.session];
preview.frame = bounds;
preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
[view.layer addSublayer: preview];

return view;
}

- (void) dealloc
{
self.session = nil;
[super dealloc];
}

#pragma mark Class Interface

+ (id) sharedInstance // private
{
if(!sharedInstance) sharedInstance = [[self alloc] init];
return sharedInstance;
}

+ (void) startRunning
{
[[[self sharedInstance] session] startRunning];
}

+ (void) stopRunning
{
[[[self sharedInstance] session] stopRunning];
}


+ (UIView *) previewWithBounds: (CGRect) bounds
{
return [[self sharedInstance] previewWithBounds: (CGRect) bounds];
}

@end

最佳答案

我看到在 IB 中您使用 EAGLView 作为 View Controller 的 view,并且在代码片段中您将预览 View 添加为该 View 的 subview 。换句话说,您的 View 层次结构如下所示:

*- EAGLView
+- Preview view

因此,预览 View 始终位于 EAGLView 之上,因为它是 EAGLView 的 subview 。如果您希望能够将其中一个显示在另一个之上,则必须像这样布局:

*- some generic UIView
+- EAGLView
+- Preview view

换句话说,在 IB 中,您应该将一个通用 UIView 绑定(bind)到 view 属性,然后拖动 EAGLView 使其位于该通用 UIView 中。那么您添加预览 View 的代码应该可以正常工作。


顺便说一句,这并不符合您的想法:

fafa= [[UIView alloc]initWithFrame:self.view.bounds]; //returns a UIView with the cameraview as a layer of that view. It works well (checked)
fafa = [CameraImageHelper previewWithBounds:self.view.bounds];

第一行创建了一个通用的 UIView。然后第二个把它扔掉(泄漏内存!),用预览 View 替换它。您应该只删除第一行。

关于iphone - OpenGL + 相机 View (iPhone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5588012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com