public
Last active

CoreImage quick'n'dirty sample: Contrast/Saturation/Brightness editing (screenshot http://twitpic.com/93gfty)

  • Download Gist
AppDelegate.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
using System;
using System.Collections.Generic;
using System.Linq;
 
using MonoTouch.Foundation;
using MonoTouch.UIKit;
 
namespace ColorControl {
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate {
 
static void Main (string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, null, "AppDelegate");
}
 
// class-level declarations
UIWindow window;
UINavigationController navigationController;
UIViewController viewController;
 
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
viewController = new ImageViewController();
navigationController = new UINavigationController();
navigationController.PushViewController (viewController, false);
 
// If you have defined a view, add it here:
window.AddSubview (navigationController.View);
// make the window visible
window.MakeKeyAndVisible ();
return true;
}
}
}
Camera.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
//
// Camera.cs: Support code for taking pictures
//
// Copyright 2010 Miguel de Icaza
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
 
namespace TweetStation
{
//
// A static class that will reuse the UIImagePickerController
// as iPhoneOS has a crash if multiple UIImagePickerController are created
// http://stackoverflow.com/questions/487173
// (Follow the links)
//
public static class Camera
{
static UIImagePickerController picker;
static Action<NSDictionary> _callback;
static void Init ()
{
if (picker != null)
return;
picker = new UIImagePickerController ();
picker.Delegate = new CameraDelegate ();
}
class CameraDelegate : UIImagePickerControllerDelegate {
public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
{
var cb = _callback;
_callback = null;
picker.DismissModalViewControllerAnimated (true);
cb (info);
}
}
public static void TakePicture (UIViewController parent, Action<NSDictionary> callback)
{
Init ();
picker.SourceType = UIImagePickerControllerSourceType.Camera;
_callback = callback;
parent.PresentModalViewController (picker, true);
}
public static void SelectPicture (UIViewController parent, Action<NSDictionary> callback)
{
Init ();
picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
_callback = callback;
parent.PresentModalViewController (picker, true);
}
}
}
ImageViewController.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
using System;
using System.Drawing;
using MonoTouch.AssetsLibrary;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.CoreImage;
using MonoTouch.CoreGraphics;
 
namespace ColorControl {
 
public class ImageViewController : UIViewController {
UIImage sourceImage, displayImage;
NSDictionary sourceMeta;
 
UIButton cameraButton, saveButton, resetButton;
UIImageView imageView;
UISlider sliderC, sliderS, sliderB;
UILabel labelC, labelS, labelB;
 
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
Title = "Color Controls";
View.BackgroundColor = UIColor.White;
 
cameraButton = UIButton.FromType (UIButtonType.RoundedRect);
cameraButton.Frame = new RectangleF(10, 10, 90,40);
cameraButton.SetTitle ("Camera", UIControlState.Normal);
cameraButton.TouchUpInside += (sender, e) => {
TweetStation.Camera.TakePicture (this, (obj) =>{
// https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPickingMediaWithInfo:
var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;
var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary;
sourceImage = photo;
displayImage = Scale (sourceImage, new SizeF(300, 200));
imageView.Image = displayImage;
sourceMeta = meta;
});
};
View.Add (cameraButton);
 
resetButton = UIButton.FromType (UIButtonType.RoundedRect);
resetButton.Frame = new RectangleF(110, 10, 90, 40);
resetButton.SetTitle ("Reset", UIControlState.Normal);
resetButton.TouchUpInside += (sender, e) => {
sliderS.Value = 1;
sliderB.Value = 0;
sliderC.Value = 1;
HandleValueChanged (sender, e);
};
View.Add (resetButton);
 
saveButton = UIButton.FromType (UIButtonType.RoundedRect);
saveButton.Frame = new RectangleF(210, 10, 90, 40);
saveButton.SetTitle ("Save", UIControlState.Normal);
saveButton.TouchUpInside += (sender, e) => {
ALAssetsLibrary library = new ALAssetsLibrary();
var img = AdjustImage (sourceImage);
if(sourceMeta == null) sourceMeta = new NSDictionary();
 
library.WriteImageToSavedPhotosAlbum (img.CGImage, sourceMeta, (assetUrl, error) => {
Console.WriteLine ("SAVED TO assetUrl:"+assetUrl);
});
};
View.Add (saveButton);
 
labelC = new UILabel(new RectangleF(10, 70, 90, 20));
labelS = new UILabel(new RectangleF(10, 110, 90, 20));
labelB = new UILabel(new RectangleF(10, 150, 90, 20));
 
labelC.Text = "Contrast";
labelS.Text = "Saturation";
labelB.Text = "Brightness";
 
View.Add (labelC);
View.Add (labelS);
View.Add (labelB);
 
sliderB = new UISlider(new RectangleF(100, 70, 210, 20));
sliderS = new UISlider(new RectangleF(100, 110, 210, 20));
sliderC = new UISlider(new RectangleF(100, 150, 210, 20));
// http://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/filter/ci/CIColorControls
 
sliderS.MinValue = 0;
sliderS.MaxValue = 2;
sliderS.Value = 1;
sliderB.MinValue = -1;
sliderB.MaxValue = 1;
sliderB.Value = 0;
 
sliderC.MinValue = 0;
sliderC.MaxValue = 4;
sliderC.Value = 1;
sliderC.TouchUpInside += HandleValueChanged;
sliderS.TouchUpInside += HandleValueChanged;
sliderB.TouchUpInside += HandleValueChanged;
View.Add (sliderC);
View.Add (sliderS);
View.Add (sliderB);
 
imageView = new UIImageView(new RectangleF(10, 190, 300, 200));
sourceImage = UIImage.FromFile ("clouds.jpg");
displayImage = sourceImage;
imageView.Image = displayImage;
View.Add (imageView);
 
if (!UIImagePickerController.IsSourceTypeAvailable (UIImagePickerControllerSourceType.Camera)) {
cameraButton.SetTitle ("No camera", UIControlState.Disabled);
cameraButton.SetTitleColor (UIColor.Gray, UIControlState.Disabled);
cameraButton.Enabled = false;
}
}
 
void HandleValueChanged (object sender, EventArgs e)
{ // use the low-res version
imageView.Image = AdjustImage (displayImage);
}
 
UIImage AdjustImage (UIImage image) {
var colorCtrls = new CIColorControls ()
{
Image = CIImage.FromCGImage (sourceImage.CGImage),
Brightness = sliderB.Value,
Saturation = sliderS.Value,
Contrast = sliderC.Value
};
var output = colorCtrls.OutputImage;
var context = CIContext.FromOptions (null);
var result = context.CreateCGImage (output, output.Extent);
colorCtrls = null;
 
return UIImage.FromImage(result);
}
// http://stackoverflow.com/questions/2417969/resize-and-saving-an-image-to-disk-in-monotouch
public static UIImage Scale (UIImage source, SizeF newSize)
{
UIGraphics.BeginImageContext (newSize);
var context = UIGraphics.GetCurrentContext ();
context.TranslateCTM (0, newSize.Height);
context.ScaleCTM (1f, -1f);
context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), source.CGImage);
var scaledImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return scaledImage;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.