add mustache sample

This commit is contained in:
Laurent Sansonetti
2012-03-29 13:20:16 +02:00
parent 8b6963f35c
commit 698203849b
7 changed files with 76 additions and 0 deletions

10
sample/mustache/Rakefile Normal file
View File

@@ -0,0 +1,10 @@
$:.unshift('../../lib')
require 'motion/project'
#ENV['debug'] ||= '0' # Because the REPL cannot be loaded with CoreImage yet.
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'Mustache'
app.frameworks += ['QuartzCore', 'CoreImage']
end

View File

@@ -0,0 +1,9 @@
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = MustacheViewController.alloc.init
@window.rootViewController.wantsFullScreenLayout = true
@window.makeKeyAndVisible
return true
end
end

View File

@@ -0,0 +1,57 @@
class MustacheViewController < UIViewController
def loadView
self.view = UIImageView.alloc.init
@debug_face = false # Set to true to debug face features.
end
def viewDidLoad
view.image = UIImage.imageNamed('matz.jpg')
view.contentMode = UIViewContentModeScaleAspectFit
end
def viewDidAppear(animated)
# CoreImage used a coordinate system which is flipped on the Y axis
# compared to UIKit. Also, a UIImageView can return an image larger than
# itself. To properly translate points, we use an affine transform.
transform = CGAffineTransformMakeScale(view.bounds.size.width / view.image.size.width, -1 * (view.bounds.size.height / view.image.size.height))
transform = CGAffineTransformTranslate(transform, 0, -view.image.size.height)
@detector ||= CIDetector.detectorOfType CIDetectorTypeFace, context:nil, options: { CIDetectorAccuracy: CIDetectorAccuracyHigh }
image = CIImage.imageWithCGImage(view.image.CGImage)
@detector.featuresInImage(image).each do |feature|
next unless feature.hasMouthPosition and feature.hasLeftEyePosition and feature.hasRightEyePosition
if @debug_face
[feature.leftEyePosition,feature.rightEyePosition,feature.mouthPosition].each do |pt|
v = UIView.alloc.initWithFrame CGRectMake(0, 0, 20, 20)
v.backgroundColor = UIColor.greenColor.colorWithAlphaComponent(0.2)
pt = CGPointApplyAffineTransform(pt, transform)
v.center = pt
view.addSubview(v)
end
end
mustacheView = UIImageView.alloc.init
mustacheView.image = UIImage.imageNamed('mustache')
mustacheView.contentMode = UIViewContentModeScaleAspectFit
w = feature.bounds.size.width
h = feature.bounds.size.height / 5
x = (feature.mouthPosition.x + (feature.leftEyePosition.x + feature.rightEyePosition.x) / 2) / 2 - w / 2
y = feature.mouthPosition.y
mustacheView.frame = CGRectApplyAffineTransform([[x, y], [w, h]], transform)
mustacheAngle = Math.atan2(feature.leftEyePosition.x - feature.rightEyePosition.x, feature.leftEyePosition.y - feature.rightEyePosition.y) + Math::PI/2
mustacheView.transform = CGAffineTransformMakeRotation(mustacheAngle)
view.addSubview(mustacheView)
end
end
def shouldAutorotateToInterfaceOrientation(*)
view.subviews.each { |v| v.removeFromSuperview }
viewDidAppear(true)
true
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB