-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoPicker.py
570 lines (476 loc) · 21.4 KB
/
PhotoPicker.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from AppKit import (NSApp,
NSCancelButton,
NSCompositeCopy,
NSEvenOddWindingRule,
NSFrameRect,
NSPNGFileType,
NSRunAlertPanel,
NSModalPanelRunLoopMode,
NSOKButton,
NSOnState,
NSSound)
from Foundation import (CIImage,
NSArray,
NSBitmapImageRep,
NSBezierPath,
NSBox,
NSBundle,
NSCIImageRep,
NSCollectionView,
NSColor,
NSDefaultRunLoopMode,
NSDictionary,
NSHeight,
NSImage,
NSImageView,
NSIndexSet,
NSLock,
NSMakeRect,
NSMakeSize,
NSMaxX,
NSMaxY,
NSMinX,
NSMinY,
NSMutableArray,
NSObject,
NSOpenPanel,
NSRunLoop,
NSTimer,
NSLocalizedString,
NSWidth,
NSZeroRect)
import objc
import QTKit
import os
import datetime
import hashlib
import unicodedata
from application.system import makedirs
from sipsimple.configuration.settings import SIPSimpleSettings
from resources import ApplicationData
class IconViewBox(NSBox):
def hitTest_(self, p):
return None
class MyCollectionView(NSCollectionView):
arrayController = objc.IBOutlet()
def deleteBackward_(self, sender):
settings = SIPSimpleSettings()
own_icon_path = settings.presence_state.icon
selection = self.arrayController.selectedObjects()
if selection.count() > 0:
obj = selection.lastObject()
path = obj.objectForKey_("path")
if own_icon_path is not None and path == unicode(own_icon_path):
return
if path.endswith("default_user_icon.tiff"):
return
os.remove(path)
self.arrayController.removeObject_(obj)
class EditImageView(NSImageView):
cropRectangle = NSMakeRect(0, 0, 220, 220)
dragPos = None
def setCropSize_(self, size=None):
if size is None:
self.cropRectangle = self.frame()
elif size == 'default':
self.cropRectangle = NSMakeRect(0, 0, 220, 220)
else:
self.cropRectangle = NSMakeRect(0, 0, size, size)
self.cropRectangle.origin.x = 0
self.cropRectangle.origin.y = 0
self.setNeedsDisplay_(True)
def getCropped(self):
image = self.image()
cropped = NSImage.alloc().initWithSize_(self.cropRectangle.size)
cropped.lockFocus()
image.drawInRect_fromRect_operation_fraction_(NSMakeRect(0, 0, NSWidth(self.cropRectangle), NSHeight(self.cropRectangle)),
self.cropRectangle, NSCompositeCopy, 1.0)
cropped.unlockFocus()
return cropped
def mouseDown_(self, event):
if self.cropRectangle:
p = self.convertPoint_fromView_(event.locationInWindow(), None)
if p.x > NSMinX(self.cropRectangle) and p.x < NSMaxX(self.cropRectangle) and\
p.y > NSMinY(self.cropRectangle) and p.y < NSMaxY(self.cropRectangle):
self.dragPos = p
self.initialPos = self.cropRectangle.origin
def mouseUp_(self, event):
self.dragPos = None
self.setNeedsDisplay_(True)
def mouseDragged_(self, event):
if self.cropRectangle and self.dragPos:
p = self.convertPoint_fromView_(event.locationInWindow(), None)
dx = self.dragPos.x - p.x
dy = self.dragPos.y - p.y
newRect = NSMakeRect(self.initialPos.x - dx, self.initialPos.y - dy,
NSWidth(self.cropRectangle), NSHeight(self.cropRectangle))
if NSMinX(newRect) < 0:
newRect.origin.x = 0
if NSMinY(newRect) < 0:
newRect.origin.y = 0
if NSMaxX(newRect) > NSWidth(self.frame()):
newRect.origin.x = NSWidth(self.frame()) - NSWidth(newRect)
if NSMaxY(newRect) > NSHeight(self.frame()):
newRect.origin.y = NSHeight(self.frame()) - NSHeight(newRect)
self.cropRectangle = newRect
self.setNeedsDisplay_(True)
def drawRect_(self, rect):
NSImageView.drawRect_(self, rect)
if self.cropRectangle:
rect = NSZeroRect
rect.size = self.frame().size
NSColor.whiteColor().set()
NSFrameRect(self.cropRectangle)
clip = NSBezierPath.bezierPathWithRect_(rect)
clip.setWindingRule_(NSEvenOddWindingRule)
clip.appendBezierPathWithRect_(self.cropRectangle)
clip.addClip()
NSColor.blackColor().colorWithAlphaComponent_(0.6).set()
NSBezierPath.bezierPathWithRect_(rect).fill()
class PhotoPicker(NSObject):
latestImageRep = None
lock = None
window = objc.IBOutlet()
tabView = objc.IBOutlet()
photoView = objc.IBOutlet()
previewButton = objc.IBOutlet()
captureButton = objc.IBOutlet()
cropButton = objc.IBOutlet()
captureView = objc.IBOutlet()
useButton = objc.IBOutlet()
cameraTabView = objc.IBOutlet()
historyTabView = objc.IBOutlet()
countdownCheckbox = objc.IBOutlet()
countdownProgress = objc.IBOutlet()
browseView = objc.IBOutlet()
cropWindow = objc.IBOutlet()
cropWindowImage = objc.IBOutlet()
cropOriginalImage = None
cropScaleSlider = objc.IBOutlet()
libraryCollectionView = objc.IBOutlet()
contentArrayController = objc.IBOutlet()
captureDecompressedVideoOutput = None
captureSession = None
captureDeviceInput = None
capture_session_initialized = False
countdown_counter = 10
timer = None
def __new__(cls, *args, **kwargs):
return cls.alloc().init()
def __init__(self, storage_folder=ApplicationData.get('photos'), high_res=False, history=True):
self.history = history
NSBundle.loadNibNamed_owner_("PhotoPicker", self)
self.lock = NSLock.alloc().init()
self.captureButton.setHidden_(True)
self.previewButton.setHidden_(False)
self.countdownCheckbox.setHidden_(True)
self.storage_folder = storage_folder
self.high_res = high_res
if self.high_res:
self.photoView.setCropSize_()
if not self.history:
self.tabView.selectTabViewItem_(self.cameraTabView)
self.previewButton.setHidden_(True)
self.countdownCheckbox.setHidden_(False)
self.captureButton.setHidden_(False)
def awakeFromNib(self):
if not self.history:
self.tabView.removeTabViewItem_(self.historyTabView)
def initAquisition(self):
if self.capture_session_initialized:
return
if self.captureSession is None:
self.captureSession = QTKit.QTCaptureSession.alloc().init()
# Find a video device
device = QTKit.QTCaptureDevice.defaultInputDeviceWithMediaType_(QTKit.QTMediaTypeVideo)
if not device:
NSRunAlertPanel(NSLocalizedString("Camera Capture Error", "Window title"), NSLocalizedString("Camera device cannot be started", "Label"), NSLocalizedString("OK", "Button title"), "", "")
self.captureSession = None
return
success, error = device.open_(None)
if not success:
NSRunAlertPanel(NSLocalizedString("Camera Capture Error", "Window title"), error, NSLocalizedString("OK", "Button title"), "", "")
self.captureSession = None
return
# Add a device input for that device to the capture session
if self.captureDeviceInput is None:
self.captureDeviceInput = QTKit.QTCaptureDeviceInput.alloc().initWithDevice_(device)
success, error = self.captureSession.addInput_error_(self.captureDeviceInput, None)
if not success:
NSRunAlertPanel(NSLocalizedString("Camera Capture Error", "Window title"), error, NSLocalizedString("OK", "Button title"), "", "")
self.captureSession = None
self.captureDeviceInput = None
return
# Add a decompressed video output that returns raw frames to the session
if self.captureDecompressedVideoOutput is None:
self.captureDecompressedVideoOutput = QTKit.QTCaptureDecompressedVideoOutput.alloc().init()
self.captureDecompressedVideoOutput.setDelegate_(self)
success, error = self.captureSession.addOutput_error_(self.captureDecompressedVideoOutput, None)
if not success:
NSRunAlertPanel(NSLocalizedString("Camera Capture Error", "Window title"), error, NSLocalizedString("OK", "Button title"), "", "")
self.captureSession = None
self.captureDeviceInput = None
self.captureDecompressedVideoOutput = None
return
# Preview the video from the session in the document window
self.captureView.setCaptureSession_(self.captureSession)
self.capture_session_initialized = True
def refreshLibrary(self):
if not self.history:
return
settings = SIPSimpleSettings()
own_icon_path = settings.presence_state.icon
selected_icon = None
def md5sum(filename):
md5 = hashlib.md5()
with open(filename,'rb') as f:
for chunk in iter(lambda: f.read(128*md5.block_size), b''):
md5.update(chunk)
return md5.hexdigest()
if os.path.exists(self.storage_folder):
files = os.listdir(self.storage_folder)
else:
files = []
array = NSMutableArray.array()
knownFiles = set()
for item in self.contentArrayController.arrangedObjects():
knownFiles.add(unicode(item.objectForKey_("path")))
seen_md5sum = {}
i = 0
for f in files:
if not f.startswith('user_icon') and not f.startswith('photo') and f != 'default_user_icon.tiff':
continue
p = os.path.normpath(self.storage_folder + "/" + f)
if p not in knownFiles:
photos_folder = unicodedata.normalize('NFC', self.storage_folder)
filename = os.path.join(photos_folder, f)
checksum = md5sum(filename)
try:
seen_md5sum[filename]
except KeyError:
seen_md5sum[filename] = checksum
image = NSImage.alloc().initWithContentsOfFile_(p)
if not image:
continue
item = NSDictionary.dictionaryWithObjectsAndKeys_(image, "picture", p, "path")
array.addObject_(item)
if own_icon_path is not None and filename == unicode(own_icon_path):
selected_icon = i
i += 1
if array.count() > 0:
self.contentArrayController.addObjects_(array)
if selected_icon is not None:
self.libraryCollectionView.setSelectionIndexes_(NSIndexSet.indexSetWithIndex_(selected_icon))
def tabView_didSelectTabViewItem_(self, tabView, item):
if item.identifier() == "recent":
if self.captureSession is not None:
self.captureSession.stopRunning()
else:
self.initAquisition()
if self.captureSession is not None:
self.captureSession.startRunning()
self.photoView.setHidden_(True)
self.captureView.setHidden_(False)
self.previewButton.setHidden_(True)
self.countdownCheckbox.setHidden_(False)
self.captureButton.setHidden_(False)
self.useButton.setEnabled_(False)
def captureOutput_didOutputVideoFrame_withSampleBuffer_fromConnection_(self, captureOutput, videoFrame, sampleBuffer, connection):
self.latestImageRep = NSCIImageRep.imageRepWithCIImage_(CIImage.imageWithCVImageBuffer_(videoFrame))
@objc.IBAction
def previewButtonClicked_(self, sender):
self.photoView.setHidden_(True)
self.captureView.setHidden_(False)
if self.captureSession is not None:
self.captureSession.startRunning()
self.previewButton.setHidden_(True)
self.countdownCheckbox.setHidden_(False)
self.captureButton.setHidden_(False)
self.useButton.setEnabled_(False)
@objc.IBAction
def captureButtonClicked_(self, sender):
if self.countdownCheckbox.state() == NSOnState:
self.countdown_counter = 10
self.previewButton.setHidden_(True)
self.captureButton.setHidden_(True)
self.countdownCheckbox.setHidden_(True)
self.countdownProgress.setHidden_(False)
self.countdownProgress.startAnimation_(None)
self.countdownProgress.setIndeterminate_(False)
self.countdownProgress.setDoubleValue_(self.countdown_counter)
self.timer = NSTimer.timerWithTimeInterval_target_selector_userInfo_repeats_(1, self, "executeTimerCapture:", None, True)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSModalPanelRunLoopMode)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSDefaultRunLoopMode)
else:
self.countdownCheckbox.setHidden_(True)
self.countdownProgress.setHidden_(True)
self.executeCapture()
def executeTimerCapture_(self, timer):
if self.countdown_counter == 1:
self.executeCapture()
self.countdownProgress.stopAnimation_(None)
self.countdownCheckbox.setHidden_(True)
self.countdownProgress.setHidden_(True)
self.timer.invalidate()
self.timer = None
else:
self.countdown_counter = self.countdown_counter - 1
NSSound.soundNamed_("Tink").play()
self.countdownProgress.setDoubleValue_(self.countdown_counter)
def executeCapture(self):
self.photoView.setHidden_(False)
self.captureView.setHidden_(True)
self.previewButton.setHidden_(False)
self.countdownCheckbox.setHidden_(True)
self.captureButton.setHidden_(True)
self.useButton.setEnabled_(True)
if self.captureSession is not None:
self.captureImage()
NSSound.soundNamed_("Grab").play()
self.captureSession.stopRunning()
@objc.IBAction
def cropWindowButtonClicked_(self, sender):
if sender.tag() == 1: # cancel
NSApp.stopModalWithCode_(NSCancelButton)
elif sender.tag() == 2: # crop
NSApp.stopModalWithCode_(NSOKButton)
@objc.IBAction
def changeCropScale_(self, sender):
scale = sender.doubleValue()
size = self.cropOriginalImage.size()
size.width = size.width * scale / 100.0
size.height = size.height * scale / 100.0
scaled = self.cropOriginalImage.copy()
scaled.setScalesWhenResized_(True)
scaled.setSize_(size)
self.cropWindowImage.setImage_(scaled)
frame = NSZeroRect.copy()
frame.size = size
self.cropWindowImage.setFrame_(frame)
def captureImage(self):
if self.latestImageRep:
imageRep = self.latestImageRep
image = NSImage.alloc().initWithSize_(imageRep.size())
image.addRepresentation_(imageRep)
image.setScalesWhenResized_(True)
h = self.photoView.frame().size.height
w = h * imageRep.size().width/imageRep.size().height
image.setSize_(NSMakeSize(w, h))
self.photoView.setImage_(image)
def storeCaptured(self):
makedirs(self.storage_folder)
dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
if not self.photoView.image():
self.captureImage()
if self.high_res:
imageRep = self.latestImageRep
image = NSImage.alloc().initWithSize_(imageRep.size())
image.addRepresentation_(imageRep)
else:
image = self.photoView.getCropped()
tiff_data = image.TIFFRepresentation()
path = self.storage_folder + "/photo%s.png" % dt
bitmap_data = NSBitmapImageRep.alloc().initWithData_(tiff_data)
png_data = bitmap_data.representationUsingType_properties_(NSPNGFileType, None)
data = png_data.bytes().tobytes()
with open(path, 'w') as f:
f.write(data)
self.refreshLibrary()
return path, image
def cropAndAddImage(self, path):
try:
image = NSImage.alloc().initWithContentsOfFile_(path)
except:
NSRunAlertPanel(NSLocalizedString("Camera Capture Error", "Window title"), NSLocalizedString("%s is not a valid image", "Label") % path, NSLocalizedString("OK", "Button title"), None, None)
return
rect = NSZeroRect.copy()
rect.size = image.size()
curSize = self.cropWindow.frame().size
if rect.size.width > curSize.width or rect.size.height > curSize.height:
self.cropWindowImage.setFrame_(rect)
self.cropOriginalImage = image.copy()
self.cropWindowImage.setImage_(image)
if NSApp.runModalForWindow_(self.cropWindow) == NSOKButton:
dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
image = self.cropWindowImage.getCropped()
tiff_data = image.TIFFRepresentation()
#path = self.storage_folder + "/photo%s.tiff" % dt
#tiff_data.writeToFile_atomically_(path, False)
path = self.storage_folder + "/photo%s.png" % dt
bitmap_data = NSBitmapImageRep.alloc().initWithData_(tiff_data)
png_data = bitmap_data.representationUsingType_properties_(NSPNGFileType, None)
data = png_data.bytes().tobytes()
with open(path, 'w') as f:
f.write(data)
self.cropWindow.orderOut_(None)
self.refreshLibrary()
else:
self.cropWindow.orderOut_(None)
#self.addImageFile(path)
def addImageFile(self, path):
path = os.path.normpath(path)
if os.path.dirname(path) != self.storage_folder:
# scale and copy the image to our photo dir
try:
image = NSImage.alloc().initWithContentsOfFile_(path)
except:
NSRunAlertPanel(NSLocalizedString("Camera Capture Error", "Window title"), NSLocalizedString("%s is not a valid image", "Label") % path, NSLocalizedString("OK", "Button title"), None, None)
return
size = image.size()
if size.width > 128 or size.height > 128:
image.setScalesWhenResized_(True)
image.setSize_(NSMakeSize(128, 128 * size.height/size.width))
finalpath = self.storage_folder + "/" + os.path.basename(path)
prefix, ext = os.path.splitext(finalpath)
i= 0
while os.path.exists(finalpath):
finalpath = prefix+str(i)+ext
image.TIFFRepresentation().writeToFile_atomically_(finalpath, False)
self.refreshLibrary()
@objc.IBAction
def browseFile_(self, sender):
panel = NSOpenPanel.openPanel()
panel.setTitle_(NSLocalizedString("Select a Picture", "Label"))
if panel.runModalForTypes_(NSArray.arrayWithObjects_("png", "tiff", "jpeg", "jpg", "tif")) == NSOKButton:
path = unicode(panel.filename())
self.cropAndAddImage(path)
@objc.IBAction
def UseButtonClicked_(self, sender):
self.window.close()
NSApp.stopModalWithCode_(1)
@objc.IBAction
def CancelButtonClicked_(self, sender):
if self.timer is not None and self.timer.isValid():
self.timer.invalidate()
self.timer = None
self.window.close()
NSApp.stopModalWithCode_(0)
def windowWillClose_(self, notification):
if self.captureDecompressedVideoOutput != None:
self.captureDecompressedVideoOutput.setDelegate_(None)
self.captureDecompressedVideoOutput = None
if self.captureSession is not None:
self.captureSession.stopRunning()
self.captureSession = None
if self.captureDeviceInput:
device = self.captureDeviceInput.device()
if device.isOpen():
device.close()
self.captureDeviceInput = None
NSApp.stopModalWithCode_(0)
def runModal(self):
self.window.makeKeyAndOrderFront_(None)
self.refreshLibrary()
result = NSApp.runModalForWindow_(self.window)
if result:
if self.tabView.selectedTabViewItem().identifier() == "recent":
selected = self.contentArrayController.selectedObjects()
if selected.count() > 0:
path = selected.lastObject().objectForKey_("path")
image = selected.lastObject().objectForKey_("picture")
return path, image
else:
return self.storeCaptured()
return None, None