I am using lodePNG library to encode and decode png files. The library take std:vector<unsigned char> image as an input:
unsigned encode(std::vector<unsigned char>& out,
const std::vector<unsigned char>& in, unsigned w, unsigned h,
LodePNGColorType colortype, unsigned bitdepth)
unsigned encode(std::vector<unsigned char>& out,
const std::vector<unsigned char>& in, unsigned w, unsigned h,
LodePNGColorType colortype, unsigned bitdepth)
To copy to clipboard, switch view to plain text mode
I need to print screen and encode it. Therefore, I asked you how to convert QPixmap to std::vector<unsigned char>.
Furthermore, I have written the following code so far (the problem of getting null was corrected once I specified the image type, thanks Chris)
int width = 400;
int height = 300;
qbuffer.setBuffer(&ba);
image->save(&qbuffer, "PNG");
int size = rawPixels.size();
char* temp = (char*) rawPixels.constData();
std::vector<unsigned char> imageVector;
imageVector.resize(size);
for(int index = 0; index < size; index++)
{
imageVector.push_back(temp[index]);
}
/* compress PNG */
std::vector<unsigned char> png;
png.resize(size);
unsigned error = lodepng::encode(png, imageVector, width, height);
if(error) qDebug() << "encoder error " << error << ": "<< lodepng_error_text(error);
int width = 400;
int height = 300;
QPixmap originalPixmap = QPixmap::grabWidget(this);
QImage *image = new QImage(originalPixmap.toImage());
QByteArray ba;
QBuffer qbuffer;
qbuffer.setBuffer(&ba);
qbuffer.open(QIODevice::WriteOnly);
image->save(&qbuffer, "PNG");
QByteArray rawPixels = qbuffer.buffer();
int size = rawPixels.size();
char* temp = (char*) rawPixels.constData();
std::vector<unsigned char> imageVector;
imageVector.resize(size);
for(int index = 0; index < size; index++)
{
imageVector.push_back(temp[index]);
}
/* compress PNG */
std::vector<unsigned char> png;
png.resize(size);
unsigned error = lodepng::encode(png, imageVector, width, height);
if(error) qDebug() << "encoder error " << error << ": "<< lodepng_error_text(error);
To copy to clipboard, switch view to plain text mode
that's all.
Bookmarks