class _CameraWidget(QWidget): imagecaptured = pyqtSignal(QPixmap) cancel = pyqtSignal() def __init__(self, parent=None): super(_CameraWidget, self).__init__(parent) self.setLayout(QGridLayout()) self.toolbar = QToolBar() spacer = QWidget() spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.toolbar.setIconSize(QSize(48, 48)) self.toolbar.addWidget(spacer) self.swapaction = self.toolbar.addAction(QIcon(":/widgets/cameraswap"), "Swap Camera") self.swapaction.triggered.connect(self.swapcamera) self.current_camera_index = 0 self.camera = None self.viewfinder = QCameraViewfinder() self.viewfinder.mousePressEvent = self.capture self.viewfinder.setAspectRatioMode(Qt.KeepAspectRatioByExpanding) self.update_camera_buttons() self.capturebutton = QPushButton("Capture", self) self.capturebutton.pressed.connect(self.capture) self.actionCancel = self.toolbar.addAction(QIcon(":/icons/cancel"), "Cancel") self.actionCancel.triggered.connect(self._cancel) self.layout().setContentsMargins(0, 0, 0, 0) self.layout().addWidget(self.toolbar) self.layout().addWidget(self.viewfinder) self.layout().addWidget(self.capturebutton) self.viewfinder.show() def __del__(self): if self.camera: self.camera.unload() def _cancel(self): if self.camera: self.camera.unload() self.cancel.emit() @property def list_of_cameras(self): return QCameraInfo.availableCameras() def update_camera_buttons(self): if len(self.list_of_cameras) <= 1: self.swapaction.setVisible(False) def capture(self, *args): self.camera.searchAndLock() self.camera_capture.capture() self.camera.unlock() def swapcamera(self): cameras = QCameraInfo.availableCameras() if self.current_camera_index + 1 == len(cameras): self.current_camera_index = 0 else: self.current_camera_index += 1 self.start(self.current_camera_index) @property def camera_res(self): width, height = tuple(roam.config.settings['camera_res'].split(',')) return width, height def imageCaptured(self, frameid, image): # TODO Doing a pixmap convert here is a waste but downstream needs a qpixmap for now # refactor later if self.camera: self.camera.unload() self.imagecaptured.emit(QPixmap.fromImage(image)) def start(self, dev=1): if self.camera: self.camera.unload() cameras = QCameraInfo.availableCameras() self.camera = QCamera(cameras[dev]) self.camera.setViewfinder(self.viewfinder) self.camera.setCaptureMode(QCamera.CaptureStillImage) self.camera.start() self.camera_capture = QCameraImageCapture(self.camera) self.camera_capture.setCaptureDestination( QCameraImageCapture.CaptureToBuffer) self.camera_capture.imageCaptured.connect(self.imageCaptured)
class VideoWindow(QMainWindow): def __init__(self, parent=None): super(VideoWindow, self).__init__(parent) def setupVideoUi(self): self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface) self.camera = QCamera(0) self.cameraviewfinder = QCameraViewfinder() self.cameraviewfinder.setAspectRatioMode(Qt.IgnoreAspectRatio) self.cameramode = self.camera.CaptureMode(2) self.cameraimgcap = QCameraImageCapture(self.camera) self.videoWidget = QVideoWidget() self.mediaPlayer.setVideoOutput(self.videoWidget) self.camera.setViewfinder(self.cameraviewfinder) def exitCall(self): sys.exit(app.exec_()) def play(self, fileNameList): self.fileNameList = fileNameList self.play_next() def play_next(self): global play_times if play_times == len(self.fileNameList): self.timer_play.stop() return single_file = self.fileNameList[play_times] try: self.mediaPlayer.setMedia( QMediaContent(QUrl.fromLocalFile(single_file))) except Exception: print("配置视频播放出错") print(Exception) #播放视频 self.mediaPlayer.play() # 开启倒计时,进行连续播放 self.single_time = math.ceil(self.getDuration(single_file)) self.timer_play = QtCore.QTimer() self.timer_play.timeout.connect(self.play_next) # 定时器以毫秒作为单位 self.timer_play.start(self.single_time * 1000) play_times = play_times + 1 def camera_start(self): # 开启摄像头 # self.cameraviewfinder.resize(640, 480) self.camera.start() # 获取视频时长(单位为秒) def getDuration(self, fileName): total_times = 0 if type(fileName) != list: clip = VideoFileClip(fileName) single_time = clip.duration clip.reader.close() clip.audio.reader.close_proc() return single_time for f in fileName: clip = VideoFileClip(f) times = clip.duration total_times = total_times + times clip.reader.close() clip.audio.reader.close_proc() return total_times def pause(self): self.mediaPlayer.pause()