Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qandroidcamera_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QANDROIDCAMERA_H
5#define QANDROIDCAMERA_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtMultimedia/private/qplatformcamera_p.h>
19
20#include <QtCore/qjniobject.h>
21#include <QtCore/private/qexpected_p.h>
22#include <QtCore/qobject.h>
23
24#include <QtMultimedia/qimagecapture.h>
25
26#include <QtFFmpegMediaPluginImpl/private/qffmpeghwaccel_p.h>
27
28#include <atomic>
29#include <memory>
30#include <optional>
31
33
34Q_DECLARE_JNI_CLASS(QtJniCamera2, "org/qtproject/qt/android/multimedia/qffmpeg/QtCamera2");
35
37class QVideoFrame;
38class QAndroidVideoFrameFactory;
39
40namespace QFFmpeg {
41
42class QAndroidImageReader;
43class QAndroidImageRegistry;
45
47
49{
51public:
53 explicit QAndroidCamera(QCamera *camera);
55
56 bool isActive() const override { return m_state == State::Started; }
57 bool isFlashModeSupported(QCamera::FlashMode mode) const override;
58 bool isFlashReady() const override;
59 bool isFocusModeSupported(QCamera::FocusMode focusMode) const override;
60 bool isTorchModeSupported(QCamera::TorchMode mode) const override;
61 void setActive(bool active) override;
62 void setCaptureSession(QPlatformMediaCaptureSession *session) override;
63 void setCamera(const QCameraDevice &camera) override;
64 bool setCameraFormat(const QCameraFormat &format) override;
65 void setFlashMode(QCamera::FlashMode mode) override;
66 void setFocusDistance(float distance) override;
67 void setStillCaptureResolution(QSize resolution);
68
69 // FocusModeAuto maps to CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE
70 //
71 // The CustomFocusPoint functionality maps to CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE
72 // but with the CaptureRequest.CONTROL_AF_REGIONS setting.
73 //
74 // FocusModeManual maps to CaptureRequest.CONTROL_AF_MODE_OFF
75 // but also requires us to be able to set focusDistance using
76 // CaptureRequest.LENS_FOCUS_DISTANCE.
77 //
78 // Currently implemented focus-modes: Auto, Manual
79 void setFocusMode(QCamera::FocusMode mode) override;
80 void setTorchMode(QCamera::TorchMode mode) override;
81 void zoomTo(float factor, float rate) override;
82
83 QVideoFrameFormat frameFormat() const override;
84
85 static bool registerNativeMethods();
86
87 void capture();
88 void updateExif(const QString &filename);
89public Q_SLOTS:
91 void onCameraOpened();
92 void onCameraDisconnect();
93 void onCameraError(int error);
94 void frameAvailable(QJniObject image, bool takePhoto = false);
98 void onCaptureSessionFailed(int reason, long frameNumber);
99 void onSessionClosed();
101
104 void onImageCaptureFailed(QImageCapture::Error error, const QString &errMsg);
105
106private:
107 // Blocks until the camera has started or failed. The error message is public.
108 [[nodiscard]] q23::expected<void, QString> tryActivate();
109 void setState(State newState);
110 QtVideo::Rotation rotation() const;
111 void updateCameraCharacteristics();
112 void cleanCameraCharacteristics();
113 QSize stillResolution() const;
114 void applyStillResolution();
115
116 void restartCamera();
117 void queueCameraRestart();
118
119 bool acceptsFrames() const;
120 void deliverFrame(QVideoFrame videoFrame, bool takePhoto);
121 void releaseImageReader();
122
123 State m_state = State::Closed;
124 QCameraDevice m_cameraDevice;
125 QtJniCamera2 m_jniCamera;
126 QFFmpegMediaCaptureSession *m_session = nullptr;
127
128 std::unique_ptr<QFFmpeg::HWAccel> m_hwAccel;
129
130 std::shared_ptr<QAndroidVideoFrameFactory> m_frameFactory;
131 std::shared_ptr<QAndroidImageReader> m_imageReader;
132 std::shared_ptr<QAndroidImageRegistry> m_imageRegistry;
133 std::shared_ptr<QAndroidPreviewTarget> m_previewTarget;
134 QList<QCamera::FlashMode> m_supportedFlashModes;
135 // List of supported focus-modes as reported by the Android camera device. Queried once when
136 // camera-device is initialized. Useful for avoiding multiple JNI calls.
137 QList<QCamera::FocusMode> m_supportedFocusModes;
138 bool m_TorchModeSupported = false;
139 bool m_wasActive = false;
140
141 QSize m_videoResolution;
142 QSize m_requestedStillResolution;
143 QSize m_stillReaderSize;
144 std::atomic<bool> m_restartPending = false;
145};
146
147} // namespace QFFmpeg
148
149QT_END_NAMESPACE
150
151#endif // QANDROIDCAMERA_H
void setCaptureSession(QPlatformMediaCaptureSession *session) override
void setFocusMode(QCamera::FocusMode mode) override
void onImageCaptureFailed(QImageCapture::Error error, const QString &errMsg)
bool isTorchModeSupported(QCamera::TorchMode mode) const override
QVideoFrameFormat frameFormat() const override
QAndroidCamera(QCamera *camera)
void setFlashMode(QCamera::FlashMode mode) override
static bool registerNativeMethods()
void onCaptureSessionFailed(int reason, long frameNumber)
void zoomTo(float factor, float rate) override
void setCamera(const QCameraDevice &camera) override
bool setCameraFormat(const QCameraFormat &format) override
void frameAvailable(QJniObject image, bool takePhoto=false)
bool isFlashReady() const override
bool isActive() const override
void setFocusDistance(float distance) override
void updateExif(const QString &filename)
void setStillCaptureResolution(QSize resolution)
bool isFocusModeSupported(QCamera::FocusMode focusMode) const override
bool isFlashModeSupported(QCamera::FlashMode mode) const override
void setActive(bool active) override
void onCameraError(int error)
void setTorchMode(QCamera::TorchMode mode) override