Results 1 to 8 of 8

Thread: How could I get the image buffer of QCamera?

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How could I get the image buffer of QCamera?

    Qt Code:
    1. QByteArray device = QCamera::availableDevices()[0];
    2. QCamera camera(device);
    3. QVideoWidget surface;
    4. surface.resize(320, 240);
    5. camera.setViewfinder(&surface);
    To copy to clipboard, switch view to plain text mode 

    How could I get the image capture by QCamera?
    I want to do some post-processing on the camera video
    QCameraImageCapture is too slow for real time

    Anything similar to following codes?

    Qt Code:
    1. QCamera camera(device);
    2. QImage img = camera.getQImage(); //take the image buffer of camera
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to stereoMatching for this useful post:

    louboutinoutlet (1st December 2013)

  3. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How could I get the image buffer of QCamera?

    You can subclass QAbstractVideoSurface and get the image data from the incoming QVideoFrame object in present method:
    Qt Code:
    1. class MyVideoSurface: public QAbstractVideoSurface{
    2. Q_OBJECT
    3. public:
    4. MyVideoSurface(QObject * parent=NULL) : QAbstractVideoSurface(parent)
    5. {}
    6.  
    7. QList<QVideoFrame::PixelFormat>
    8. supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const{
    9. ...
    10. return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB24 << ...; // here return whatever formats you will handle
    11. }
    12.  
    13. bool present(const QVideoFrame& frame){
    14. if (frame.isValid() && ...) {
    15. QVideoFrame cloneFrame(frame);
    16. cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
    17. const QImage img(cloneFrame.bits(),
    18. cloneFrame.width(),
    19. cloneFrame.height(),
    20. QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
    21.  
    22. // do something with the image ...
    23.  
    24. cloneFrame.unmap();
    25. return true;
    26. }
    27. return false;
    28. }
    29.  
    30. };
    To copy to clipboard, switch view to plain text mode 
    Of course this way you cannot use QVideoWidget as the QCamera object can have only one viewfinder attached - so you'll have to handle the rendering yourself.

  4. The following user says thank you to stampede for this useful post:

    stereoMatching (1st December 2013)

  5. #3
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How could I get the image buffer of QCamera?

    Thanks, but this solution is a little bit verbose(no offense)
    do we have an easier way to get the image?

    besides, do we have to copy the frame before we can
    do anything to it?

  6. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How could I get the image buffer of QCamera?

    this solution is a little bit verbose(no offense)
    None taken I agree that this is quite a lot of code just to get one QImage object.
    besides, do we have to copy the frame before we can do anything to it?
    You can access pixel data from QVideoFrame object only if its mapped - and since map() is non-const method, you cannot call it on a const object.
    Anyway don't worry about the copy constructor, because copy constructor of QVideoFrame creates a shallow copy. When you call map() on the frame object, it may transfer the underlying pixel data to RAM (for example if frame data is stored in video memory), but I think it depends on the implementation - I can imagine for some implementations this pixel data can be already in the main memory. ReadOnly flag probably allows even more optimizations as the mapped pixel data will not be written back to original video frame data, but I'm not really an expert on QCamera / QVideo* stuff so you use my advice with caution
    Furthermore, QImage constructor does not copy the image buffer, so you'll have to copy the image only when you process it.
    Btw. what is your OS and qcamera backend ? I remember using QCamera with gstreamer backend on linux and to be honest I was not very happy with it - I couldn't get it to list possible framerates / resolutions (tested with few uvc cameras), let alone change the capture parameters...
    Last edited by stampede; 1st December 2013 at 18:42. Reason: updated contents

  7. The following user says thank you to stampede for this useful post:

    stereoMatching (2nd December 2013)

  8. #5
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How could I get the image buffer of QCamera?

    Btw. what is your OS and qcamera backend ?
    The OS I intent to deploy are

    OS X 10.8.5(mac mini)
    win 7 64bits
    android 4.1.2(Qt5.2 rc1 have bugs on this platform yet)

    I use the default back end come with the binary
    In truth, I don't know how to change the backend

    At first, I would like to use the cv::VideoCapture to process the image
    but I don't know how to link the camera lib of opencv on android with Qt5
    so I decided to give QCamera a try

    If I am correct, I have to process the image frame by frame in this present function?

  9. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How could I get the image buffer of QCamera?

    Yes, you could do that

  10. The following user says thank you to stampede for this useful post:

    stereoMatching (3rd December 2013)

  11. #7
    Join Date
    Sep 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Android

    Default Re: How could I get the image buffer of QCamera?

    I 've implemented a camera like your code. I want to use QVideoFrame for displaying camera stream on android. I've converted each frame to QImage and then used QPaimter.
    but frames are showed very slowly. here is my code:

    bool MainWindow:resent(const QVideoFrame &frame)
    {

    QVideoFrame cloneFrame(frame);

    if(cloneFrame.map(QAbstractVideoBuffer::ReadOnly))
    {
    img = QImage(cloneFrame.size(), QImage::Format_ARGB32);
    qt_convert_NV21_to_ARGB32(cloneFrame.bits(),
    (quint32 *)img.bits(),
    cloneFrame.width(),
    cloneFrame.height());

    widget->update();

    cloneFrame.unmap();
    }
    return true;
    }

    ....
    void MainWindow:aintEvent(QPaintEvent *event)
    {
    QRectF target(10.0, 20.0, 600.0, 650.0);
    QRectF source(0.0, 0.0, 600.0, 650.0);
    QPainter painter(this);
    painter.begin(&img);
    painter.drawImage(target, img, source);
    painter.end();

    }

  12. #8
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How could I get the image buffer of QCamera?

    You example give me a memory leak, if I use several time.
    Any idea how to fix it?

Similar Threads

  1. [SOLVED] Cannot create Indexed8 image from buffer
    By xenonforlife in forum Newbie
    Replies: 1
    Last Post: 23rd April 2012, 12:53
  2. Set zoomTo for QCamera?
    By tamnv110 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 29th August 2011, 13:54
  3. Replies: 0
    Last Post: 24th January 2011, 11:12
  4. Replies: 2
    Last Post: 29th September 2008, 00:08
  5. OpenGL show image from buffer memory leak[SOLVED]
    By ^NyAw^ in forum Qt Programming
    Replies: 0
    Last Post: 30th January 2008, 16:21

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.