class Camera(QObject): def __init__(self, parent=QObject()): super(Camera, self).__init__(parent) # chooses the system default camera self.cam = QCamera() self.caminfo = QCameraInfo(self.cam) self.camvfind = QCameraViewfinder() self.camvfindset = QCameraViewfinderSettings() self.recorder = QMediaRecorder(self.cam) def iniCamera(self): cameras = QCameraInfo.availableCameras() for cameraInfo in cameras: # select the capturing device if it is available if (cameraInfo.description().find("Capture") is not -1): self.cam = QCamera(cameraInfo) self.caminfo = QCameraInfo(self.cam) self.recorder = QMediaRecorder(self.cam) print("Camera Chosen: " + self.caminfo.description()) print(self.cam.supportedViewfinderFrameRateRanges()) if self.cam.isCaptureModeSupported(QCamera.CaptureVideo): print("Capturemode supported") def startVid(self): self.cam.load() # self.camvfind.show() self.cam.setViewfinder(self.camvfind) self.cam.setCaptureMode(QCamera.CaptureVideo) self.cam.start() audio = QAudioEncoderSettings() audio.setCodec("audio/amr") audio.setQuality(QtMultimedia.QMultimedia.NormalQuality) video = QVideoEncoderSettings() # video.setCodec("video/mp4") video.setQuality(QtMultimedia.QMultimedia.NormalQuality) video.setResolution(1920, 1080) video.setFrameRate(30.0) # self.recorder.setAudioSettings(audio) self.recorder.setVideoSettings(video) self.recorder.setContainerFormat("mp4") def startRecording(self, filename): directory = os.path.abspath(str(os.getcwd())) abs_path = os.path.join(directory, filename) self.recorder.setOutputLocation(QUrl(abs_path)) self.recorder.record() def stopRecording(self): self.recorder.stop() def getViewFinder(self): return self.camvfind
def __init__(self, d_param, oc_cam_info, *args, **kwargs): super(CSmartCameraPreviewWindow, self).__init__(d_param, *args, **kwargs) self.f_initial_frame_rate = d_param['initial_frame_rate'] # in Hz s_cam_descr = oc_cam_info.description() oc_cam = QCamera(oc_cam_info) _camera_sync_load_and_start(oc_cam) l_resolutions = oc_cam.supportedViewfinderResolutions() l_frate_ranges = oc_cam.supportedViewfinderFrameRateRanges() _camera_sync_stop_and_unload(oc_cam) del oc_cam if len(l_frate_ranges) == 0 or len(l_resolutions) == 0: raise RuntimeError( "The camera (%s) does not support frame rate/resolution information retrieval" % s_cam_descr) self.b_startup_guard = False self.toolbar = QtWidgets.QToolBar("Preview") l_items = [] for oc_res in l_resolutions: l_items.append("%i x %i" % (oc_res.width(), oc_res.height())) self.cbox_resolution = CLabeledComboBox("Resolution:") self.cbox_resolution.cbox.addItems(l_items) self.cbox_resolution.cbox.currentIndexChanged.connect( self.__cb_on_resolution_cbox_index_changed) self.toolbar.addWidget(self.cbox_resolution) self.toolbar.addSeparator() l_items = [] for oc_frate in l_frate_ranges: l_items.append("%f" % oc_frate.minimumFrameRate) self.cbox_frame_rate = CLabeledComboBox("Frame Rate:") self.cbox_frame_rate.cbox.addItems(l_items) self.cbox_frame_rate.cbox.currentIndexChanged.connect( self.__cb_on_frame_rate_cbox_index_changed) self.toolbar.addWidget(self.cbox_frame_rate) self.toolbar.addSeparator() self.addToolBar(self.toolbar)
class Camera(QObject): def __init__(self, parent=QObject()): super(Camera, self).__init__(parent) # chooses the system default camera self.cam = QCamera() self.imageCapture = QCameraImageCapture(self.cam) self.caminfo = QCameraInfo(self.cam) self.camvfind = QCameraViewfinder() self.camvfindset = QCameraViewfinderSettings() self.recorder = QMediaRecorder(self.cam) def init_camera(self): cameras = QCameraInfo.availableCameras() for cameraInfo in cameras: # select the capturing device if it is available if cameraInfo.description().find("Capture") is not -1: self.cam = QCamera(cameraInfo) self.caminfo = QCameraInfo(self.cam) self.recorder = QMediaRecorder(self.cam) print("Camera Chosen: " + self.caminfo.description()) print(self.cam.supportedViewfinderFrameRateRanges()) self.cam.setCaptureMode(QCamera.CaptureStillImage) if self.cam.isCaptureModeSupported(QCamera.CaptureStillImage): print("Capturemode supported") self.cam.load() self.cam.setViewfinder(self.camvfind) self.cam.start() self.imageCapture = QCameraImageCapture(self.cam) self.imageCapture.setCaptureDestination( QCameraImageCapture.CaptureToBuffer) def capture_still(self): self.imageCapture.capture() def setOnCapture(self, callback=None): self.imageCapture.imageCaptured.connect(callback) def start_vid(self): self.cam.load() # self.camvfind.show() self.cam.setViewfinder(self.camvfind) self.cam.setCaptureMode(QCamera.CaptureVideo) self.cam.start() audio = QAudioEncoderSettings() audio.setCodec("audio/amr") audio.setQuality(QtMultimedia.QMultimedia.NormalQuality) video = QVideoEncoderSettings() # video.setCodec("video/mp4") video.setQuality(QtMultimedia.QMultimedia.NormalQuality) video.setResolution(1920, 1080) video.setFrameRate(30.0) # self.recorder.setAudioSettings(audio) self.recorder.setVideoSettings(video) self.recorder.setContainerFormat("mp4") print("Output Loc: " + str(self.recorder.outputLocation())) def start_recording(self): directory = os.path.abspath(str(os.getcwd())) filename = "test" + str(time.time()) + ".mp4" abs_path = os.path.join(directory, filename) self.recorder.setOutputLocation(QUrl(abs_path)) self.recorder.record() def stop_recording(self): self.recorder.stop()
def __init__(self, d_param, oc_cam_info, *args, **kwargs): super(CSmartCameraPreviewWindow, self).__init__(d_param, *args, **kwargs) self.i_initial_cbox_resolution_idx = -1 self.f_initial_frame_rate = d_param['initial_frame_rate'] # in Hz self.i_initial_frame_width = d_param['initial_frame_width'] self.i_initial_frame_height = d_param['initial_frame_height'] self.s_initial_frame_wh = "%i x %i" % (self.i_initial_frame_width, self.i_initial_frame_height) s_cam_descr = oc_cam_info.description() print("DEBUG: CSmartCameraPreviewWindow(): [%s @ %.1f Hz] %s" % \ (self.s_initial_frame_wh, self.f_initial_frame_rate, s_cam_descr) \ ) oc_cam = QCamera(oc_cam_info) oc_cam.load() l_resolutions = oc_cam.supportedViewfinderResolutions() l_frate_ranges = oc_cam.supportedViewfinderFrameRateRanges() oc_cam.unload() del oc_cam if len(l_frate_ranges) == 0 or len(l_resolutions) == 0: raise RuntimeError("The camera (%s) does not support frame rate/resolution information retrieval" % s_cam_descr) b_requested_fsize_found = False for oc_res in l_resolutions: if self.s_initial_frame_wh == "%i x %i" % (oc_res.width(), oc_res.height()): b_requested_fsize_found = True break if not b_requested_fsize_found: raise RuntimeError( \ "The camera [%s] does not support frame size value [%s] requested in the ini file" % \ (s_cam_descr, self.s_initial_frame_wh) \ ) b_requested_frate_found = False for oc_frate in l_frate_ranges: if abs(oc_frate.minimumFrameRate - self.f_initial_frame_rate) <= 0.5: b_requested_frate_found = True if not b_requested_frate_found: raise RuntimeError( \ "The camera [%s] does not support frame rate value [%i Hz] requested in the ini file" % \ (s_cam_descr, self.f_initial_frame_rate) \ ) self.b_startup_guard = False self.toolbar = QtWidgets.QToolBar("Preview") l_items = [] for oc_res in l_resolutions: l_items.append("%i x %i" % (oc_res.width(), oc_res.height())) self.cbox_resolution = CLabeledComboBox("Resolution:") self.cbox_resolution.cbox.addItems(l_items) self.cbox_resolution.cbox.currentIndexChanged.connect(self.__cb_on_resolution_cbox_index_changed) self.toolbar.addWidget(self.cbox_resolution) self.toolbar.addSeparator() l_items = [] for oc_frate in l_frate_ranges: l_items.append("%f" % oc_frate.minimumFrameRate) self.cbox_frame_rate = CLabeledComboBox("Frame Rate:") self.cbox_frame_rate.cbox.addItems(l_items) self.cbox_frame_rate.cbox.currentIndexChanged.connect(self.__cb_on_frame_rate_cbox_index_changed) self.toolbar.addWidget(self.cbox_frame_rate) self.toolbar.addSeparator() self.addToolBar(self.toolbar)
def main(): app = QApplication( sys.argv ) # must be called in order to be able to use all the stuff below(!) print("The application is executed on: %s" % app.platformName()) l_cameras = QCameraInfo.availableCameras() if len(l_cameras) == 0: print("ERROR: no cameras found") sys.exit(-1) else: print("INFO: found %i camera(s)" % len(l_cameras)) print() for i_idx, oc_caminfo in enumerate(l_cameras): # QCameraInfo - derived properties print("Camera #%i:" % i_idx) print("\t deviceName: %s" % oc_caminfo.deviceName()) print("\t description: %s" % oc_caminfo.description()) print("\t isNull: %s" % oc_caminfo.isNull()) print("\t orientation: %s" % oc_caminfo.orientation()) print("\t position: %s" % oc_caminfo.position()) # QCamera - derived properties oc_camera = QCamera(i_idx) camera_sync_load_and_start(oc_camera) oc_camera_exposure = oc_camera.exposure() print("\t captureMode: %s" % captureMode2str(oc_camera.captureMode())) print("\t exposure:") print("\t\t isAvailabe: %s" % oc_camera_exposure.isAvailable()) if oc_camera_exposure.isAvailable(): print("\t\t aperture: %s" % oc_camera_exposure.aperture()) # TODO test this, add more entries oc_camera_focus = oc_camera.focus() print("\t focus:") print("\t\t isAvailabe: %s" % oc_camera_focus.isAvailable()) if oc_camera_focus.isAvailable(): print("\t\t aperture: %f" % oc_camera_focus.digitalZoom()) # TODO test this, add more entries oc_cam_img_proc = oc_camera.imageProcessing() print("\t imageProcessing:") print("\t\t isAvailabe: %s" % oc_cam_img_proc.isAvailable()) if oc_cam_img_proc.isAvailable(): print("\t\t brightness: %f" % oc_cam_img_proc.brightness()) print("\t\t contrast: %f" % oc_cam_img_proc.contrast()) print("\t\t denoisingLevel: %f" % oc_cam_img_proc.denoisingLevel()) print("\t\t manualWhiteBalance: %f" % oc_cam_img_proc.manualWhiteBalance()) print("\t\t saturation: %f" % oc_cam_img_proc.saturation()) print("\t\t sharpeningLevel: %f" % oc_cam_img_proc.sharpeningLevel()) print("\t isVideoCaptureSupported: %s" % oc_camera.isCaptureModeSupported(QCamera.CaptureVideo)) print("\t lockStatus: %s" % lockStatus2str(oc_camera.lockStatus())) print("\t requestedLocks: %s" % lockType2str(oc_camera.requestedLocks())) print("\t state: %s" % state2str(oc_camera.state())) print("\t status: %s" % status2str(oc_camera.status())) print("\t supportedLocks: %s" % lockType2str(oc_camera.supportedLocks())) print( "\t supportedViewfinderFrameRateRanges: %s" % frameRateRange2str(oc_camera.supportedViewfinderFrameRateRanges())) print("\t supportedViewfinderPixelFormats: %s" % repr(oc_camera.supportedViewfinderPixelFormats())) print("\t supportedViewfinderResolutions: \n\t\t%s" % frameResolution2str(oc_camera.supportedViewfinderResolutions())) print("\t len(supportedViewfinderSettings): %s" % len(oc_camera.supportedViewfinderSettings())) # QCameraViewfinderSettings - derived properties oc_vf_settings = oc_camera.viewfinderSettings() if oc_vf_settings.isNull(): print("\t viewfinderSettings: not supported") camera_sync_stop_and_unload(oc_camera) print() continue print("\t maximumFrameRate: %f" % oc_vf_settings.maximumFrameRate()) print("\t minimumFrameRate: %f" % oc_vf_settings.minimumFrameRate()) print("\t resolution: %s" % frameResolution2str([oc_vf_settings.resolution()])) # TODO the rest of methods... camera_sync_stop_and_unload(oc_camera) print()
class CameraInterface(CameraBase.CameraBase): def __init__(self, *args, camera_info=None): super(CameraInterface, self).__init__(*args) # 定义相机实例对象并设置捕获模式 if camera_info: self.mCamera = QCamera(camera_info) else: self.mCamera = QCamera() self.mCamera.setCaptureMode(QCamera.CaptureViewfinder) self.mDisplayWidth = 800 self.mDisplayHeight = 600 self.mRate = 10 # 设置取景器分辨率 self.setDisplaySize(self.mDisplayWidth, self.mDisplayHeight) self.setRate(self.mRate) # 初始化取景器 self.mViewCamera = QtMultimediaWidgets.QCameraViewfinder(self) self.mViewCamera.show() self.mCamera.setViewfinder(self.mViewCamera) self.mCamera.setCaptureMode(QCamera.CaptureStillImage) # 设置图像捕获 self.mCapture = QCameraImageCapture(self.mCamera) if self.mCapture.isCaptureDestinationSupported( QCameraImageCapture.CaptureToBuffer): self.mCapture.setCaptureDestination( QCameraImageCapture.CaptureToBuffer) # CaptureToBuffer # self.mCapture.error.connect(lambda i, e, s: self.alert(s)) self.mCapture.imageAvailable.connect(self.readFrame) self.mTimerImageGrab = QTimer(self) self.mTimerImageGrab.timeout.connect(self.timerImgGrab) # self.t1 = 0.0 def timerImgGrab(self): self.mCapture.capture('tmp.jpg') def readFrame(self, requestId, image): self.mFrame = image.image().copy() def openCamera(self): if not self.mCameraOpened: self.mCamera.start() viewFinderSettings = QCameraViewfinderSettings() rate_range = self.mCamera.supportedViewfinderFrameRateRanges() if rate_range: viewFinderSettings.setMinimumFrameRate( rate_range[0].minimumFrameRate) viewFinderSettings.setMaximumFrameRate( rate_range[0].maximumFrameRate) else: viewFinderSettings.setMinimumFrameRate(1) viewFinderSettings.setMaximumFrameRate(self.mRate) self.mTimerImageGrab.start(1000 / self.mRate) self.mCameraOpened = True def releaseCamera(self): if self.mCameraOpened: self.mCamera.stop() self.mCameraOpened = False self.signalReleased.emit() def takePictures(self, path: str): self.mCapture.setCaptureDestination(QCameraImageCapture.CaptureToFile) self.mCapImg.capture(path) self.mCapture.setCaptureDestination( QCameraImageCapture.CaptureToBuffer) def takeVideo(self, path: str): pass def endTakeVideo(self): pass def setDisplaySize(self, display_width_: int, display_height_: int): self.mDisplayWidth = display_width_ self.mDisplayHeight = display_height_ viewFinderSettings = QCameraViewfinderSettings() viewFinderSettings.setResolution(self.mDisplayWidth, self.mDisplayHeight) self.mCamera.setViewfinderSettings(viewFinderSettings) def setRate(self, rate): self.mRate = rate