iSightからのビデオ入力をウィンドウにリアルタイムで表示する
公開日:
:
最終更新日:2011/05/30
Mac
記事内に広告を含む場合があります。記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
ども、@akio0911 です。
Cocoaでこんなアプリを作ってみました。
実際にはiSightからのビデオ入力がリアルタイムで表示されています。以下、コードを貼っておきます。
#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>
@interface MyRecorderController : NSObject {
IBOutlet QTCaptureView *mCaptureView;
QTCaptureSession *mCaptureSession;
QTCaptureMovieFileOutput *mCaptureMovieFileOutput;
QTCaptureDeviceInput *mCaptureDeviceInput;
}
- (IBAction)startRecording:(id)sender;
- (IBAction)stopRecording:(id)sender;
@end
#import "MyRecorderController.h"
@implementation MyRecorderController
- (IBAction)startRecording:(id)sender
{
[mCaptureMovieFileOutput recordToOutputFileURL:[NSURL fileURLWithPath:@"/Users/Shared/My Recorded Movie.mov"]];
}
- (IBAction)stopRecording:(id)sender
{
[mCaptureMovieFileOutput recordToOutputFileURL:nil];
}
- (void)captureOutput:(QTCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL forConnections:(NSArray *)connections dueToError:(NSError *)error
{
[[NSWorkspace sharedWorkspace] openURL:outputFileURL];
// Do something with the movie at /Users/Shared/My Recorded Movie.mov
}
- (void)awakeFromNib
{
//Create the capture session
mCaptureSession = [[QTCaptureSession alloc] init];
//Connect inputs and outputs to the session
BOOL success = NO;
NSError *error;
// Find a video device
QTCaptureDevice *device = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
if (device) {
success = [device open:&error];
if (!success) {
// Handle error
}
// Add the video device to the session as device input
mCaptureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:device];
success = [mCaptureSession addInput:mCaptureDeviceInput error:&error];
if (!success) {
// Handle error
}
// Create the movie file output and add it to the session
mCaptureMovieFileOutput = [[QTCaptureMovieFileOutput alloc] init];
success = [mCaptureSession addOutput:mCaptureMovieFileOutput error:&error];
if (!success) {
// Handle error
}
// Set the controller be the movie file output delegate.
[mCaptureMovieFileOutput setDelegate:self];
// Associate the capture view in the UI with the session
[mCaptureView setCaptureSession:mCaptureSession];
}
// Start the capture session running
[mCaptureSession startRunning];
}
- (void)windowWillClose:(NSNotification *)notification
{
[mCaptureSession stopRunning];
[[mCaptureDeviceInput device] close];
}
- (void)dealloc
{
[mCaptureSession release];
[mCaptureDeviceInput release];
[mCaptureMovieFileOutput release];
[super dealloc];
}
@end
スレッドとか管理する必要がないようです。これは楽すぎる。