2018-02-09 23:10:03 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2024-02-19 13:36:32 +01:00
|
|
|
from PyQt5.QtCore import QSize
|
|
|
|
from PyQt5.QtGui import QIcon
|
|
|
|
from PyQt5.QtMultimedia import QCamera, QCameraImageCapture, QCameraInfo
|
|
|
|
from PyQt5.QtMultimediaWidgets import QCameraViewfinder
|
|
|
|
from PyQt5.QtWidgets import (
|
|
|
|
QAction,
|
|
|
|
QApplication,
|
|
|
|
QComboBox,
|
|
|
|
QErrorMessage,
|
|
|
|
QFileDialog,
|
|
|
|
QMainWindow,
|
|
|
|
QStatusBar,
|
|
|
|
QToolBar,
|
|
|
|
)
|
2018-02-09 23:10:03 +01:00
|
|
|
|
|
|
|
|
2024-02-19 13:36:32 +01:00
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2018-02-09 23:10:03 +01:00
|
|
|
|
|
|
|
self.available_cameras = QCameraInfo.availableCameras()
|
|
|
|
if not self.available_cameras:
|
2024-02-19 13:36:32 +01:00
|
|
|
pass # quit
|
2018-02-09 23:10:03 +01:00
|
|
|
|
|
|
|
self.status = QStatusBar()
|
|
|
|
self.setStatusBar(self.status)
|
|
|
|
|
|
|
|
self.save_path = ""
|
|
|
|
|
|
|
|
self.viewfinder = QCameraViewfinder()
|
|
|
|
self.viewfinder.show()
|
|
|
|
self.setCentralWidget(self.viewfinder)
|
|
|
|
|
|
|
|
# Set the default camera.
|
|
|
|
self.select_camera(0)
|
|
|
|
|
|
|
|
# Setup tools
|
|
|
|
camera_toolbar = QToolBar("Camera")
|
|
|
|
camera_toolbar.setIconSize(QSize(14, 14))
|
|
|
|
self.addToolBar(camera_toolbar)
|
|
|
|
|
2024-02-19 13:36:32 +01:00
|
|
|
photo_action = QAction(
|
|
|
|
QIcon(os.path.join("images", "camera-black.png")),
|
|
|
|
"Take photo...",
|
|
|
|
self,
|
|
|
|
)
|
2018-02-09 23:10:03 +01:00
|
|
|
photo_action.setStatusTip("Take photo of current view")
|
|
|
|
photo_action.triggered.connect(self.take_photo)
|
|
|
|
camera_toolbar.addAction(photo_action)
|
|
|
|
|
2024-02-19 13:36:32 +01:00
|
|
|
change_folder_action = QAction(
|
|
|
|
QIcon(os.path.join("images", "blue-folder-horizontal-open.png")),
|
|
|
|
"Change save location...",
|
|
|
|
self,
|
|
|
|
)
|
2018-02-09 23:10:03 +01:00
|
|
|
change_folder_action.setStatusTip("Change folder where photos are saved.")
|
|
|
|
change_folder_action.triggered.connect(self.change_folder)
|
|
|
|
camera_toolbar.addAction(change_folder_action)
|
|
|
|
|
|
|
|
camera_selector = QComboBox()
|
|
|
|
camera_selector.addItems([c.description() for c in self.available_cameras])
|
2024-02-19 13:36:32 +01:00
|
|
|
camera_selector.currentIndexChanged.connect(self.select_camera)
|
2018-02-09 23:10:03 +01:00
|
|
|
|
|
|
|
camera_toolbar.addWidget(camera_selector)
|
|
|
|
|
|
|
|
self.setWindowTitle("NSAViewer")
|
|
|
|
self.show()
|
|
|
|
|
|
|
|
def select_camera(self, i):
|
|
|
|
self.camera = QCamera(self.available_cameras[i])
|
|
|
|
self.camera.setViewfinder(self.viewfinder)
|
|
|
|
self.camera.setCaptureMode(QCamera.CaptureStillImage)
|
|
|
|
self.camera.error.connect(lambda: self.alert(self.camera.errorString()))
|
|
|
|
self.camera.start()
|
|
|
|
|
|
|
|
self.capture = QCameraImageCapture(self.camera)
|
|
|
|
self.capture.error.connect(lambda i, e, s: self.alert(s))
|
|
|
|
self.capture.imageCaptured.connect(lambda d, i: self.status.showMessage("Image %04d captured" % self.save_seq))
|
|
|
|
|
|
|
|
self.current_camera_name = self.available_cameras[i].description()
|
|
|
|
self.save_seq = 0
|
|
|
|
|
|
|
|
def take_photo(self):
|
|
|
|
timestamp = time.strftime("%d-%b-%Y-%H_%M_%S")
|
2024-02-19 13:36:32 +01:00
|
|
|
self.capture.capture(
|
|
|
|
os.path.join(
|
|
|
|
self.save_path,
|
|
|
|
"%s-%04d-%s.jpg" % (self.current_camera_name, self.save_seq, timestamp),
|
|
|
|
)
|
|
|
|
)
|
2018-02-09 23:10:03 +01:00
|
|
|
self.save_seq += 1
|
|
|
|
|
|
|
|
def change_folder(self):
|
|
|
|
path = QFileDialog.getExistingDirectory(self, "Snapshot save location", "")
|
|
|
|
if path:
|
|
|
|
self.save_path = path
|
|
|
|
self.save_seq = 0
|
|
|
|
|
|
|
|
def alert(self, s):
|
|
|
|
"""
|
|
|
|
Handle errors coming from QCamera dn QCameraImageCapture by displaying alerts.
|
|
|
|
"""
|
|
|
|
err = QErrorMessage(self)
|
|
|
|
err.showMessage(s)
|
|
|
|
|
|
|
|
|
2024-02-19 13:36:32 +01:00
|
|
|
if __name__ == "__main__":
|
2018-02-09 23:10:03 +01:00
|
|
|
app = QApplication(sys.argv)
|
|
|
|
app.setApplicationName("NSAViewer")
|
|
|
|
|
|
|
|
window = MainWindow()
|
2024-02-19 13:36:32 +01:00
|
|
|
app.exec_()
|