Re: Display USB Video Camera output : AVFoundation?
Re: Display USB Video Camera output : AVFoundation?
- Subject: Re: Display USB Video Camera output : AVFoundation?
- From: Jerry Krinock <email@hidden>
- Date: Sat, 12 Sep 2015 15:49:32 -0700
Well, AVCaptureView is not very malleable. I needed to use an AVCaptureVideoPreviewLayer, which needed quite a few more lines of code than the zero I reported yesterday :( For the record, I have pasted in the working code below.
The only problem is that this silly warning prints to the console whenever my view loads:
CoreAnimation: Warning! CAImageQueueSetOwner() is deprecated and does nothing. Please stop calling this method.
I am doing no such thing. This is built with the 10.10 SDK, running in 10.11. Can anyone explain? Otherwise I’ll report as a bug in 10.11.
Jerry
*** Camera.h ***
@interface Camera : NSObject
+ (Camera*)sharedCamera ;
- (BOOL)startInView:(NSView*)view
error_p:(NSError**)error_p ;
@end
*** Camera.m ***
#import <AVFoundation/AVFoundation.h>
static Camera* sharedCamera = nil ;
@interface Camera ()
@property AVCaptureSession* session ;
@end
@implementation Camera
+ (Camera*)sharedCamera {
if (sharedCamera == nil) {
sharedCamera = [[self alloc] init] ;
}
return sharedCamera ;
}
- (BOOL)startInView:(NSView*)view
error_p:(NSError**)error_p {
BOOL ok = NO ;
NSError* error = nil ;
AVCaptureDevice* device = nil ;
for (device in [AVCaptureDevice devices]) {
/* We want an external video camera. Grab the first device which will
do video and is not vended by Apple Inc. This code could be improved
if more properties of the intended camera were available. */
if ([device hasMediaType:AVMediaTypeVideo]) {
NSString* modelID = [device modelID] ;
if ([modelID rangeOfString:@"VendorID_0x106B"].location == NSNotFound) {
ok = YES ;
break ;
}
}
}
if (!ok) {
NSDictionary* info = @{
NSLocalizedDescriptionKey
: NSLocalizedString(@"No external camera found", nil),
} ;
error = [NSError errorWithDomain:kMyErrorDomain
code:258740
userInfo:info] ;
}
AVCaptureDeviceInput* input = nil ;
if (device) {
input = [[AVCaptureDeviceInput alloc] initWithDevice:device
error:&error] ;
if (!input) {
ok = NO ;
}
}
if (ok) {
self.session = [[AVCaptureSession alloc] init] ;
[self.session addInput:input] ;
AVCaptureVideoPreviewLayer* layer ;
layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session] ;
layer.contentsGravity = kCAGravityResizeAspectFill ;
view.layer = layer ;
[view setWantsLayer:YES] ;
layer.contentsGravity = kCAGravityResizeAspectFill ;
view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize ;
view.layerContentsPlacement = NSViewLayerContentsPlacementCenter ;
layer.frame = view.bounds ;
[self.session startRunning] ;
}
if (error && error_p) {
*error_p = error ;
}
return ok ;
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden