Byte array to QImage
-
I am trying to form a QImage from the Byte array and save the image. Below is the code:
BYTE* pBuf=(BYTE*)malloc(iBytes*sy);
I have copied all the image data in this pBuf from the camera (not copying that code)QImage img(1200,1920,QImage::Format_Mono); img = QImage::fromData((uchar*)pBuf,iBytes*sy,"JPG"); img.save( "C:\\test_folder\\file.JPG" );
After running this code, a file.JPG is saved but it is not a valid file. When I am trying to open this file the system is displaying the message "it looks like we don't support this format".
How can I solve this issue? -
what does QtGui.QImageWriter.supportedImageFormats() report? That should tell you something.
-
Do you really think the camera is sending jpeg images?
-
I am trying to form a QImage from the Byte array and save the image. Below is the code:
BYTE* pBuf=(BYTE*)malloc(iBytes*sy);
I have copied all the image data in this pBuf from the camera (not copying that code)QImage img(1200,1920,QImage::Format_Mono); img = QImage::fromData((uchar*)pBuf,iBytes*sy,"JPG"); img.save( "C:\\test_folder\\file.JPG" );
After running this code, a file.JPG is saved but it is not a valid file. When I am trying to open this file the system is displaying the message "it looks like we don't support this format".
How can I solve this issue?@hardikgarg19
Maybe the set format is wrong. TryFormat_Grayscale8
orFormat_RGB32
.Or try to leave the format blank, so QImage will detect the reading format by itself.
QImage::fromData((uchar*)pBuf, iBytes*sy);
All described in the docs
-
Hi,
Besides the points of my fellows, are you sure about the size of the image ? 1200x1920 seems a bit non standard for a camera.
On that point, what camera is that ?
How do you get data from it ?
How did you configure it ? -
@hardikgarg19
Maybe the set format is wrong. TryFormat_Grayscale8
orFormat_RGB32
.Or try to leave the format blank, so QImage will detect the reading format by itself.
QImage::fromData((uchar*)pBuf, iBytes*sy);
All described in the docs
@beecksche
I have tried the change, but, I am getting the bool value as false and no image is created.QImage img(1200,1920,QImage::Format_Grayscale8); img = QImage::fromData((uchar*)pBuf,iBytes*sy); bool b = img.save( "C:\\test_folder\\file_img.BMP");
-
You really should try to find out what format the camera is sending - guessing is not the correct solution here.
-
I'm curious about the "iBytes * sy" in your code.
If you are truly grabbing an 8 bit gray scale image from the camera then the size of the data will likely be just "iBytes" which, and I'm going out on a limb here, should be imageWidth * imageHeight.
If you are multiplying this by some factor, the mysterious "sy", then the image is probably RGB24 (sy == 3 ?) or RGB32 (sy == 4 ?). -
Hi,
Besides the points of my fellows, are you sure about the size of the image ? 1200x1920 seems a bit non standard for a camera.
On that point, what camera is that ?
How do you get data from it ?
How did you configure it ?@SGaist Hi,
I am using a FPGA based camera (http://isgcameras.com/allegro-usb3-family/).Below is the code:
long sx,sy,iBpp; //get the image size
//pMedleyUcam is the camera object
pMedleyUcam->get_SizeX(&sx);
pMedleyUcam->get_SizeY(&sy);pMedleyUcam->GetBytesPerPixel(&iBpp);
int iBytes=sx*iBpp; // horizontal image size in bytesBYTE* pBuf=(BYTE*)malloc(iBytes*sy); short v; pMedleyUcam->Grab(&v); BYTE *ptr=pBuf; for(int y=0;y<sy;y++) { pMedleyUcam->GetImagePointer(0, sy-1-y,&var); memcpy(ptr,var.pbVal,iBytes); ptr+=iBytes; }
//form the image using the pBuf
QImage img(1200,1920,QImage::Format_Grayscale8);
img = QImage::fromData((uchar*)pBuf,iBytes*sy);
bool b = img.save( "C:\test_folder\file_img.BMP");b is returning false here
-
You really should try to find out what format the camera is sending - guessing is not the correct solution here.
@Christian-Ehrlicher
The camera allows me to check the available pixel format. Below is the list:
Mono8
Mono10p
Mono12p
BayerRGB
BayerRGB10p
BayerRGB12p
RGB8
RGB565P
BGR8
YCbCr422_8
YUV400_8
YUV8_UYV -
Hi, if you try with double slashes
bool b = img.save( "C:\\test_folder\\file_img.BMP");
is b still returning false?
@hskoglund Hi,
Yes, it is still returning false. -
Looks like you're only getting the raw pixels from the camera (in the specified pixel format). To save to a file, like a BMP file., you need an header as well.
To get one, you could try create an empty QImage with the specified x and y size, and then fill it with those pixels you get from the camera, say something like:QImage img(1200,1920,QImage::Format_Grayscale8); for (int y = 0; (y < 1920); ++y) for (int x = 0; (x < 1200); ++x) img.setPixel(x,y,((uchar*)pBuf) + x + 1200 * y);
-
According to this SO question you can do a line at a time:
https://stackoverflow.com/questions/14563180/raw-data-to-qimage -
Looks like you're only getting the raw pixels from the camera (in the specified pixel format). To save to a file, like a BMP file., you need an header as well.
To get one, you could try create an empty QImage with the specified x and y size, and then fill it with those pixels you get from the camera, say something like:QImage img(1200,1920,QImage::Format_Grayscale8); for (int y = 0; (y < 1920); ++y) for (int x = 0; (x < 1200); ++x) img.setPixel(x,y,((uchar*)pBuf) + x + 1200 * y);
@hskoglund Hi,
Yes, that was the problem. Thank you so much. -
According to this SO question you can do a line at a time:
https://stackoverflow.com/questions/14563180/raw-data-to-qimage@fcarney Hi,
Thank you so much for sharing the link. I have used the QImage.scanLine and it worked.