QCamera 类

QCamera 类提供系统摄像头设备的接口。 更多...

头: #include <QCamera>
qmake: QT += multimedia
继承: QMediaObject

公共类型

struct FrameRateRange
enum CaptureMode { CaptureViewfinder, CaptureStillImage, CaptureVideo }
flags CaptureModes
enum Error { NoError, CameraError, InvalidRequestError, ServiceMissingError, NotSupportedFeatureError }
enum LockChangeReason { UserRequest, LockAcquired, LockFailed, LockLost, LockTemporaryLost }
enum LockStatus { Unlocked, Searching, Locked }
enum LockType { NoLock, LockExposure, LockWhiteBalance, LockFocus }
flags LockTypes
enum Position { UnspecifiedPosition, BackFace, FrontFace }
enum State { UnloadedState, LoadedState, ActiveState }
enum Status { ActiveStatus, StartingStatus, StoppingStatus, StandbyStatus, LoadedStatus, …, UnavailableStatus }

特性

公共函数

QCamera (QCamera::Position position , QObject * parent = nullptr)
QCamera (const QCameraInfo & cameraInfo , QObject * parent = nullptr)
QCamera (const QByteArray & deviceName , QObject * parent = nullptr)
QCamera (QObject * parent = nullptr)
virtual ~QCamera ()
QCamera::CaptureModes captureMode () const
QCamera::Error error () const
QString errorString () const
QCameraExposure * exposure () const
QCameraFocus * focus () const
QCameraImageProcessing * imageProcessing () const
bool isCaptureModeSupported (QCamera::CaptureModes mode ) const
QCamera::LockStatus lockStatus () const
QCamera::LockStatus lockStatus (QCamera::LockType lockType ) const
QCamera::LockTypes requestedLocks () const
void setViewfinder (QVideoWidget * viewfinder )
void setViewfinder (QGraphicsVideoItem * viewfinder )
void setViewfinder (QAbstractVideoSurface * surface )
void setViewfinderSettings (const QCameraViewfinderSettings & settings )
QCamera::State state () const
QCamera::Status status () const
QCamera::LockTypes supportedLocks () const
QList<QCamera::FrameRateRange> supportedViewfinderFrameRateRanges (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const
QList<QVideoFrame::PixelFormat> supportedViewfinderPixelFormats (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const
QList<QSize> supportedViewfinderResolutions (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const
QList<QCameraViewfinderSettings> supportedViewfinderSettings (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const
QCameraViewfinderSettings viewfinderSettings () const

重实现公共函数

virtual QMultimedia::AvailabilityStatus availability () const override

公共槽

void load ()
void searchAndLock (QCamera::LockTypes locks )
void searchAndLock ()
void setCaptureMode (QCamera::CaptureModes mode )
void start ()
void stop ()
void unload ()
void unlock (QCamera::LockTypes locks )
void unlock ()

信号

void captureModeChanged (QCamera::CaptureModes mode )
void errorOccurred (QCamera::Error value )
void lockFailed ()
void lockStatusChanged (QCamera::LockType lock , QCamera::LockStatus status , QCamera::LockChangeReason reason )
void lockStatusChanged (QCamera::LockStatus status , QCamera::LockChangeReason reason )
void locked ()
void stateChanged (QCamera::State state )
void statusChanged (QCamera::Status status )

详细描述

QCamera can be used with QCameraViewfinder for viewfinder display, QMediaRecorder for video recording and QCameraImageCapture for image taking.

可以使用 QCameraInfo 列表可用摄像头并选取要使用的摄像头。

const QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
for (const QCameraInfo &cameraInfo : cameras) {
    if (cameraInfo.deviceName() == "mycamera")
        camera = new QCamera(cameraInfo);
}
					

摄像头概述 了解更多信息。

成员类型文档编制

enum QCamera:: CaptureMode
flags QCamera:: CaptureModes

This enum holds the capture mode of the camera.

常量 描述
QCamera::CaptureViewfinder 0 Camera is only configured to display viewfinder.
QCamera::CaptureStillImage 0x01 Camera is configured for still frames capture.
QCamera::CaptureVideo 0x02 Camera is configured for video capture.

The CaptureModes type is a typedef for QFlags <CaptureMode>. It stores an OR combination of CaptureMode values.

enum QCamera:: Error

此枚举保持最后错误代码。

常量 描述
QCamera::NoError 0 没有发生错误。
QCamera::CameraError 1 有出现错误。
QCamera::InvalidRequestError 2 System resource doesn't support requested functionality.
QCamera::ServiceMissingError 3 没有可用摄像头服务。
QCamera::NotSupportedFeatureError 4 不支持特征。

enum QCamera:: LockChangeReason

This enum holds the reason why the camera lock status changed.

常量 描述
QCamera::UserRequest 0 The lock status changed in result of user request, usually to unlock camera settings.
QCamera::LockAcquired 1 The lock status successfuly changed to QCamera::Locked .
QCamera::LockFailed 2 The camera failed to acquire the requested lock in result of autofocus failure, exposure out of supported range, etc.
QCamera::LockLost 3 The camera is not able to maintain the requested lock any more. Lock status is changed to QCamera::Unlocked .
QCamera::LockTemporaryLost 4 The lock is lost, but the camera is working hard to reacquire it. This value may be used in continuous focusing mode, when the camera loses the focus, the focus lock state is changed to Qcamera::Searching with LockTemporaryLost reason.

enum QCamera:: LockStatus

This enum holds the overall status for all the requested camera locks.

常量 描述
QCamera::Unlocked 0 The application is not interested in camera settings value. The camera may keep this parameter without changes, this is common with camera focus, or adjust exposure and white balance constantly to keep the viewfinder image nice.
QCamera::Searching 1 The application has requested the camera focus, exposure or white balance lock with QCamera::searchAndLock (). This state indicates the camera is focusing or calculating exposure and white balance.
QCamera::Locked 2 The camera focus, exposure or white balance is locked. The camera is ready to capture, application may check the exposure stays the same, parameters. The Locked status usually means the requested parameter except in the cases when the parameter is requested to be constantly updated. For example, in continuous focusing mode, the focus is considered locked as long as the object is in focus, even while the actual focusing distance may be constantly changing.

enum QCamera:: LockType
flags QCamera:: LockTypes

This enum holds the camera lock type.

常量 描述
QCamera::NoLock 0
QCamera::LockExposure 0x01 Lock camera exposure.
QCamera::LockWhiteBalance 0x02 Lock the white balance.
QCamera::LockFocus 0x04 Lock camera focus.

The LockTypes type is a typedef for QFlags <LockType>. It stores an OR combination of LockType values.

enum QCamera:: Position

This enum specifies the physical position of the camera on the system hardware.

常量 描述
QCamera::UnspecifiedPosition 0 The camera position is unspecified or unknown.
QCamera::BackFace 1 The camera is on the back face of the system hardware. For example on a mobile device, it means it is on the opposite side to that of the screen.
QCamera::FrontFace 2 The camera is on the front face of the system hardware. For example on a mobile device, it means it is on the same side as that of the screen. Viewfinder frames of front-facing cameras are mirrored horizontally, so the users can see themselves as looking into a mirror. Captured images or videos are not mirrored.

该枚举在 Qt 5.3 引入或被修改。

另请参阅 QCameraInfo::position ().

enum QCamera:: State

This enum holds the current state of the camera.

常量 描述
QCamera::UnloadedState 0 The initial camera state, with camera not loaded. The camera capabilities, except supported capture modes, are unknown. While the supported settings are unknown in this state, it's allowed to set the camera capture settings like codec, resolution, or frame rate.
QCamera::LoadedState 1 The camera is loaded and ready to be configured. In this state it's allowed to query camera capabilities, set capture resolution, codecs, etc. The viewfinder is not active in the loaded state. The camera consumes power in the loaded state.
QCamera::ActiveState 2 In the active state as soon as camera is started the viewfinder displays video frames and the camera is ready for capture.

enum QCamera:: Status

This enum holds the current status of the camera.

常量 描述
QCamera::ActiveStatus 8 The camera has been started and can produce data. The viewfinder displays video frames in active state. Depending on backend, changing some camera settings like capture mode, codecs or resolution in ActiveState may lead to changing the camera status to LoadedStatus and StartingStatus while the settings are applied and back to ActiveStatus when the camera is ready.
QCamera::StartingStatus 6 The camera is starting in result of state transition to QCamera::ActiveState . The camera service is not ready to capture yet.
QCamera::StoppingStatus 7 The camera is stopping in result of state transition from QCamera::ActiveState to QCamera::LoadedState or QCamera::UnloadedState .
QCamera::StandbyStatus 5 The camera is in the power saving standby mode. The camera may come to the standby mode after some time of inactivity in the QCamera::LoadedState 状态。
QCamera::LoadedStatus 4 The camera is loaded and ready to be configured. This status indicates the camera device is opened and it's possible to query for supported image and video capture settings, like resolution, framerate and codecs.
QCamera::LoadingStatus 2 The camera device loading in result of state transition from QCamera::UnloadedState to QCamera::LoadedState or QCamera::ActiveState .
QCamera::UnloadingStatus 3 The camera device is unloading in result of state transition from QCamera::LoadedState or QCamera::ActiveState to QCamera::UnloadedState .
QCamera::UnloadedStatus 1 The initial camera status, with camera not loaded. The camera capabilities including supported capture settings may be unknown.
QCamera::UnavailableStatus 0 The camera or camera backend is not available.

特性文档编制

captureMode : QCamera::CaptureModes

The type of media (video or still images), the camera is configured to capture.

It's allowed to change capture mode in any camera state, but if the camera is currently active, chaging capture mode is likely to lead to camera status chaged to QCamera::LoadedStatus , QCamera::LoadingStatus , and when the camera is ready to QCamera::ActiveStatus .

访问函数:

QCamera::CaptureModes captureMode () const
void setCaptureMode (QCamera::CaptureModes mode )

通知程序信号:

void captureModeChanged (QCamera::CaptureModes mode )

lockStatus : const QCamera::LockStatus

This property holds the overall status for all the requested camera locks.

访问函数:

QCamera::LockStatus lockStatus () const
QCamera::LockStatus lockStatus (QCamera::LockType lockType ) const

通知程序信号:

void lockStatusChanged (QCamera::LockStatus status , QCamera::LockChangeReason reason )
void lockStatusChanged (QCamera::LockType lock , QCamera::LockStatus status , QCamera::LockChangeReason reason )

state : const QCamera::State

This property holds the current state of the camera object.

访问函数:

QCamera::State state () const

通知程序信号:

void stateChanged (QCamera::State state )

status : const QCamera::Status

This property holds the current status of the camera object.

访问函数:

QCamera::Status status () const

通知程序信号:

void statusChanged (QCamera::Status status )

成员函数文档编制

QCamera:: QCamera ( QCamera::Position position , QObject * parent = nullptr)

Construct a QCamera which uses a hardware camera located a the specified position .

For example on a mobile phone it can be used to easily choose between front-facing and back-facing cameras.

If no camera is available at the specified position or if position is QCamera::UnspecifiedPosition , the default camera is used.

该函数在 Qt 5.3 引入。

QCamera:: QCamera (const QCameraInfo & cameraInfo , QObject * parent = nullptr)

Construct a QCamera from a camera description cameraInfo and parent .

该函数在 Qt 5.3 引入。

QCamera:: QCamera (const QByteArray & deviceName , QObject * parent = nullptr)

Construct a QCamera from deviceName and parent .

If no camera with that deviceName exists, the camera object will be invalid.

QCamera:: QCamera ( QObject * parent = nullptr)

Construct a QCamera with a parent .

[signal] void QCamera:: captureModeChanged ( QCamera::CaptureModes mode )

Signals the capture mode 已改变。

注意: 通知程序信号对于特性 captureMode .

[signal] void QCamera:: errorOccurred ( QCamera::Error value )

Signal emitted when error state changes to value .

该函数在 Qt 5.15 引入。

[slot] void QCamera:: load ()

Opens the camera device. The camera state is changed to QCamera::LoadedState .

It's not necessary to explicitly load the camera, unless the application needs to read the supported camera settings and change the default values according to the camera capabilities.

In all the other cases, it's possible to start the camera directly from the unloaded state.

/sa QCamera::UnloadedState

[signal] void QCamera:: lockFailed ()

Signals locking of at least one requested camera settings failed.

[signal] void QCamera:: lockStatusChanged ( QCamera::LockType lock , QCamera::LockStatus status , QCamera::LockChangeReason reason )

信号 lock status was changed with specified reason .

注意: 通知程序信号对于特性 lockStatus .

注意: 信号 lockStatusChanged 在此类中被重载。通过使用函数指针句法连接到此信号,Qt 提供用于获得如此范例展示的函数指针的方便帮助程序:

connect(camera, QOverload<QCamera::LockType, QCamera::LockStatus, QCamera::LockChangeReason>::of(&QCamera::lockStatusChanged),
    [=](QCamera::LockType lock, QCamera::LockStatus status, QCamera::LockChangeReason reason){ /* ... */ });
					

[signal] void QCamera:: lockStatusChanged ( QCamera::LockStatus status , QCamera::LockChangeReason reason )

Signals the overall status for all the requested camera locks was changed with specified reason .

注意: 通知程序信号对于特性 lockStatus .

注意: 信号 lockStatusChanged 在此类中被重载。通过使用函数指针句法连接到此信号,Qt 提供用于获得如此范例展示的函数指针的方便帮助程序:

connect(camera, QOverload<QCamera::LockStatus, QCamera::LockChangeReason>::of(&QCamera::lockStatusChanged),
    [=](QCamera::LockStatus status, QCamera::LockChangeReason reason){ /* ... */ });
					

[signal] void QCamera:: locked ()

Signals all the requested camera settings are locked.

[slot] void QCamera:: searchAndLock ( QCamera::LockTypes locks )

Locks the camera settings with the requested locks , including focusing in the single autofocus mode, exposure and white balance if the exposure and white balance modes are not manual.

The camera settings are usually locked before taking one or multiple still images, in responce to the shutter button being half pressed.

QCamera::locked () signal is emitted when camera settings are successfully locked, otherwise QCamera::lockFailed () 被发射。

QCamera also emits lockStatusChanged ( QCamera::LockType , QCamera::LockStatus ) on individual lock status changes and lockStatusChanged ( QCamera::LockStatus ) signal on composite status changes.

Locking serves two roles: it initializes calculation of automatic parameter (focusing, calculating the correct exposure and white balance) and allows to keep some or all of those parameters during number of shots.

If the camera doesn't support keeping one of parameters between shots, the related lock state changes to QCamera::Unlocked .

It's also acceptable to relock already locked settings, depending on the lock parameter this initiates new focusing, exposure or white balance calculation.

[slot] void QCamera:: searchAndLock ()

Lock all the supported camera settings.

[slot] void QCamera:: start ()

Starts the camera.

State is changed to QCamera::ActiveState if camera is started successfully, otherwise errorOccurred () 信号被发射。

While the camera state is changed to QCamera::ActiveState , starting the camera service can be asynchronous with the actual status reported with QCamera::status 特性。

[signal] void QCamera:: stateChanged ( QCamera::State state )

Signals the camera state 已改变。

Usually the state changes is caused by calling load (), unload (), start () 和 stop (), but the state can also be changed change as a result of camera error.

注意: 通知程序信号对于特性 state .

[signal] void QCamera:: statusChanged ( QCamera::Status status )

Signals the camera status 已改变。

注意: 通知程序信号对于特性 status .

[slot] void QCamera:: stop ()

Stops the camera. The camera state is changed from QCamera::ActiveState to QCamera::LoadedState .

In this state, the camera still consumes power.

另请参阅 unload () 和 QCamera::UnloadedState .

[slot] void QCamera:: unload ()

Closes the camera device and deallocates the related resources. The camera state is changed to QCamera::UnloadedState .

[slot] void QCamera:: unlock ( QCamera::LockTypes locks )

Unlocks the camera settings specified with locks or cancel the current locking if one is active.

[slot] void QCamera:: unlock ()

Unlock all the requested camera locks.

[virtual] QCamera:: ~QCamera ()

销毁摄像头对象。

[override virtual] QMultimedia::AvailabilityStatus QCamera:: availability () const

重实现: QMediaObject::availability () const.

Returns the availability state of the camera service.

QCamera::Error QCamera:: error () const

Returns the error state of the object.

QString QCamera:: errorString () const

Returns a string describing a camera's error state.

QCameraExposure *QCamera:: exposure () const

Returns the camera exposure control object.

QCameraFocus *QCamera:: focus () const

Returns the camera focus control object.

QCameraImageProcessing *QCamera:: imageProcessing () const

Returns the camera image processing control object.

bool QCamera:: isCaptureModeSupported ( QCamera::CaptureModes mode ) const

Returns true if the capture mode is suported.

QCamera::LockStatus QCamera:: lockStatus () const

Returns the status of requested camera settings locks.

注意: Getter function for property lockStatus.

QCamera::LockStatus QCamera:: lockStatus ( QCamera::LockType lockType ) const

Returns the lock status for a given lockType .

注意: Getter 函数对于特性 lockStatus .

QCamera::LockTypes QCamera:: requestedLocks () const

Returns the requested lock types.

void QCamera:: setViewfinder ( QVideoWidget * viewfinder )

设置 QVideoWidget based camera viewfinder . The previously set viewfinder is detached.

void QCamera:: setViewfinder ( QGraphicsVideoItem * viewfinder )

设置 QGraphicsVideoItem based camera viewfinder . The previously set viewfinder is detached.

void QCamera:: setViewfinder ( QAbstractVideoSurface * surface )

Sets a video surface as the viewfinder of a camera.

If a viewfinder has already been set on the camera the new surface will replace it.

void QCamera:: setViewfinderSettings (const QCameraViewfinderSettings & settings )

Sets the viewfinder settings .

If some parameters are not specified, or null settings are passed, the camera will choose default values.

If the camera is used to capture videos or images, the viewfinder settings might be ignored if they conflict with the capture settings. You can check the actual viewfinder settings once the camera is in the QCamera::ActiveStatus 状态。

Changing the viewfinder settings while the camera is in the QCamera::ActiveState state may cause the camera to be restarted.

该函数在 Qt 5.5 引入。

另请参阅 viewfinderSettings (), supportedViewfinderResolutions (), supportedViewfinderFrameRateRanges (),和 supportedViewfinderPixelFormats ().

QCamera::LockTypes QCamera:: supportedLocks () const

Returns the lock types that the camera supports.

QList < QCamera::FrameRateRange > QCamera:: supportedViewfinderFrameRateRanges (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const

Returns a list of supported viewfinder frame rate ranges.

This is a convenience function which retrieves unique frame rate ranges from the supported settings.

If non null viewfinder settings are passed, the returned list is reduced to frame rate ranges supported with partial settings applied.

The camera must be loaded before calling this function, otherwise the returned list is empty.

该函数在 Qt 5.5 引入。

另请参阅 QCameraViewfinderSettings::minimumFrameRate (), QCameraViewfinderSettings::maximumFrameRate (),和 setViewfinderSettings ().

QList < QVideoFrame::PixelFormat > QCamera:: supportedViewfinderPixelFormats (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const

Returns a list of supported viewfinder pixel formats.

This is a convenience function which retrieves unique pixel formats from the supported settings.

If non null viewfinder settings are passed, the returned list is reduced to pixel formats supported with partial settings applied.

The camera must be loaded before calling this function, otherwise the returned list is empty.

该函数在 Qt 5.5 引入。

另请参阅 QCameraViewfinderSettings::pixelFormat () 和 setViewfinderSettings ().

QList < QSize > QCamera:: supportedViewfinderResolutions (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const

Returns a list of supported viewfinder resolutions.

This is a convenience function which retrieves unique resolutions from the supported settings.

If non null viewfinder settings are passed, the returned list is reduced to resolutions supported with partial settings applied.

The camera must be loaded before calling this function, otherwise the returned list is empty.

该函数在 Qt 5.5 引入。

另请参阅 QCameraViewfinderSettings::resolution () 和 setViewfinderSettings ().

QList < QCameraViewfinderSettings > QCamera:: supportedViewfinderSettings (const QCameraViewfinderSettings & settings = QCameraViewfinderSettings()) const

Returns a list of supported viewfinder settings.

The list is ordered by preference; preferred settings come first.

可选 settings argument can be used to conveniently filter the results. If settings is non null, the returned list is reduced to settings matching the given partial settings .

The status of the camera must be LoadedStatus before calling this function, otherwise the returned list is empty.

该函数在 Qt 5.5 引入。

另请参阅 setViewfinderSettings (), supportedViewfinderResolutions (), supportedViewfinderFrameRateRanges (),和 supportedViewfinderPixelFormats ().

QCameraViewfinderSettings QCamera:: viewfinderSettings () const

Returns the viewfinder settings being used by the camera.

Settings may change when the camera is started, for example if the viewfinder settings are undefined or if unsupported values are set.

If viewfinder settings are not supported by the camera, it always returns a null QCameraViewfinderSettings 对象。

该函数在 Qt 5.5 引入。

另请参阅 setViewfinderSettings ().