Results 1 to 2 of 2

Thread: QCameraViewfinder behaves differently in Windows and Xubuntu.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QCameraViewfinder behaves differently in Windows and Xubuntu.

    Hi, I wanted to create a test project to use my webcam to take a still picture and save it as a file.
    I'm using Qt 5.3.1 on Windows7 and Qt 5.5.1 on Xubuntu 14
    The code is identical in both platforms, but in my Mainwindows application, I have to different ways of taking the picture:
    1. The first way involves creating a QCamera, a QCameraViewfinder and a QCameraImageCapture from within the Mainwindow. One slot creates the camera and viewfinder which it displays as a separate window. The second slot attempts to capture the image so as to save to a file.

    1. The second way is to create a separate dialog box which has the QCameraViewfinder inside a QLayout. Most of the dialog is designed with the QtDesigner inside QtCreator,
      and the QCameraViewfinder is appended (by hand written code) as a widget to the vertical layout I have used to align a btCapture button and a stop button.


    It's a surprise to me that the first method works on Xubuntu, but not on Windows: I get a message
    failed to start
    Stream did not open
    in Windows (with a black viewfinder)
    but a small and functional viewfinder in Xubuntu.
    The problem with this is that I get a lot of
    QWidget:: paintEngine: Should no longer be called
    messages.

    The second solution works in Windows without a problem, but on Xubuntu I get a lot of
    QWidget:: paintEngine: Should no longer be called
    messages.
    I dont know if those messages signal danger!

    Of course a nice small fully working example would be welcome.

    Now here is the code. In the mainwindow.cpp file I have this
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow),
    4. m_QCamera(NULL),
    5. m_QCameraImageCapture(NULL),
    6. m_QCameraViewfinder(NULL),
    7. m_DialogViewer(NULL)
    8. {
    9. ui->setupUi(this);
    10. m_DialogViewer = new DialogViewer(this);
    11. connect(ui->actionCount_cameras,SIGNAL(triggered(bool)),this, SLOT(slotCountCameras()));
    12. connect(ui->actionViewfinder,SIGNAL(triggered(bool)),this,SLOT(slotViewfinder1()));
    13. connect(ui->actionCapture_Image,SIGNAL(triggered(bool)),this,SLOT(slotCaptureImage1()));
    14. connect(ui->actionCapture_Image2,SIGNAL(triggered(bool)),this,SLOT(slotCaptureImage2()));
    15. connect(ui->actionViewer_dialog,SIGNAL(triggered(bool)),this,SLOT(slotDialogViewer()));
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. if(m_QCamera)
    22. delete m_QCamera;
    23. if(m_QCameraImageCapture)
    24. delete m_QCameraImageCapture;
    25. if(m_QCameraViewfinder)
    26. delete m_QCameraViewfinder;
    27. if(m_DialogViewer)
    28. delete m_DialogViewer;
    29. }
    30. void MainWindow::tracemsg(QString s)
    31. {
    32. ui->plainTextEdit1->append(s);
    33. }
    34.  
    35. void MainWindow::slotCountCameras()
    36. {
    37. int camera_count = QCameraInfo::availableCameras().count();
    38. tracemsg(tr("Number of cameras connected = %1").arg(camera_count));
    39. for(int i = 0; i < camera_count;i++)
    40. {
    41. QCameraInfo caminfo = QCameraInfo::availableCameras().at(i);
    42. tracemsg(tr("device name = %1").arg(caminfo.deviceName()));
    43. tracemsg(tr("description = %1").arg(caminfo.description()));
    44.  
    45. }
    46. }
    47.  
    48. // adaptation of code in http://doc.qt.io/qt-5/cameraoverview.html
    49.  
    50. void MainWindow::slotViewfinder1()
    51. {
    52. m_QCamera = new QCamera;
    53. m_QCamera->setCaptureMode(QCamera::CaptureStillImage);
    54. m_QCameraViewfinder = new QCameraViewfinder;
    55. m_QCamera->setViewfinder(m_QCameraViewfinder);
    56. m_QCameraViewfinder->show();
    57. m_QCamera->start(); // to start the viewfinder
    58. }
    59.  
    60. void MainWindow::slotCaptureImage1()
    61. {
    62.  
    63. if(m_QCameraImageCapture)
    64. delete m_QCameraImageCapture;
    65. m_QCameraImageCapture = new QCameraImageCapture(m_QCamera);
    66. m_QCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    67. tracemsg(tr("Codec used = %1").arg(m_QCameraImageCapture->encodingSettings().codec()));
    68.  
    69. connect(m_QCameraImageCapture,SIGNAL(imageCaptured(int,QImage)),this, SLOT(slotSaveImage1(int,QImage)));
    70. if(m_QCameraImageCapture->isAvailable())
    71. {
    72. m_QCameraImageCapture->capture("");
    73. }
    74. else
    75. {
    76. tracemsg(tr("Capture not available"));
    77. }
    78.  
    79. }
    80.  
    81. void MainWindow::slotCaptureImage2()
    82. {
    83.  
    84. if(m_QCameraImageCapture)
    85. delete m_QCameraImageCapture;
    86.  
    87. m_QCameraImageCapture = new QCameraImageCapture(m_QCamera);
    88. m_QCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    89. tracemsg(tr("Codec used = %1").arg(m_QCameraImageCapture->encodingSettings().codec()));
    90. if(m_QCameraImageCapture->isAvailable())
    91. {
    92. m_QCameraImageCapture->capture("Portrait2.jpg");
    93. }
    94. else
    95. {
    96. tracemsg(tr("Capture not available"));
    97. }
    98. }
    99.  
    100. void MainWindow::slotSaveImage1(int id, QImage img)
    101. {
    102. tracemsg(tr("id save image received %1").arg(id));
    103. img.save(tr("Portrait1.jpg"),"JPEG") ;
    104. }
    105.  
    106. void MainWindow::slotDialogViewer()
    107. {
    108. m_DialogViewer->show();
    109. }
    To copy to clipboard, switch view to plain text mode 
    Here is the code of DialogViewer
    Qt Code:
    1. #include <QCamera>
    2. #include <QMessageBox>
    3. #include <QCameraViewfinder>
    4. #include <QCameraImageCapture>
    5.  
    6. #include "dialogviewer.h"
    7. #include "ui_dialogviewer.h"
    8.  
    9. DialogViewer::DialogViewer(QWidget *parent) :
    10. QDialog(parent),
    11. ui(new Ui::DialogViewer),
    12. m_QCamera(NULL),
    13. m_QCameraImageCapture(NULL),
    14. m_QCameraViewfinder(NULL)
    15. {
    16.  
    17. ui->setupUi(this);
    18. QSizePolicy policy(QSizePolicy::Preferred,QSizePolicy::Preferred,QSizePolicy::DefaultType);
    19.  
    20. m_QCamera = new QCamera(this);
    21. m_QCameraViewfinder = new QCameraViewfinder(this);
    22. m_QCameraViewfinder->setBaseSize(600,800);
    23. m_QCameraViewfinder->setSizePolicy(policy);
    24. m_QCamera->setViewfinder(m_QCameraViewfinder);
    25. ui->verticalLayout->addWidget(m_QCameraViewfinder);
    26. m_QCamera->start(); // to start the viewfinder
    27.  
    28. }
    29. void DialogViewer::tracemsg(QString s)
    30. {
    31. QMessageBox::critical(this,tr("error"),s);
    32. }
    33.  
    34. DialogViewer::~DialogViewer()
    35. {
    36. delete ui;
    37. }
    38.  
    39. void DialogViewer::on_btCapture_clicked()
    40. {
    41. m_QCameraImageCapture = new QCameraImageCapture(m_QCamera);
    42. m_QCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    43.  
    44. //connect(m_QCameraImageCapture,SIGNAL(imageCaptured(int,QImage)),this, SLOT(slotSaveImage1(int,QImage)));
    45. if(m_QCameraImageCapture->isAvailable())
    46. {
    47. QString file("portrait_from_dialog.jpg");
    48. m_QCameraImageCapture->capture(file);
    49. }
    50. else
    51. {
    52. tracemsg(tr("Capture not available"));
    53. }
    54. }
    55.  
    56. void DialogViewer::on_stop_clicked()
    57. {
    58. close();
    59. }
    To copy to clipboard, switch view to plain text mode 
    For completeness here is Mainwindow.h
    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. namespace Ui {
    4. class MainWindow;
    5. }
    6.  
    7. class QCamera;
    8. class QCameraViewfinder;
    9. class QCameraImageCapture;
    10. class DialogViewer;
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19. private slots:
    20. void tracemsg(QString s);
    21. void slotCountCameras();
    22. void slotViewfinder1();
    23. void slotCaptureImage1();
    24. void slotCaptureImage2();
    25. void slotSaveImage1(int id, QImage img);
    26. void slotDialogViewer();
    27.  
    28. private:
    29. Ui::MainWindow *ui;
    30. QCamera *m_QCamera;
    31. QCameraViewfinder *m_QCameraViewfinder;
    32. QCameraImageCapture *m_QCameraImageCapture;
    33. DialogViewer *m_DialogViewer;
    34. };
    To copy to clipboard, switch view to plain text mode 
    The code of Dialogviewer.h
    Qt Code:
    1. #include <QDialog>
    2. class QCamera;
    3. class QCameraViewfinder;
    4. class QCameraImageCapture;
    5. namespace Ui {
    6. class DialogViewer;
    7. }
    8.  
    9. class DialogViewer : public QDialog
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit DialogViewer(QWidget *parent = 0);
    15. ~DialogViewer();
    16.  
    17. private slots:
    18. void on_btCapture_clicked();
    19.  
    20. void on_stop_clicked();
    21.  
    22. private:
    23. Ui::DialogViewer *ui;
    24. QCamera *m_QCamera;
    25. QCameraViewfinder *m_QCameraViewfinder;
    26. QCameraImageCapture *m_QCameraImageCapture;
    27. void tracemsg(QString s);
    28. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by feraudyh; 15th July 2017 at 20:52. Reason: presentation

Similar Threads

  1. How to mirror the image in QCameraViewFinder?
    By Violet Giraffe in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2016, 00:00
  2. QCameraViewfinder aspect ratio
    By gpuckett in forum Qt Programming
    Replies: 0
    Last Post: 29th May 2014, 19:52
  3. Replies: 1
    Last Post: 9th March 2011, 22:50
  4. Replies: 2
    Last Post: 8th February 2011, 18:07
  5. QPalette works differently on windows and linux
    By babu198649 in forum Newbie
    Replies: 3
    Last Post: 6th March 2008, 07:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.