Solved Byte array to QImage
-
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 ?). -
@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
-
Hi, if you try with double slashes
bool b = img.save( "C:\\test_folder\\file_img.BMP");
is b still returning false?
-
@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 -
@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 -
@hskoglund Hi,
Yes, that was the problem. Thank you so much. -
@fcarney Hi,
Thank you so much for sharing the link. I have used the QImage.scanLine and it worked.