def initCamera(self): if self.config != None and self.config.cameraDisabled != 1: self.qcam = QCamera() self.qcam.locked.connect(self.snap) self.imgCapture = QCameraImageCapture(self.qcam) self.imgCapture.imageSaved.connect(self.pictureSaved) self.imgCapture.error.connect(self.error) self.autoShutOff = TorchAutoShutOff(self) self.autoShutOff.schedule(500)
class Camera(): def __init__(self): self.torchState = "off" def setConfig(self, config): self.config = config def initCamera(self): if self.config != None and self.config.cameraDisabled != 1: self.qcam = QCamera() self.qcam.locked.connect(self.snap) self.imgCapture = QCameraImageCapture(self.qcam) self.imgCapture.imageSaved.connect(self.pictureSaved) self.imgCapture.error.connect(self.error) self.autoShutOff = TorchAutoShutOff(self) self.autoShutOff.schedule(500) def onShutter(self): self.notify(self.config.quickSnapShutterSound, self.config.quickSnapShutterLedPattern) def onSave(self): self.notify(self.config.quickSnapSaveSound, self.config.quickSnapSaveLedPattern) def notify(self, snd, ledPattern): print snd try: if ledPattern != None: ledPattern.fire() if snd != None and len(snd) > 0: self.playSound(snd) except: pass def setFlashMode(self, mode): self.qcam.exposure().setFlashMode( { "auto": QCameraExposure.FlashAuto , "manual": QCameraExposure.FlashManual , "on": QCameraExposure.FlashOn , "off": QCameraExposure.FlashOff , "torch": QCameraExposure.FlashTorch }[mode]) def focusAndSnap(self, flashMode): if self.config != None and self.config.cameraDisabled != 1: self.qcam.setCaptureMode(QCamera.CaptureStillImage) self.qcam.start() self.setFlashMode(flashMode) self.qcam.searchAndLock() def snap(self): self.onShutter() self.imgCapture.capture(self.getPictureFile()) def pictureSaved(self, picId, picFilename): print >>sys.stderr, 'saved picture: ' + picFilename self.onSave() self.unloadCamera() def error(self, errId, err, errStr): print >>sys.stderr, "error: " + errStr self.unloadCamera() def unloadCamera(self): self.qcam.unlock() self.qcam.unload() def getPictureFile(self): millis = int(round(time.time() * 1000)) return "/home/user/MyDocs/DCIM/" + str(millis) + ".jpg" def playSound(self, snd): subprocess.Popen(["aplay", snd]) def torchToggle(self): if self.torchState == "on": self.torchOff() else: self.torchOn() def torchOn(self): if self.config != None and self.config.cameraDisabled != 1: print "torch on" self.qcam.setCaptureMode(QCamera.CaptureVideo) self.setFlashMode("torch") self.qcam.start() self.torchState = "on" if self.config != None and self.config.torchAutoShutOffTimeMs != None: self.autoShutOff.schedule(self.config.torchAutoShutOffTimeMs) def torchOff(self): if self.config != None and self.config.cameraDisabled != 1: self.autoShutOff.cancel() print "torch off" self.qcam.setCaptureMode(QCamera.CaptureStillImage) self.setFlashMode("manual") self.unloadCamera() self.torchState = "off"