//将整个目录下的PNG图片文件转化成C文件
void CReadImageDataH::GenrateC(QDir dir)
{
unsigned char* imagedatap;
int sum = 0;
int maxvalue = 0;
QStringList ImageListLocal = dir.entryList(QStringList() << "*", QDir::Files | QDir::Readable, QDir::Name);
// QFile filein(filename);
for (int index = 0; index < ImageListLocal.count(); index++)
{
QImage image(dir.path() + "\\" + ImageListLocal[index]);
image.convertTo(QImage::Format_Grayscale8);
imagedatap = image.bits();
QFile fileout(QString("./output/PNG_2_C/%1.c").arg(ImageListLocal[index].remove(".png")));
if (fileout.open(QIODevice::WriteOnly))
{
fileout.write(QString("const unsigned char test_%1[] = { \n").arg(index).toLocal8Bit());
for (int i = 0; i < image.byteCount(); i++)
{
if (i % 16 == 0)
{
fileout.write("\n");
}
fileout.write(QString("0x%1,").arg((ushort)imagedatap[i], 2, 16, QChar('0')).toLocal8Bit());
}
fileout.write("\n};\n\n");
fileout.close();
}
}
}
//将整个目录下.C文件转化为PNG文件
int CReadImageDataH::GenratePNG(QDir dir)
{
int result = E_SUCCESS;
QString dirname = dir.path();
QStringList ImageList = dir.entryList(QStringList() << "*", QDir::Files | QDir::Readable, QDir::Name);
if (ImageList.count() == 0)
{
result = E_FOLDER_EMPTY;
}
else
{
for (int i = 0; i < ImageList.count(); i++)
{
QByteArray imageByteArray = CReadImageDataH::ReadFile(dirname + "\\" + ImageList[i]);
uchar* transData = (unsigned char*)imageByteArray.data();
QImage image2 = QImage(transData, 320, 80, QImage::Format_Indexed8);
image2.save(QString("./output/C_2_PNG/%1.png").arg(ImageList[i].remove(".cpp").remove(".c")), "PNG");
}
}
return result;
}