Skip to content

Instantly share code, notes, and snippets.

@kengonakajima
Last active March 16, 2022 08:32
Show Gist options
  • Save kengonakajima/74d648d67a860b65dfb9be6da1de6180 to your computer and use it in GitHub Desktop.
Save kengonakajima/74d648d67a860b65dfb9be6da1de6180 to your computer and use it in GitHub Desktop.
//
// ViewController.h
// cameratest3gameobjc
//
// Created by ringo on 2022/03/16.
//
#import <Cocoa/Cocoa.h>
#import <SpriteKit/SpriteKit.h>
#import <GameplayKit/GameplayKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : NSViewController<AVCaptureVideoDataOutputSampleBufferDelegate>
@property (assign) IBOutlet SKView *skView;
//@interface ViewController : UIViewController
@property (nonatomic, strong) AVCaptureSession* session;
@property (nonatomic, strong) IBOutlet NSImageView* imageView;
@end
//
// ViewController.m
// cameratest3gameobjc
//
// Created by ringo on 2022/03/16.
//
#import "ViewController.h"
#import "GameScene.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Load the SKScene from 'GameScene.sks'
GameScene *scene = (GameScene *)[SKScene nodeWithFileNamed:@"GameScene"];
// Set the scale mode to scale to fit the window
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene
[self.skView presentScene:scene];
self.skView.showsFPS = YES;
self.skView.showsNodeCount = YES;
///////////////////
///
AVCaptureDevice *inputDevice = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for(AVCaptureDevice *camera in devices) {
NSLog([camera localizedName]);
// if([camera position] == AVCaptureDevicePositionFront) { // is front camera
inputDevice = camera;
break;
// }
}
printf("front:%p\n",inputDevice);
//デバイス取得
AVCaptureDevice* device = inputDevice;// [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//入力作成
AVCaptureDeviceInput* deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
//ビデオデータ出力作成
NSDictionary* settings = @{(id)kCVPixelBufferPixelFormatTypeKey:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]}; // 32RGBAはあるけどエラーになる
AVCaptureVideoDataOutput* dataOutput = [[AVCaptureVideoDataOutput alloc] init];
dataOutput.videoSettings = settings;
[dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
//セッション作成
self.session = [[AVCaptureSession alloc] init];
[self.session addInput:deviceInput];
[self.session addOutput:dataOutput];
self.session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureConnection *videoConnection = NULL;
// カメラの向きなどを設定する
[self.session beginConfiguration];
for ( AVCaptureConnection *connection in [dataOutput connections] )
{
for ( AVCaptureInputPort *port in [connection inputPorts] )
{
if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
}
}
}
if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
{
[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}
[self.session commitConfiguration];
// セッションをスタートする
[self.session startRunning];
}
//delegateメソッド。各フレームにおける処理
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
// イメージバッファの取得
CVImageBufferRef buffer;
buffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// イメージバッファのロック
CVPixelBufferLockBaseAddress(buffer, 0);
// イメージバッファ情報の取得
uint8_t* base;
size_t width, height, bytesPerRow;
base = CVPixelBufferGetBaseAddress(buffer);
width = CVPixelBufferGetWidth(buffer);
height = CVPixelBufferGetHeight(buffer);
bytesPerRow = CVPixelBufferGetBytesPerRow(buffer);
printf("base:%d %d %d w:%d h:%d bpr:%d\n", base[0], base[1], base[2], (int)width, (int)height,(int)bytesPerRow);
// イメージバッファのアンロック
CVPixelBufferUnlockBaseAddress(buffer, 0);
}
@end
@kengonakajima
Copy link
Author

Info.plistにPrivacyのCameraの説明を追加するのと、 Signing and CapabilitiesでCameraをオンにする。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment