/
TakePhotosViewController.cs
178 lines (148 loc) · 6.06 KB
/
TakePhotosViewController.cs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using UIKit;
using ObjCRuntime;
using CoreGraphics;
using System;
using System.IO;
using Foundation;
namespace Puratap
{
public partial class TakePhotosViewController : UIViewController
{
private DetailedTabs _tabs;
private int _photosCounter;
private long _customerNumber;
public int PhotosCounter { get { return _photosCounter; } set { _photosCounter = value; } }
public long CustomerNumber { get { return _customerNumber; } set { _customerNumber = value; } }
UIImagePickerController _ipc;
Action<UIImagePickerController, NSDictionary> HandleFinishedPickingMedia;
Action<UIImagePickerController> HandleCanceledPickingMedia;
public TakePhotosViewController (DetailedTabs tabs) : base ("TakePhotosViewController", null)
{
this._tabs = tabs;
this.Title = NSBundle.MainBundle.LocalizedString ("Take photos", "Take photos");
using (var image = UIImage.FromBundle ("Images/86-camera") ) this.TabBarItem.Image = image;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
HandleFinishedPickingMedia = new Action<UIImagePickerController, NSDictionary> (
delegate(UIImagePickerController picker, NSDictionary info) {
UIImage im = (UIImage)info.ObjectForKey(UIImagePickerController.OriginalImage);
NSError err;
string path = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal),
String.Format ("{0}_{1}_{2}.jpg", _customerNumber.ToString (),
DateTime.Now.Date.ToString ("yyyy-MM-dd"),
_photosCounter.ToString ()));
_photosCounter++;
im = TakePhotosViewController.ScaleImage(im, 500);
im.AsJPEG ().Save (path, true, out err);
im.SaveToPhotosAlbum (null);
im.Dispose (); im = null;
// error handling here according to NSError err
if (err != null) {
using (var alert = new UIAlertView ("Error:" + err.LocalizedDescription, "Unable to save image to: " + path, null, "OK", null)) {
alert.Show ();
}
err.Dispose ();
err = null;
}
picker.DismissViewController (true, null); // _ipc.DismissModalViewControllerAnimated (true);
_tabs.SelectedViewController = _tabs.ViewControllers [_tabs.LastSelectedTab];
_tabs._jobRunTable.TableView.SelectRow (_tabs._jobRunTable.LastSelectedRowPath, true, UITableViewScrollPosition.None);
});
HandleCanceledPickingMedia = new Action<UIImagePickerController> (
delegate(UIImagePickerController picker) {
if (! (_tabs.ViewControllers[_tabs.LastSelectedTab] is TakePhotosViewController))
_tabs.SelectedViewController = _tabs.ViewControllers [_tabs.LastSelectedTab];
else
_tabs.SelectedViewController = _tabs.ViewControllers[0];
_tabs._jobRunTable.TableView.SelectRow (_tabs._jobRunTable.LastSelectedRowPath, true, UITableViewScrollPosition.None);
picker.DismissViewController(true, null);
});
}
public static UIImage ScaleImage(UIImage image, int maxSize)
{
UIImage result;
nint width, height;
using (CGImage imageRef = image.CGImage) {
CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo;
CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB();
if (alphaInfo == CGImageAlphaInfo.None) {
alphaInfo = CGImageAlphaInfo.NoneSkipLast;
}
width = imageRef.Width;
height = imageRef.Height;
if (height >= width) {
width = (int)Math.Floor((double)width * ((double)maxSize / (double)height));
height = maxSize;
} else {
height = (int)Math.Floor((double)height * ((double)maxSize / (double)width));
width = maxSize;
}
CGBitmapContext bitmap;
if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down) {
bitmap = new CGBitmapContext(IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
} else {
bitmap = new CGBitmapContext(IntPtr.Zero, height, width, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
}
switch (image.Orientation) {
case UIImageOrientation.Left:
bitmap.RotateCTM((float)Math.PI / 2);
bitmap.TranslateCTM(0, -height);
break;
case UIImageOrientation.Right:
bitmap.RotateCTM(-((float)Math.PI / 2));
bitmap.TranslateCTM(-width, 0);
break;
case UIImageOrientation.Up:
break;
case UIImageOrientation.Down:
bitmap.TranslateCTM(width, height);
bitmap.RotateCTM(-(float)Math.PI);
break;
}
bitmap.DrawImage(new CGRect(0, 0, width, height), imageRef);
result = UIImage.FromImage(bitmap.ToImage());
bitmap = null;
}
return result;
}
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
if (Runtime.Arch != Arch.SIMULATOR) {
_ipc = new UIImagePickerController ();
_ipc.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
_ipc.SourceType = UIImagePickerControllerSourceType.Camera;
_ipc.ShowsCameraControls = true;
_ipc.AllowsEditing = false;
_ipc.Delegate = new ImagePickerDelegate (HandleFinishedPickingMedia, HandleCanceledPickingMedia);
this.PresentViewController (_ipc, animated, null);
}
}
// [Obsolete]
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight);
}
}
public class ImagePickerDelegate : UIImagePickerControllerDelegate {
private readonly Action<UIImagePickerController, NSDictionary> _captureEvent;
private readonly Action<UIImagePickerController> _cancelEvent;
public ImagePickerDelegate(Action<UIImagePickerController, NSDictionary> captureEvent,
Action<UIImagePickerController>cancelEvent)
{
_captureEvent = captureEvent;
_cancelEvent = cancelEvent;
}
public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
{
_captureEvent(picker, info);
}
public override void Canceled (UIImagePickerController picker)
{
_cancelEvent (picker);
}
}
}