How can I create image from unsigned char pointer?
-
wrote on 18 Dec 2018, 10:47 last edited by
I have a struct like this
struct RGBImage { unsigned char *data; size_t width; size_t height; size_t size; };
And v4l2 functions to take a picture from camera. When I take a picture and save it to a file its ok but instead of saving image to file I wan't to display image on
QGraphicsScene
. Here is my code for it:auto frame = webcam.frame(); // get picture from camera in RGBImage format image = new QImage((char *) frame.data); QPixmap pixmap = QPixmap::fromImage(*image); if (pixmap.isNull()) qDebug()<<"pixmap is null"; QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap); scene->addItem(item);
But when I run this program I see that pixmap is null. How can I fix this problem? Or is there a better way to convert
unsigned char *
to image ? -
I have a struct like this
struct RGBImage { unsigned char *data; size_t width; size_t height; size_t size; };
And v4l2 functions to take a picture from camera. When I take a picture and save it to a file its ok but instead of saving image to file I wan't to display image on
QGraphicsScene
. Here is my code for it:auto frame = webcam.frame(); // get picture from camera in RGBImage format image = new QImage((char *) frame.data); QPixmap pixmap = QPixmap::fromImage(*image); if (pixmap.isNull()) qDebug()<<"pixmap is null"; QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap); scene->addItem(item);
But when I run this program I see that pixmap is null. How can I fix this problem? Or is there a better way to convert
unsigned char *
to image ?@onurcevik What is the format of the picture you get?
-
I have a struct like this
struct RGBImage { unsigned char *data; size_t width; size_t height; size_t size; };
And v4l2 functions to take a picture from camera. When I take a picture and save it to a file its ok but instead of saving image to file I wan't to display image on
QGraphicsScene
. Here is my code for it:auto frame = webcam.frame(); // get picture from camera in RGBImage format image = new QImage((char *) frame.data); QPixmap pixmap = QPixmap::fromImage(*image); if (pixmap.isNull()) qDebug()<<"pixmap is null"; QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap); scene->addItem(item);
But when I run this program I see that pixmap is null. How can I fix this problem? Or is there a better way to convert
unsigned char *
to image ?@onurcevik the constructor of QImage that accepts (only) a datapointer expects a xpm image are you sure your camera image qualifies, if not this fails silently.
Also, are you sure about the life time of the data, afaik QImage does not make deep copy of the underlying data, I run into this when I created a QImage from a local/temporary QByteArray. -
@onurcevik What is the format of the picture you get?
-
@onurcevik the constructor of QImage that accepts (only) a datapointer expects a xpm image are you sure your camera image qualifies, if not this fails silently.
Also, are you sure about the life time of the data, afaik QImage does not make deep copy of the underlying data, I run into this when I created a QImage from a local/temporary QByteArray. -
@onurcevik you'll have to us one of the other constructors for example:
http://doc.qt.io/qt-5/qimage.html#QImage-3
but, the function will need more information, width & height as well as the expected format : http://doc.qt.io/qt-5/qimage.html#Format-enum
1/6