[Qt-interest] Convert an array to a JPEG

Robert Wood robert.wood at apostrophe.co.uk
Fri Sep 2 13:19:08 CEST 2011


I will say first off I am not asking for JPEG expertise, so bear with me!

I have written a program to connect to my Oscilloscope. I can control it 
remotely and get information back from the scope. I have successfully 
managed to get various bits o data such as rise times, frequency of 
signal. So, I know my basic code works.

I have downloaded a JPEG and have it sitting in a biggish array of 
ViChar which I believe is just a normal eight bit char. I have then 
opened a file and used QTextStream to send these bytes out, one at a 
time into a .jpeg file.

Now, when I try to open the file it tell me there is an error. The GIMP 
briefly gives me this:

Quantisation table 0x01 was not defined

When I look at the files with a hex editor it looks very close to what I 
want - certainly all the exif data is there at the start of the file, 
but the file is three times the size of a JPEG I have saved from the 
scope to a memory stick.

My code is here:

status = viRead(vi, (ViBuf)JPEGbuffer, sizeof(JPEGbuffer), &retCnt);

     if (status < VI_SUCCESS )
         return -1;

     QFile file("tek.jpeg");
     if (file.open( QIODevice::WriteOnly))
         {
         QTextStream stream( &file  );
         stream.setFieldWidth(oct);

         for (i=0;i<retCnt;i++)
             stream << JPEGbuffer[i];
         file.close();
         return 1;
         }
     else
         return -1;


So, the viRead gets the JPEG via USB and is calling an API provided by 
Tektronix, who make the scope. retcnt is the number of bytes returned.

I suspect I am close because the start and end of the file look 
identical, but I am wondering whether I need to stream out the bytes 
using a [slightly] different method. Should I somehow specify I am 
streaming chars?

The documentation talks about "several global manipulator functions" and 
defines things like oct for eight bit data, but I can't work out how to 
set this to even try!

There is a 	setFieldWidth ( int width ), but I don't really understand 
what this does and whether this might help. I have tried setting it, but 
it just gives me errors if I try things like:

stream.setFieldWidth(oct);

Most grateful for any pointers!









More information about the Qt-interest-old mailing list