首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用QCamera在PyQt5中单击照片?

如何使用QCamera在PyQt5中单击照片?
EN

Stack Overflow用户
提问于 2021-10-04 14:28:25
回答 1查看 533关注 0票数 0

PyQT5 QCamera没有点击照片

我试着引用他们的官方PyQt5 QCamera文档,但不太理解。

我已经创建了一个带有pushbutton

  • Button的主窗口,只在可用相机列表长度> 0时执行function

  • clickphoto函数以捕获图像,否则会打印错误

代码语言:javascript
复制
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import os
class Ui_MainWindow(object):
    def clickphoto(self):
        print("clickphoto Called !")
        available_cameras = QCameraInfo.availableCameras()
        if len(available_cameras)>0:
            print(available_cameras[0].description())
            try:
                camera = QCamera(available_cameras[0])
                camera.setCaptureMode(QCamera.CaptureStillImage)
                camera.start()
                capture = QCameraImageCapture(camera)
                capture.capture(str(os.getcwd())+"//"+"999.jpg")

                # i also tried capture.capture("999.jpg") still no output 
                # (checked in Pictures folder)

            except Exception as e:
                print("Exception occured, ",e)
        else:
            print("Error")

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(251, 271)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.clickphotobtn = QtWidgets.QPushButton(self.centralwidget)
        self.clickphotobtn.setGeometry(QtCore.QRect(90, 110, 75, 23))
        self.clickphotobtn.setObjectName("clickphotobtn")

        # click event !!
        self.clickphotobtn.clicked.connect(self.clickphoto)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 251, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.clickphotobtn.setText(_translate("MainWindow", "Capture"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

输出

代码语言:javascript
复制
PS C:\Users\Beast80K\Documents\Auto Brightness> & C:/Users/Beast80K/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/Beast80K/Documents/Auto Brightness/trycappic.py"
clickphoto Called !
USB2.0 PC CAMERA
Unsupported media type: "{32595559-0000-0010-8000-00AA00389B71}"
Unsupported media type: "{32595559-0000-0010-8000-00AA00389B71}"
Unsupported media type: "{32595559-0000-0010-8000-00AA00389B71}"
Unsupported media type: "{32595559-0000-0010-8000-00AA00389B71}"
Unsupported media type: "{32595559-0000-0010-8000-00AA00389B71}"

5.15.2

  • Windows

  • Python3.9.6

  • Qt版本:

  • 10 21h1 (OS Build 19043.1237)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-04 15:21:25

你必须考虑到:

  1. 检查设备是否有拍照能力。

  1. 捕获照片是异步的,所以您必须使用信号来知道设备是否准备好拍照,以及设备是否已经保存完照片。

代码语言:javascript
复制
import os
from pathlib import Path
from typing import ChainMap
import uuid

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtMultimedia import QCamera, QCameraImageCapture, QCameraInfo

from gui import Ui_MainWindow

CURRENT_DIRECTORY = Path(__file__).resolve().parent
PHOTO_DIRECTORY = CURRENT_DIRECTORY / "photos"

PHOTO_DIRECTORY.mkdir(parents=True, exist_ok=True)


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.clickphotobtn.clicked.connect(self.handle_clicked)

    def handle_clicked(self):
        self.capture()

    def capture(self):
        for camera_info in QCameraInfo.availableCameras():
            camera = QCamera(camera_info, self)
            if not camera.isCaptureModeSupported(QCamera.CaptureStillImage):
                print("Camera cannot capture images")
                continue
            camera.setCaptureMode(QCamera.CaptureStillImage)
            camer.errorOccurred.connect(self.handle_errorOccurred)
            camera.start()
            image_capture = QCameraImageCapture(camera, self)
            image_capture.readyForCaptureChanged.connect(
                self.handle_readyForCaptureChanged
            )
            image_capture.imageSaved.connect(self.handle_imageSaved)
            camera.searchAndLock()

    def handle_readyForCaptureChanged(self, ok):
        if ok:
            image_capture = self.sender()
            image_capture.capture(os.fspath(PHOTO_DIRECTORY / str(uuid.uuid4())))
            camera = image_capture.mediaObject()
            if isinstance(camera, QCamera):
                camera.unlock()

    def handle_imageSaved(self):
        image_capture = self.sender()
        camera = image_capture.mediaObject()
        if isinstance(camera, QCamera):
            camera.stop()
            camera.deleteLater()
        image_capture.deleteLater()

    def handle_errorOccurred(self, error):
        print(error, self.sender().errorString())

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

注意:不建议修改QtDesigner生成的代码,所以必须恢复文件并将其称为gui.py

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69437722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档