gpt4 book ai didi

ios - 在 AVCaptureVideoPreviewLayer 上绘制一个矩形,可能吗?

转载 作者:可可西里 更新时间:2023-11-01 03:59:48 26 4
gpt4 key购买 nike

几天来我一直在努力解决这个问题。

我想在 CALayer (AVCaptureVideoPreviewLayer) 的顶部绘制一个矩形,它恰好是来自 iPhone4 相机的视频源。

这是我的部分设置;

    //(in function for initialization)

-(void)initDevices {
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil];

AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES;
captureOutput.minFrameDuration = CMTimeMake(1, 30);
dispatch_queue_t queue;
queue = dispatch_queue_create("cameraQueue", NULL);
[captureOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureOutput setVideoSettings:videoSettings];
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession addInput:captureInput];
[self.captureSession addOutput:captureOutput];
[self.captureSession setSessionPreset:AVCaptureSessionPresetHigh];
self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession];
self.prevLayer.frame = CGRectMake(0, 0, 400, 400);
self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.prevLayer.delegate = self;
[self.view.layer addSublayer: self.prevLayer];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {

[self performSelectorOnMainThread:@selector(drawme) withObject:nil waitUntilDone:YES];
}

- (void)drawme {
[self.prevLayer setNeedsDisplay];
}

//delegate function that draws to a CALayer
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {
NSLog(@"hello layer!");
CGContextSetRGBFillColor (ctx, 1, 0, 0, 1);
CGContextFillRect (ctx, CGRectMake (0, 0, 200, 100 ));
}

这可能吗?从我当前的代码中,我打印了“hello layer”,但相机画面没有填充的矩形。

任何帮助都会很棒。 :)

最佳答案

我认为您应该向 AVCaptureVideoPreviewLayer 添加另一个层,我会为您修改示例代码。你可以试试。

    //(in function for initialization)

-(void)initDevices {
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil];

AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES;
captureOutput.minFrameDuration = CMTimeMake(1, 30);
dispatch_queue_t queue;
queue = dispatch_queue_create("cameraQueue", NULL);
[captureOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureOutput setVideoSettings:videoSettings];
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession addInput:captureInput];
[self.captureSession addOutput:captureOutput];
[self.captureSession setSessionPreset:AVCaptureSessionPresetHigh];
self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession];
self.prevLayer.frame = CGRectMake(0, 0, 400, 400);
self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:self.prevLayer];

self.drawLayer = [CAShapeLayer layer];
CGRect parentBox = [self.captureVideoPreviewLayer frame];
[self.drawLayer setFrame:parentBox];
[self.drawLayer setDelegate:self];
[self.drawLayer setNeedsDisplay];
[self.captureVideoPreviewLayer addSublayer:self.drawLayer];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {

[self performSelectorOnMainThread:@selector(drawme) withObject:nil waitUntilDone:YES];
}

- (void)drawme {
[self.drawLayer setNeedsDisplay];
}

//delegate function that draws to a CALayer
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {
NSLog(@"hello layer!");
CGContextSetRGBFillColor (ctx, 1, 0, 0, 1);
CGContextFillRect (ctx, CGRectMake (0, 0, 200, 100 ));
}

关于ios - 在 AVCaptureVideoPreviewLayer 上绘制一个矩形,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4435401/

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