Converting an image to text - qt

I want to be able to save an image as text in a xml file and I can't manage to find a efficient way to do it !
So far I tried :
QByteArray ImageAsByteArray;
QBuffer ImageBuffer(&ImageAsByteArray);
ImageBuffer.open(QIODevice::WriteOnly);
rImage.save(&ImageBuffer, "PNG");
return QString(ImageAsByteArray.toBase64());
Despite the fact it's working, the result is a file that is huge ! I tried adding some QCompress in there but without much success... Actually the QCompress doesn't seem to compress anything...
I think I'm doing it the wrong way, but could someone enlight my path please ?

Are you loading the image file to QImage and then getting the bytes from that QImage? If yes, then you are base64 encoding the raw image. In that case it really doesn't matter at all how much the original image file is compressed.
You should read the original image file (png or jpg) as a binary stream and base64 encode that stream. Example:
QFile* file = new QFile("Image001.jpg");
file->open(QIODevice::ReadOnly);
QByteArray image = file->readAll();
int originalSize = image.length();
QString encoded = QString(image.toBase64());
int encodedSize = encoded.size();
My test image's originalSize is 1028558 bytes, and encodedSize is 1371412 bytes, which is 33% more than the originalSize (see Jérôme's comment to your question).

Related

Reduce WAV file size using cscore library

I want to reduce WAV file using cscore libary without loosing audio, something like NAudio
var pcmStream = WaveFormatConversionStream.CreatePcmStream(inputSteam);
var wavCompressedStream = new WaveFormatConversionStream(target, pcmStream);
You can change the format of a wave file with by opening the existing audio data as IWaveSource, changing its format and writing it back to any stream or file.
For example:
IWaveSource waveSource = ...
waveSource
.ToMono() //for example, convert to mono
.ChangeSampleRate(44100) // specify samplerate
.ToSampleSource()
.ToWaveSource(16) //specify bits per sample
.WriteToWavStream(stream);
But keep in mind that you probably will loose audio quality by downsampling, changing bits per sample, ...

corrupt data in decode base64 to image in Qt

I write a program decode the base64 string to image. I wrote a sample:
QFile file("./image.jpg");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QByteArray raw = file.readAll().toBase64();
QImage = image;
image.loadFromData(QByteArray::fromBase64(raw), "JPG");
image.save("output.jpg", "JPG");
The output of the program is:
Corrupt JPEG data: 65 extraneous bytes before marker 0xc0
Quantization table 0x01 was not defined
I can't find something useful with google. I only read image file, and encode it with base64, then decode it. Could you tell me what's wrong with my code?
I have figured out what's wrong with my code. When i open a image file, i use the QIODevice::Text open mode. But the image is a binary file, so i should remove the QIODevice::Text option. After do that, the code run well.

Load raw memory blob in DPX format with Graphicsmagick

I have a library that generates a Big Endian 10-bit DPX image in a memory buffer. It's just the raw 10-bit RGB data, though, with no headers. I'm trying to load this data into an instance of Magick::Image like this:
Magick::Blob blob(dataBuffer, dataBufferSize;
image.read(blob, Magick::Geometry(width, height), 10 /*bits*/, "DPX");
This throws the following exception, though: Magick: Improper image header ()
Is it possible to load a raw DPX into a Magick::Image?
I don't think that your answer is a good one. It it is working by accident. Your blob data is likely to be in some other format than DPX. Specifying 'SDPX' (an unsupported format specification) allowed the file format detection to automatically work and select the correct format.
Using
enter code herMagick::Blob blob(dataBuffer, dataBufferSize);
image.read(blob);
should then be sufficient. Most image file formats do not require specifying the format or the depth.
Figured out my own answer here. I took a look at the DPX loading source and found out for this case this line:
image.read(blob, Magick::Geometry(width, height), 10 /*bits*/, "DPX");
should be:
image.read(blob, Magick::Geometry(width, height), 10 /*bits*/, "SDPX");

Qt drag drop UNC path

when dragging a file from path
\\Nearlinestorage\anc\sequences\anc_SH_005\2d\cgRenders\Maya_Files\28.07.11
in lineEdit area it cuts \\nearlinestorage when reading ui->lineedit->text()
how is it possible to get the whole path
solved. Problem is in the droparea::dropevent(QDropEvent *event).
I replaced
QString url = urlList.at(i).path();
with
QString url = urlList.at(i).toLocalFile();
I get proper result ( as expected ).
Make sure the QLineEdit doesn't have a minimum number of characters set.

Constructing QImage from unsigned char* data

I encountered a problem with passing Image object (captured with Point Grej FlyCapture2 SDK) to QImage object. I am getting a pointer associated with Image data by function:
virtual unsigned char* FlyCapture2::GetData ( )
and then loading the data by:
QImage::QImage ( uchar * data, int width, int height, int bytesPerLine, Format format )
Formats of data of both Image objects are 8-bit monocolor. BytesPerLine parameter should be equal to width of the Image (I've already checked it by saving FlyCapture2::Image to .bmp and then loading it to QImage).
Do you thing the problem is casting from unsigned char* to uchar*?
Do you have any other ideas? Copying image pixel by pixel is much too slow.
EDIT: I am converting Image captured by FlyCapture into the FlyCapture2::PIXEL_FORMAT_RGB8 , for which: R = G = B = 8 bits, within PGR::SnapShot() function. SnapShot() returns unsigned char* const.
and here is a part of my Qt display function:
unsigned char *const img = PGRSystem->SnapShot();
QImage Img(img, 1024, 768, QImage::Format_RGB888);
QGraphicsScene *Scene = new QGraphicsScene();
Scene->addPixmap(QPixmap::fromImage(Img));
ui.ImageView->setScene(Scene);
ui.ImageView->fitInView(ui.ImageView->itemAt(100,100));
delete [] Scene;
I also tried to save Img to file, but got unhandled exception then. I tried other pixel format pairs (FlyCapture2::PIXEL_FORMAT_RGB - 24 bit RGB with QImage::RGB888 and FlyCapture2::PIXEL_FORMAT_RGBU32 with QImage::RGB32)
It is also worth to mention that QImage constuctor, which I am using, does not set the colorTable (I am getting exception when checking if QImage is in grayScale).
I need a little more help I guess.
First thing - QImage doesn't support a native greyscale image, which is what it sounds as if you're getting as output - so I would be curious what Format argument you're using. Probably the easiest solution, though memory-inefficent, will be to expand your greyscale image to RGB by copying each value three times (into a new QByteArray).
An additional concern is that the particular QImage constructor you're using, does not copy the underlying data, so you need be sure the pointer returned from GetData() outlives the QImage - or force the QImage to make a copy internally, using, say, QImage::copy.
Seeing more code would help, as other respondents noted above.
Thanks a lot for your help, you were right about the Image formats. Unfortunatelly the main problem was associated with passing the pointer between functions. In PGR::SnapShot() I was creating FlyCapture2::Image and then I was getting the pointer to data. FlyCapture2::Image was detructed when exit the function so returned pointer was BadPtr.

Resources