Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use Camera, dimensions and others configurations.



  • Hello, I'm from Colombia I will explain my problem in English as well as possible, in the code some things are in Spanish, I hope you understand me, I'm trying to develop an app with Python and PyQt5, I found a Code where you can select a camera and take photos, delete it or save it. The code Now I have modified it in the part where you watch your camera space where you watch the photo you will take. as seen in this image:

    capturadora.PNG

    I don't understand what is this orange frame or square, I suppose that is the dimensions to the Camera, but when I try to change in the Code the dimensions that I think to be, yes change the size of this frame or square, but my problem is that I need it to be like the gray frame behind this. When I change this size I can decrease its width, but I cannot change its length so that it is the same as the back frame, this is what I need, the frame orange where the camera appears is equal to the gray frame.

    The CODE :

    from os import remove, getcwd
    
    from PyQt5.QtGui import QFont, QIcon, QPixmap
    from PyQt5.QtCore import Qt, QByteArray, QFile, QByteArray, QIODevice, QBuffer
    from PyQt5.QtMultimedia import QCamera, QCameraInfo, QCameraImageCapture
    from PyQt5.QtMultimediaWidgets import QCameraViewfinder, QVideoWidget
    from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget, QFileDialog,
                                 QAction, QStyle, QActionGroup, QFrame, QLabel, QStackedWidget,
                                 QMessageBox)
    
    
    
    
    
    # ======================== CLASE tomarFoto =========================
    
    class tomarFoto(QMainWindow):
        def __init__(self, parent = None):
            super(tomarFoto, self).__init__(parent)
    
            self.setWindowTitle("CAPTURADORA DE RADIOGRAFIAS ")
            self.setWindowIcon(QIcon("icono.png"))
            self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint |
                                Qt.MSWindowsFixedSizeDialogHint)
            self.setFixedSize(945, 620)
    
            self.initUI()
    
        def initUI(self):
    
          # ========================= MENÚ ===========================
            
            menu = self.menuBar()
            
            archivoMenu = menu.addMenu("&Archivo")
            self.accionIniciarCamara = archivoMenu.addAction(self.style().standardIcon(QStyle.SP_MediaPlay),
                                                             "Iniciar la camara", self.iniciarCamara)
            self.accionDetenerCamara = archivoMenu.addAction(self.style().standardIcon(QStyle.SP_MediaStop),
                                                             "Detener la camara", self.detenerCamara)
            archivoMenu.addAction(self.style().standardIcon(QStyle.SP_MessageBoxCritical), " Salir", self.close)
    
            dispositivosMenu = menu.addMenu("&Dispositivos")
    
            videoDevicesGroup = QActionGroup(self)
            videoDevicesGroup.setExclusive(True)
    
            dispositivoCamara = QByteArray()
    
            for nombreDispositivo in QCamera.availableDevices():
                descripcion = QCamera.deviceDescription(nombreDispositivo)
                videoDeviceAction = QAction(descripcion, videoDevicesGroup)
                videoDeviceAction.setCheckable(True)
                videoDeviceAction.setData(nombreDispositivo)
    
                if dispositivoCamara.isEmpty():
                    dispositivoCamara = nombreDispositivo
                    videoDeviceAction.setChecked(True)
    
                dispositivosMenu.addAction(videoDeviceAction)
    
            # Instancia del Widget central
            self.widgets = Widgets(dispositivoCamara, self)
    
            # Llamar función cuando se activa una Acción del Menú
            videoDevicesGroup.triggered.connect(self.widgets.actualizarDispositivoCamara)
    
            # Establecer el Widget central de la ventana
            self.setCentralWidget(self.widgets)
    
      # ======================= FUNCIONES ============================
    
        def iniciarCamara(self):
            self.widgets.iniciarCamara()
    
        def detenerCamara(self):
            self.widgets.detenerCamara()
    
    
    
    # ========================= CLASE Widgets ==========================
    
    class Widgets(QWidget):
        def __init__(self, dispositivoCamara, parent=None):
            super(Widgets, self).__init__(parent)
    
            self.parent = parent
    
            self.estadoFoto = False
            self.byteArrayFoto = QByteArray()
            
          # ==========================================================
    
            frame = QFrame(self)
            frame.setFrameShape(QFrame.Box)
            frame.setFrameShadow(QFrame.Sunken)
            frame.setFixedWidth(920)
            frame.setFixedHeight(380)
            frame.move(10, 10)
    
            # Instancias
            self.paginaVisor = QVideoWidget()
            self.paginaVisor.resize(935, 375)
           # THIS IS THE PART WHEN I CHANGE THE SIZE OF THE ORANGE FRAME# 
            self.visor = QCameraViewfinder(self.paginaVisor)
            self.visor.resize(900, 340)
           
    
            self.labelFoto = QLabel()
            self.labelFoto.setAlignment(Qt.AlignCenter)
            self.labelFoto.resize(915, 375)
    
            # QStackedWidget
            self.stackedWidget = QStackedWidget(frame)
            self.stackedWidget.addWidget(self.paginaVisor)
            self.stackedWidget.addWidget(self.labelFoto)
            self.stackedWidget.resize(915, 375)
            self.stackedWidget.move(2, 2)
    
          # ======================== BOTONES =========================
    
            self.buttonTomarFoto = QPushButton("Tomar foto", self)
            self.buttonTomarFoto.resize(110, 26)
            self.buttonTomarFoto.move(100, 525)
    
            self.buttonEliminarFoto = QPushButton("Eliminar foto", self)
            self.buttonEliminarFoto.resize(110, 26)
            self.buttonEliminarFoto.move(450, 525)
    
            self.buttonGuardarFoto = QPushButton("Guardar foto", self)
            self.buttonGuardarFoto.resize(110, 26)
            self.buttonGuardarFoto.move(800, 525)
    
          # ======================== EVENTOS =========================
            
            self.buttonTomarFoto.clicked.connect(self.tomarFoto)
            self.buttonEliminarFoto.clicked.connect(self.eliminarFoto)
            self.buttonGuardarFoto.clicked.connect(self.guardarFoto)
    
          # ================== FUNCIONES AUTOMÁTICAS =================
          
            self.setCamara(dispositivoCamara)
    
      # ======================= FUNCIONES ============================
    
        def setCamara(self, dispositivoCamara):
            if dispositivoCamara.isEmpty():
                self.camara = QCamera()
            else:
                self.camara = QCamera(dispositivoCamara)
    
            self.camara.stateChanged.connect(self.actualizarEstadoCamara)
    
            self.capturaImagen = QCameraImageCapture(self.camara)
    
            self.camara.setViewfinder(self.visor)
    
            self.actualizarEstadoCamara(self.camara.state())
            
            self.capturaImagen.imageCaptured.connect(self.procesarImagenCapturada)
            self.capturaImagen.imageSaved.connect(self.imagenGuardada)
    
            self.camara.isCaptureModeSupported(QCamera.CaptureStillImage)
    
            self.camara.start()
    
            self.paginaVisor.update()
    
        def actualizarDispositivoCamara(self, action):
            self.setCamara(action.data())
    
        def actualizarEstadoCamara(self, estado):
            if estado == QCamera.ActiveState:
                self.parent.accionIniciarCamara.setEnabled(False)
                self.parent.accionDetenerCamara.setEnabled(True)
    
                if not self.estadoFoto:
                    self.buttonTomarFoto.setEnabled(True)
                    self.buttonEliminarFoto.setEnabled(False)
                    self.buttonGuardarFoto.setEnabled(False)
            elif estado in (QCamera.UnloadedState, QCamera.LoadedState):
                self.parent.accionIniciarCamara.setEnabled(True)
                self.parent.accionDetenerCamara.setEnabled(False)
    
                if not self.estadoFoto:
                    self.buttonTomarFoto.setEnabled(False)
                    self.buttonEliminarFoto.setEnabled(False)
                    self.buttonGuardarFoto.setEnabled(False)
    
        def iniciarCamara(self):
            self.camara.start()
    
        def detenerCamara(self):
            self.camara.stop()
    
        def tomarFoto(self):
            rutaFoto = "{}/fotoTemporal.jpg".format(getcwd())
            self.capturaImagen.capture(rutaFoto)
    
            self.estadoFoto = True
    
        def procesarImagenCapturada(self, requestId, imagen):
            foto = QPixmap.fromImage(imagen)
            
            buffer = QBuffer(self.byteArrayFoto)
            buffer.open(QIODevice.WriteOnly)
            buffer.close()
            foto.save(buffer, "PNG")
                
            fotoEscalada = foto.scaled(self.labelFoto.size())
    
            self.labelFoto.setPixmap(fotoEscalada)
            self.mostrarImagenCapturada()
    
        def visualizarVisor(self):
            self.stackedWidget.setCurrentIndex(0)
    
        def mostrarImagenCapturada(self):
            self.stackedWidget.setCurrentIndex(1)
    
            self.buttonTomarFoto.setEnabled(False)
            self.buttonEliminarFoto.setEnabled(True)
            self.buttonGuardarFoto.setEnabled(True)
    
        def imagenGuardada(self, id, nombreFoto):
            if QFile.exists(nombreFoto):
                remove(nombreFoto)
    
        def eliminarFoto(self):
            self.estadoFoto = False
            self.byteArrayFoto.clear()
            
            self.labelFoto.clear()
    
            self.actualizarEstadoCamara(self.camara.state())
            self.visualizarVisor()
    
        def guardarFoto(self):
            guardarComo, extension = QFileDialog.getSaveFileName(self, "Guardar como", "Foto",
                                                                 "JPG (*.jpg);;PNG (*.png);;ICO (*.ico);;BMP (*.bmp)",
                                                                 options=QFileDialog.Options())
                    
            if guardarComo:
                foto = QPixmap()
                foto.loadFromData(self.byteArrayFoto, "PNG")
                foto.save(guardarComo, quality = 100)
    
                QMessageBox.information(self, "Guardar foto",
                                        "Foto guardada con éxito                                 ")
    
                self.eliminarFoto()
    
    if __name__ == '__main__':
    
        import sys
        
        aplicacion = QApplication(sys.argv)
    
        fuente = QFont("Bahnschrift Light")
        fuente.setPointSize(10)
        
        aplicacion.setFont(fuente)
    
        ventana = tomarFoto()
        ventana.show()
    
        sys.exit(aplicacion.exec_())
    
    

    Maybe, the size doesn't increase due to the camera
    Please, I know that the code is some large, but I need to solve this problem


Log in to reply