Custom Camera on Android
-
Hello,
I use Qt 6.7.3 for Android on a Zebra-phone. The integrated camera app is not very usefull (e.g. always vertical preview of taken photos). Therefore I need a special camera modul with some feature on the video-preview:
3x3 Grid and cutting of some pixel-lines at to top and buttom of the video-preview.On the right side of the dialog is a Button frame for control, on the left side I will sie the video.
I tried 3 methods, but every had problems.
1. Manipulate Vidoestream:
VideoSurface = new TMyVideoSurface(this); // derived from QAbstractVideoSurface connect(VideoSurface, &TMyVideoSurface::frameReceived, this, &Camera_Frame::updateVideoPicture);This worked on Windows very good, but on Android its to slow - the video is delayed.
The slow part should be, datatransfer from "Camera-RAM" to nommal RAM.2. Widget over the QVideoWidget
m_overlay= new TMyCameraOverlay(m_videoWidget);I see no overlay. I heared that the QVideoWidget "overdraws" all.
3. Use QGraphicsScene:
// Main part of the code m_graphicsScene = new QGraphicsScene(this); m_graphicsView = new QGraphicsView(m_graphicsScene, this); m_graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_videoItem = new QGraphicsVideoItem(); m_graphicsScene->addItem(m_videoItem); // 3. Create Custom-Overlay and put it in the Szene m_overlay = new TCameraOverlay(); // Proxy makes normal Widget visible QGraphicsProxyWidget *proxy = m_graphicsScene->addWidget(m_overlay); proxy->setZValue(1); // Overlay in den forground ! // Pack View in the Layout ui->ViewLayout->addWidget(m_graphicsView, 0, 0); // 4. Connect Camera (Set VideoItem as VideoOutput) m_captureSession = new QMediaCaptureSession(this); m_imageCapture = new QImageCapture(this); connect(m_imageCapture, &QImageCapture::imageCaptured, this, &Camera_Frame::processCapturedImage); m_captureSession->setCamera(m_camera); m_captureSession->setVideoOutput(m_videoItem); // Important: videoItem instead of videoWidget! m_captureSession->setImageCapture(m_imageCapture);But here I get a very strange effect. The whole window especialy the control-buttons do react on touch evens no more.
Who has experiences with a camera modul on Android ?
-
It seems that QCamera in Qt 6 is pretty much lacking, as you can't even adjust preview fps. It could slow because
- the camera might give you 30 fps, which the app cant handle. You'd need some own code to throttle it
- similarly, the camera may use a way too large resolution #slow. That you can set in somewhere - first get a list of supported resolutions and select from there
And when it comes to drawing a grid etc., you can always show the output in a regular widget and draw anything you wish on top of every frame.
-
Hello,
I use Qt 6.7.3 for Android on a Zebra-phone. The integrated camera app is not very usefull (e.g. always vertical preview of taken photos). Therefore I need a special camera modul with some feature on the video-preview:
3x3 Grid and cutting of some pixel-lines at to top and buttom of the video-preview.On the right side of the dialog is a Button frame for control, on the left side I will sie the video.
I tried 3 methods, but every had problems.
1. Manipulate Vidoestream:
VideoSurface = new TMyVideoSurface(this); // derived from QAbstractVideoSurface connect(VideoSurface, &TMyVideoSurface::frameReceived, this, &Camera_Frame::updateVideoPicture);This worked on Windows very good, but on Android its to slow - the video is delayed.
The slow part should be, datatransfer from "Camera-RAM" to nommal RAM.2. Widget over the QVideoWidget
m_overlay= new TMyCameraOverlay(m_videoWidget);I see no overlay. I heared that the QVideoWidget "overdraws" all.
3. Use QGraphicsScene:
// Main part of the code m_graphicsScene = new QGraphicsScene(this); m_graphicsView = new QGraphicsView(m_graphicsScene, this); m_graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_videoItem = new QGraphicsVideoItem(); m_graphicsScene->addItem(m_videoItem); // 3. Create Custom-Overlay and put it in the Szene m_overlay = new TCameraOverlay(); // Proxy makes normal Widget visible QGraphicsProxyWidget *proxy = m_graphicsScene->addWidget(m_overlay); proxy->setZValue(1); // Overlay in den forground ! // Pack View in the Layout ui->ViewLayout->addWidget(m_graphicsView, 0, 0); // 4. Connect Camera (Set VideoItem as VideoOutput) m_captureSession = new QMediaCaptureSession(this); m_imageCapture = new QImageCapture(this); connect(m_imageCapture, &QImageCapture::imageCaptured, this, &Camera_Frame::processCapturedImage); m_captureSession->setCamera(m_camera); m_captureSession->setVideoOutput(m_videoItem); // Important: videoItem instead of videoWidget! m_captureSession->setImageCapture(m_imageCapture);But here I get a very strange effect. The whole window especialy the control-buttons do react on touch evens no more.
Who has experiences with a camera modul on Android ?
@Andy314 solution one is the right answer but you're missing few things.
You should not process the camera streams on the main thread, you need to create another thread for gettig the camera frames and sending them to the UiMoreover, after getting the camera frame and display it, you should free the memory space consumed by it. otherwise it will be slower and take too much ram