SlideShare a Scribd company logo
カメラアプリ開発入門
(第2回)
AVFoundationを使った無音カメラアプリの作り方
2013/6/29 名古屋iPhone開発者勉強会
13年6月29日土曜日
大塚 崇(おおつか たかし)
DJ / フリーランスのエンジニア・プログラマ
ハンドル名: takatronix
Facebook/Twitter/Skype/LINE/Weibo -> takatronix
http://takatronix.com
趣味興味:旅行、語学、筋トレ、LEGO、FX、心理学、 
脳科学、宇宙
自己紹介
13年6月29日土曜日
リリースしたアプリ
セクシーミラー、SEXY SCAN、 放射能汚染
地図、和牛スキャン ...
(代表作)
セクシーミラー-編集不要の神自撮りアプリ
13年6月29日土曜日
http://sexymirror-app.com
2013/1リリース イギリスのiPhone総合で
10位に、現在42万ダウンロード
セクシーミラー-編集不要の神自撮りアプリ
13年6月29日土曜日
iOSカメラAPI
UIImagePickerController
AVFoundation.framework
iOS4から、標準のカメラUIを使わない
アプリが作れる
よくあるカメラのUI
非常に簡単だが自由がない
リアルタイムエフェクトはできない
実装は結構大変だがなんでもできる
13年6月29日土曜日
AVFoundationを扱うのはけっこう大変
初期化だけでもかなりめんどくさい
ので....
AVFoundationを超絶簡単に使
えるクラスをつくりました
13年6月29日土曜日
サンプルのプロジェクトを用意しました。
http://takatronix.com/tutorial/20130629.zip
13年6月29日土曜日
#import <UIKit/UIKit.h>
#import "CameraManager.h"
@interface ViewController : UIViewController
@property CameraManager* cameraManager; // カメラマネージャクラス
@property IBOutlet UIImageView* previewView; // プレビューを配置するビュー
@property IBOutlet UIImageView* captureview; // キャプチャ後のイメージ
@end
CameraManagerクラスの使い方
13年6月29日土曜日
初期化
- (void)viewDidLoad
{
[super viewDidLoad];
// カメラクラスを初期化
_cameraManager = CameraManager.new;
// プレビューレイヤを設定
[_cameraManager setPreview:_previewView];
}
13年6月29日土曜日
撮影
// 静止画撮影(シャッター音あり)
-(IBAction)photo:(id)sender{
[_cameraManager takePhoto:^(UIImage *image, NSError *error) {
_captureview.image = image;
}];
}
// ビデオイメージ取得(シャッター音なし)
-(IBAction)video:(id)sender{
_captureview.image = _cameraManager.rotatedVideoImage;
}
13年6月29日土曜日
カメラ制御
// バックカメラを使う
-(IBAction)back:(id)sender{
[_cameraManager useFrontCamera:NO];
}
// フロントカメラを使う
-(IBAction)front:(id)sender{
[_cameraManager useFrontCamera:YES];
}
// カメラ切り替え
-(IBAction)flip:(id)sender{
[_cameraManager flipCamera];
}
13年6月29日土曜日
ライト制御
// ライト切り替え
-(IBAction)light:(id)sender{
[_cameraManager lightToggle];
}
// ライトON
-(IBAction)lightOn:(id)sender{
[_cameraManager light:YES];
}
// ライトOFF
-(IBAction)lightOff:(id)sender{
[_cameraManager light:NO];
}
13年6月29日土曜日
こんな感じに簡単につかえます。
詳しい使い方はCameraManager.hを読んで
ください
13年6月29日土曜日
さっそく
コードを読みながら
理解しよう
13年6月29日土曜日
基本的なクラスを
ざっと解説
13年6月29日土曜日
AVCaptureSession
セッション管理をするクラス
入力と出力をつなぎ映像や音声の流
れを定義し、実行する
13年6月29日土曜日
AVCaptureInput
カメラ、マイクなどの入力デバイスの
データを受け取る
AVCaptureSessionにつなぐ
13年6月29日土曜日
AVCaptureDevice
カメラやマイクなどのデバイス
ライトをつけたり、フォーカスの制
御したりするときに使う
AVCaptureInputを作成するときに使う
13年6月29日土曜日
AVCaptureOutput
ファイルやバッファなどの出力
AVCaptureSessionにつなぐ
AVCaptureStillImageOutput 静止画
AVCaptureAudioDataOutput
AVCaptureVideoDataOutput
オーディオ
ビデオ
いろんなのありますw
13年6月29日土曜日
AVCaptureVideoPreviewLayer
プレビューを表示するCALayer
カメラの生のデータを表示できる
リアルタイムエフェクトしたい場合には
使えないが、表示は速い。
AVCaptureSessionにつなぐ
13年6月29日土曜日
AVCaptureConnection
入力と出力の接続状態の設定とか
も、もうやめて・・・ って感じですよねw
13年6月29日土曜日
AVCaptureSession
AVCaptureOutput
AVCaptureInput
AVCaptureInput
AVCaptureOutput
startRunningで開始
カメラの入力
画像化
13年6月29日土曜日
// デフォルトはバックカメラ
videoInput = [AVCaptureDeviceInput deviceInputWithDevice:self.backCameraDevice error:nil];
/////////////////////////////////////////////////
// キャプチャセッションの作成
/////////////////////////////////////////////////
! captureSession = AVCaptureSession.new;
[captureSession setSessionPreset:preset];
[captureSession addInput:videoInput];
!self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
! [self.previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
[self setupImageCapture];
[self setupVideoCapture];
[captureSession startRunning];
セッションの作成、初期化
13年6月29日土曜日
カメラを変更するときなど接続を組み直すとき
CaptureSessionの変更
// カメラを有効化する
-(void)enableCamera:(AVCaptureDevicePosition)desiredPosition{
[captureSession stopRunning];
for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
if ([d position] == desiredPosition) {
[captureSession beginConfiguration];
videoInput= [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
for (AVCaptureInput *oldInput in [[_previewLayer session] inputs]) {
[captureSession removeInput:oldInput];
}
[captureSession addInput:videoInput];
[captureSession commitConfiguration];
break;
}
}
[captureSession startRunning];
}
[captureSession beginConfigration]
[captureSession commitConfiration]
入出力の切り替え
反映
13年6月29日土曜日
静止画の取得
AVCaptureStillImageOutput
CaptureSessionに接続した
AVCaptureStillImageOutputから
CMSampleBufferを取得
デバイスの向きに合わせたUImageに変換
-(void)takePhoto:(takePhotoBlock) block
表示
13年6月29日土曜日
動画の取得
AVCaptureVideoDataOutput
CaptureSessionに
AVCaptureVideoDataOutputを接続
CaptureSettion startRunning後
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
が、ビデオのフレームの更新毎に呼ばれる
13年6月29日土曜日
画像データの表示までの流れ
CMSampleBuffer
CVImageBuffer
CGImage
UIImage
Core Media
CoreVideo
Core Graphics
UIKit
13年6月29日土曜日
取得画像の向き
デバイスの向きが縦でも横でも取得す
るビデオイメージは横向き固定
撮影時のデバイスの向きを記録し、画
像化するときに回転させること
CameraManager rotatedVideoImageを参照
13年6月29日土曜日
例題
プレビューレイヤを使わずに取得し
た画像をリアルタイムでモノクロ化
してみよう
*モノクロ化のコードは前回のチュートリアル
を参考に
http://takatronix.com/tutorial/20130525.zip
13年6月29日土曜日
プロジェクトとこのスライドはここから
落とせますよ。
http://takatronix.com/tutorial/20130629.zip
13年6月29日土曜日
takatronix検索
http://takatronix.com
13年6月29日土曜日
ありがとうございました
takatronix検索
http://takatronix.com
13年6月29日土曜日

More Related Content

PPT
Fundamentals of Financial Ratio Analysis
PPTX
Property, Plant & Equipment IAS 16
PPTX
Activity based costing
PPT
Valuation
PDF
Financal feasibility study
PPTX
ACTIVITY BASED COSTING
PPT
Financial Planning and Forecasting
DOC
Tcs synonyms and antonyms
Fundamentals of Financial Ratio Analysis
Property, Plant & Equipment IAS 16
Activity based costing
Valuation
Financal feasibility study
ACTIVITY BASED COSTING
Financial Planning and Forecasting
Tcs synonyms and antonyms

What's hot (6)

PPT
Risk And Return
PPT
Target Costing.ppt
PPT
Risk and Return: Portfolio Theory and Assets Pricing Models
PPSX
Ind AS 116 Leases
PPTX
PPT
Traditional &amp; Abc
Risk And Return
Target Costing.ppt
Risk and Return: Portfolio Theory and Assets Pricing Models
Ind AS 116 Leases
Traditional &amp; Abc
Ad

AVFoundationを使った無音カメラアプリの作り方