I have a button in a Plugin to take a photo and relate it with a feature of a layer in QGIS.
My aim is to select a feature, open a camera view (viewfinder), take a photo and save it with the feature id and datetime as file name (e.g. 20210412-112351_223.jpg).
To do that I use the code below based on this post.
The problem is when I use this Plugin in my laptop the picture taken is 1280x720 but with a Surface Book 3 the result picture is 640x320.
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
from qgis.core import (QgsProject,
Qgis,
QgsExpressionContextUtils)
from qgis.utils import iface
import os
import time
class creaventana(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 1280, 720)
self.setStyleSheet("background : lightgrey;")
self.available_cameras = QCameraInfo.availableCameras()
if not self.available_cameras:
iface.messageBar().pushMessage('No hay cámaras', 'El dispositivo no tiene ninguna cámara',
level=Qgis.Warning,
duration=5)
self.status = QStatusBar()
self.status.setStyleSheet("background : white;")
self.setStatusBar(self.status)
self.viewfinder = QCameraViewfinder()
self.viewfinder.show()
self.setCentralWidget(self.viewfinder)
self.select_camera(0)
toolbar = QToolBar("Cámara")
self.addToolBar(toolbar)
click_action = QAction("Hacer foto", self)
click_action.setStatusTip("Esto hace la foto")
click_action.setToolTip("Hacer foto")
click_action.triggered.connect(self.click_photo)
toolbar.addAction(click_action)
camera_selector = QComboBox()
camera_selector.setStatusTip("Cámara frontal / trasera")
camera_selector.setToolTip("Elegir cámara")
camera_selector.setToolTipDuration(2500)
camera_selector.addItems([camera.description()
for camera in self.available_cameras])
camera_selector.currentIndexChanged.connect(self.select_camera)
toolbar.addWidget(camera_selector)
toolbar.setStyleSheet("background : white;")
self.setWindowTitle("PyQt5 Cam")
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 error_msg, error,
msg: self.alert(msg))
self.capture.imageCaptured.connect(lambda d, i: self.status.showMessage("Imagen tomada: " + str(self.save_seq)))
self.current_camera_name = self.available_cameras[i].description()
self.save_seq = 0
def click_photo(self):
individuos = QgsProject.instance().mapLayersByName('Individuos')[0]
individuoSeleccionado = individuos.selectedFeatures()
for individuo in individuoSeleccionado:
idind = individuo['id_ind']
timestamp = time.strftime('%Y%m%d-%H%M%S')
ruta = 'D:/fotos/'
self.capture.capture(os.path.join(ruta, "%s_%s.jpg" % (idind, timestamp)))
self.save_seq += 1
def alert(self, msg):
error = QErrorMessage(self)
error.showMessage(msg)
def abrecamara(self):
individuos = QgsProject.instance().mapLayersByName('Individuos')[0]
individuoSeleccionado = individuos.selectedFeatures()
cuentaIndividuoSeleccionado = len(list(individuoSeleccionado))
if cuentaIndividuoSeleccionado == 1:
ventana = creaventana()
ventana.show()
elif cuentaIndividuoSeleccionado > 1:
iface.messageBar().pushMessage('Selección', 'Solo puede haber un individuo seleccionado',
level=Qgis.Warning,
duration=5)
elif cuentaIndividuoSeleccionado == 0:
iface.messageBar().pushMessage('Selección', 'Tiene que haber al menos un individuo seleccionado',
level=Qgis.Warning,
duration=5)
from Taking picture from QGIS Plugin using PyQt
No comments:
Post a Comment