gpt4 book ai didi

ios AVFoundation 点击​​聚焦

转载 作者:IT王子 更新时间:2023-10-29 07:57:27 24 4
gpt4 key购买 nike

我正在尝试创建一个相机应用程序,它或多或少会像默认相机应用程序一样工作。目前对我不起作用的东西是点击以集中注意力。我希望相机聚焦并在我触摸的点上做任何事情,就像真正的相机应用程序一样。

这是我的 viewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];

// Session
_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPresetPhoto;

// Input
_videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_videoInput = [AVCaptureDeviceInput deviceInputWithDevice:_videoDevice error:nil];

// Output
_frameOutput = [[AVCaptureVideoDataOutput alloc] init];
_frameOutput.videoSettings = [NSDictionary dictionaryWithObject:AVVideoCodecJPEG forKey:AVVideoCodecKey];

[_frameOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
[_session addInput:_videoInput];
[_session addOutput:_frameOutput];
[_session startRunning];
};

下面是让我的相机在点击时对焦的方法。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:touch.view];
focusLayer.frame = CGRectMake((touchPoint.x-25), (touchPoint.y-25), 50, 50);

if ([_videoDevice isFocusPointOfInterestSupported]) {
NSError *error;
if ([_videoDevice lockForConfiguration:&error]) {
[_videoDevice setFocusPointOfInterest:touchPoint];
[_videoDevice setExposurePointOfInterest:touchPoint];

[_videoDevice setFocusMode:AVCaptureFocusModeAutoFocus];
if ([_videoDevice isExposureModeSupported:AVCaptureExposureModeAutoExpose]){
[_videoDevice setExposureMode:AVCaptureExposureModeAutoExpose];
}
[_videoDevice unlockForConfiguration];
}
}


// NSLog(@"x = %f, y = %f", touchPoint.x, touchPoint.y);
}];
}

点击屏幕后什么都没有发生。

最佳答案

您必须使用类似以下代码的方式将 touchPoint 调整到 [0,1] 范围内:

    CGRect screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;
double focus_x = thisFocusPoint.center.x/screenWidth;
double focus_y = thisFocusPoint.center.y/screenHeight;

[[self captureManager].videoDevice lockForConfiguration:&error];
[[self captureManager].videoDevice setFocusPointOfInterest:CGPointMake(focus_x,focus_y)];
[[self captureManager].videoDevice unlockForConfiguration];

有关这方面的文档可以在 Apple - AV Foundation Programming Guidelines - see section Media Capture where you will find information on Focus Modes 中找到:

If it’s supported, you set the focal point using focusPointOfInterest. You pass a CGPoint where {0,0} represents the top left of the picture area, and {1,1} represents the bottom right in landscape mode with the home button on the right—this applies even if the device is in portrait mode.

关于ios AVFoundation 点击​​聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13095007/

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