我试图让相机输入显示在预览图层视图上.
self.cameraPreviewView与IB中的UIView相关联
这是我目前使用AV Foundation Programming Guide编写的代码.但预览从未显示过
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
NSLog(@"Couldn't create video capture device");
}
[session addInput:input];
// Create video preview layer and add it to the UI
AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
UIView *view = self.cameraPreviewView;
CALayer *viewLayer = [view layer];
newCaptureVideoPreviewLayer.frame = view.bounds;
[viewLayer addSublayer:newCaptureVideoPreviewLayer];
self.cameraPreviewLayer = newCaptureVideoPreviewLayer;
[session startRunning];
Run Code Online (Sandbox Code Playgroud) 我想在我的应用程序中实现自定义相机.所以,我正在创建这个相机AVCaptureDevice.
现在我只想在我的自定义相机中显示灰度输出.所以我试图用这个setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:和AVCaptureWhiteBalanceGains.我正在使用AVCamManual:将AVCam扩展为使用手动捕获.
- (void)setWhiteBalanceGains:(AVCaptureWhiteBalanceGains)gains
{
NSError *error = nil;
if ( [videoDevice lockForConfiguration:&error] ) {
AVCaptureWhiteBalanceGains normalizedGains = [self normalizedGains:gains]; // Conversion can yield out-of-bound values, cap to limits
[videoDevice setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:normalizedGains completionHandler:nil];
[videoDevice unlockForConfiguration];
}
else {
NSLog( @"Could not lock device for configuration: %@", error );
}
}
Run Code Online (Sandbox Code Playgroud)
但为此,我必须将RGB增益值传递到1到4之间.所以我创建了这个方法来检查MAX和MIN值.
- (AVCaptureWhiteBalanceGains)normalizedGains:(AVCaptureWhiteBalanceGains) gains
{
AVCaptureWhiteBalanceGains g = gains;
g.redGain = MAX( 1.0, g.redGain );
g.greenGain = MAX( 1.0, g.greenGain ); …Run Code Online (Sandbox Code Playgroud) 我正在使用AVCaptureDevice.setTorchModeOn(level)方法以可变亮度打开手电筒。
在我的旧 iPhone SE 上,它运行良好——当我level从0变为 时,我可以清楚地看到 4 种不同的亮度级别1。
但在 iPhone 11 Pro 上,手电筒仅在水平为1.0! 如果远离最高水平,它的亮度(与控制中心的手电筒相比)。
我尝试使用maxAvailableTorchLevel常量,但结果与使用1.0.
还尝试了超过的值1.0- 这会导致异常(如预期的那样)。
有人也有这个问题吗?也许有一些解决方法?
我有一个程序来拍摄带有默认参数的视频,当我移动相机时,将自动调整曝光,聚焦和白平衡.我想在程序中添加2个按钮:LOCK和PRESET.按下LOCK按钮时,曝光,对焦和白平衡将被修复,其值将作为用户设置存储.PRESET按钮用于将用户设置设置为相机,以确保我们可以拍摄具有相同曝光,对焦和白平衡值的所有视频.
锁定部分很简单,我只需要改变模式,所以我想知道是否有任何方法可以获得并设置曝光,聚焦和白平衡的值.
以曝光为例,默认情况下,曝光模式设置为AVCaptureExposureModeContinuousAutoExposure,这意味着,当我拿着iPhone拍摄视频时,曝光将自动调整,以确保我们可以在明亮或黑暗的环境中清晰地看到场景.
AVCaptureDevice* pCaptureDevice = [self videoDeviceWithPosition:AVCaptureDevicePositionBack];
[pCaptureDevice lockForConfiguration:nil];
[pCaptureDevice setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
[pCaptureDevice unlockForConfiguration];
Run Code Online (Sandbox Code Playgroud)
按下LOCK按钮时,我将exposureMode更改为AVCaptureExposureModeLocked,因此不再更改当前曝光值.
[pCaptureDevice lockForConfiguration:nil];
[pCaptureDevice setExposureMode:AVCaptureExposureModeLocked];
[pCaptureDevice unlockForConfiguration];
Run Code Online (Sandbox Code Playgroud)
但是,如何在按下按钮时获取曝光值?更重要的是,当按下PRESET时,如何将曝光值设置回AVCaptureDevice.
我在论坛中搜索过,只发现了Michael Grinich的帖子:访问iOS 6新的API,用于相机曝光和快门速度.我按照他的发现并尝试使用私有API获取并设置exposureGain和exposureDuration,但我发现在AVCaptureExposureModeContinuousAutoExposure模式下,当我将相机从黑暗环境移动到明亮时(曝光增益= 1.0和exposureDuration = {1,30,1,0}).设置它们并没有改变曝光度.
[pCaptureDevice lockForConfiguration:nil];
[pCaptureDevice setExposureMode:AVCaptureExposureModeLocked];
[pCaptureDevice setManualExposureSupportEnabled:YES];
NSLog(@"exposure gain: %f", [pCaptureDevice exposureGain]);
[pCaptureDevice setExposureGain:3.0];
NSLog(@"exposure gain: %f", [pCaptureDevice exposureGain]);
[pCaptureDevice unlockForConfiguration];
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题吗?首先十分感谢!
我注意到iPhone 6s上的AVCaptureDeviceFormat 1080p 60 fps不支持焦点像素,因此在光线不足的情况下,移动相机时相机仍会自动对焦。由于焦点搜寻是一个问题,因此这会导致视频录制出现问题。但是,本机摄像头应用程序在1080p 60 fps设置下可完美工作,而在同一场景下无需进行任何对焦搜索。本机相机如何实现它?我尝试在录制之前锁定焦点,还尝试将device.smoothAutoFocusEnabled设置为YES,但结果仍然不如本机Camera应用程序。有任何想法吗 ?
使用QR码阅读器.我是编程新手,所以这可能很容易解决.错误是"类型'AVCaptureDevice'没有成员'defaultDevice'"感谢提前帮助!
//Creating session
let session = AVCaptureSession()
//Define capture device
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do
{
let input = try AVCaptureDeviceInput(device: captureDevice)
session.addInput(input)
}
Run Code Online (Sandbox Code Playgroud) 我编写的Mac OS X应用程序正在使用macbook内置的facetime相机拍摄照片.
在MacBookAir3,2,MacBookPro8,2和MacBookPro10,2上工作正常,但在新的MacBook上它需要"黑暗"的照片.我理解这是因为自动曝光,但我很难让它工作.该AVCaptureDevice adjustingExposure设置为NO,但所拍摄的照片仍然是完全黑暗的.
代码:setupCamera在应用启动期间调用一次
-(void) setupCamera
{
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetPhoto;
sessionInitialized = YES;
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:NULL];
if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure])
[device setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])
[device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance])
[device setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance];
[device unlockForConfiguration];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if(error != nil) {
// ...
}
if([session canAddInput:input]) {
[session addInput:input];
} else {
// ...
}
output = [[AVCaptureStillImageOutput …Run Code Online (Sandbox Code Playgroud) 我正在使用AVFoundation从设备的相机录制到电影文件,使用AVCaptureMovieFileOutput.我希望允许用户在录制开始之前在高帧率和高分辨率模式之间切换,但我无法弄清楚你应该知道哪个AVCaptureDeviceFormat产生适合视频录制的最高分辨率.
例如,在我正在测试的iPod touch上,它列出了后置摄像头的以下设备格式:
<AVCaptureDeviceFormat: 0x13108e260 'vide'/'420v' 192x 144, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @17.00), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310911a0 'vide'/'420f' 192x 144, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @17.00), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310afa60 'vide'/'420v' 352x 288, { 2- 30 fps}, HRSI:2992x2448, fov:49.745, max zoom:153.00 (upscales @8.50), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310af9b0 'vide'/'420f' 352x 288, { 2- 30 fps}, HRSI:2992x2448, fov:49.745, max zoom:153.00 (upscales @8.50), AF …Run Code Online (Sandbox Code Playgroud) 我发现使用AVCaptureVideoPreviewLayer相机覆盖,显示的相机图像的质量比使用相机时更好ARSKView.我需要根据相机输出使用ARKit和显示一些SKNodes,所以我想使用ARSKView和运行它的会话是强制性的.另一方面,似乎AVCaptureVideoPreviewLayer需要设置AVCaptureSession会话.
是否可以通过使用来获取相机输出AVCaptureVideoPreviewLayer并仍然能够显示AR元素ARKit?否则,使用时是否还有提高相机输出质量的目的ARSKView?我的相关会话设置如下:
if ARWorldTrackingConfiguration.isSupported {
let configuration = ARWorldTrackingConfiguration()
skView.session.run(configuration)
}
Run Code Online (Sandbox Code Playgroud) 我在应用中使用前置摄像头。我希望在拍照时用户可以放大和缩小相机
我尝试了这段代码
let device = AVCaptureDevice.default(for: .video)
print(sender.scale)
let vZoomFactor = sender.scale * prevZoomFactor
if sender.state == .ended {
prevZoomFactor = vZoomFactor >= 1 ? vZoomFactor : 1
}
if sender.state == .changed{
do {
try device!.lockForConfiguration()
if (vZoomFactor <= device!.activeFormat.videoMaxZoomFactor) {
device!.videoZoomFactor = max(1.0, min(vZoomFactor, device!.activeFormat.videoMaxZoomFactor))
device?.unlockForConfiguration()
} else {
print("Unable to set videoZoom: (max \(device!.activeFormat.videoMaxZoomFactor), asked \(vZoomFactor))")
}
} catch {
print("\(error.localizedDescription)")
}
}
Run Code Online (Sandbox Code Playgroud)
后置摄像头中的所有功能均正常运行,但前置摄像头未应用变焦。
avcapturedevice ×10
ios ×7
avfoundation ×3
swift ×3
avcapture ×2
adjustment ×1
arkit ×1
autofocus ×1
camera ×1
facetime ×1
flashlight ×1
iphone ×1
macos ×1
objective-c ×1
sprite-kit ×1