import config
import mediafiles
-import mjpgclient
import motionctl
import remote
-import settings
import template
import update
import v4l2ctl
camera_config = config.get_camera(camera_id)
if camera_config['@proto'] == 'v4l2':
- jpg = mjpgclient.get_jpg(camera_id)
- if jpg is None:
- return self.finish()
-
- self.finish(jpg)
+ picture = mediafiles.get_current_picture(camera_config,
+ width=self.get_argument('width', None),
+ height=self.get_argument('height', None))
+
+ self.finish(picture)
else:
- def on_response(jpg):
- if jpg is None:
- self.finish()
-
- else:
- self.finish(jpg)
+ def on_response(picture):
+ self.finish(picture)
- remote.current_picture(
+ remote.get_current_picture(
camera_config['@host'],
camera_config['@port'],
camera_config['@username'],
camera_config['@password'],
- camera_config['@remote_camera_id'], on_response)
-
+ camera_config['@remote_camera_id'],
+ on_response,
+ width=self.get_argument('width', None),
+ height=self.get_argument('height', None))
+
@BaseHandler.auth()
def list(self, camera_id):
logging.debug('listing pictures for camera %(id)s' % {'id': camera_id})
from PIL import Image
import config
+import mjpgclient
import utils
image.save(sio, format='JPEG')
return sio.getvalue()
+
+
+def get_current_picture(camera_config, width, height):
+ jpg = mjpgclient.get_jpg(camera_config['@id'])
+
+ if jpg is None:
+ return None
+
+ if width is height is None:
+ return jpg
+
+ sio = StringIO.StringIO(jpg)
+ image = Image.open(sio)
+
+ width = width and int(width) or image.size[0]
+ height = height and int(height) or image.size[1]
+
+ if width >= image.size[0] and height >= image.size[1]:
+ return jpg # no enlarging of the picture on the server side
+
+ image.thumbnail((width, height), Image.ANTIALIAS)
+
+ sio = StringIO.StringIO()
+ image.save(sio, format='JPEG')
+
+ return sio.getvalue()
except KeyError:
pass
- iostream.IOStream.close(self)
+ try:
+ iostream.IOStream.close(self)
+
+ except:
+ pass # already closed, nevermind
def _check_error(self):
if self.error is None:
http_client.fetch(request, on_response)
-def current_picture(host, port, username, password, camera_id, callback):
+def get_current_picture(host, port, username, password, camera_id, callback, width, height):
global _snapshot_cache
logging.debug('getting current picture for remote camera %(id)s on %(host)s:%(port)s' % {
'host': host,
'port': port})
- request = _make_request(host, port, username, password, '/picture/%(id)s/current/' % {'id': camera_id})
+ query = {}
+
+ if width:
+ query['width'] = str(width)
+
+ if height:
+ query['height'] = str(height)
+
+ request = _make_request(host, port, username, password, '/picture/%(id)s/current/' % {'id': camera_id}, query=query)
- cached = _snapshot_cache.setdefault(request.url, {'pending': 0, 'jpg': None})
+ cache_key = (host, port, camera_id)
+ cached = _snapshot_cache.setdefault(cache_key, {'pending': 0, 'jpg': None})
if cached['pending'] > 0: # a pending request for this snapshot exists
return callback(cached['jpg'])
def on_response(response):
cached['pending'] -= 1
- cached['jpg'] = response.body
+ cached['picture'] = response.body
if response.error:
logging.error('failed to get current picture for remote camera %(id)s on %(host)s:%(port)s: %(msg)s' % {
timestamp /= 500;
}
timestamp = Math.round(timestamp);
- img.src = '/picture/' + cameraId + '/current/?_=' + timestamp;
+ img.src = '/picture/' + cameraId + '/current/?_=' + timestamp + '&width=' + img.width;
img.loading = true;
}