将(文本文件)转换为任何格式的图像(png)C++

Converting a ( text file ) into any format of image ( png ) C++

本文关键字:图像 png C++ 格式 文本 文件 转换 任何      更新时间:2023-10-16

有人可以给我一个C++代码,用于将文本文件转换为任何格式的图像,我知道图像没有任何意义,但是出于安全原因,我正在做这件事

有人可以给我一个C++代码,如果没有C++,那么JAVA就可以工作

我真的在网上查了很多,真的什么也没找到

我知道每个人都会说一个人做你的工作,但我真的没时间了请这是严重的如果有人愿意这样做,我什至愿意付钱

提前致谢

我不知道

您希望转换是什么,但是您可以将文本文件中的文本视为无符号字符数组,并将此数据存储为RGB数组BMP或其他无损压缩图像格式。为了加载和保存图像,有很多库(OpenIL/DevIL非常简单和有用)。

这样,图像

没有任何意义,但仍然是有效的图像,而不是仅更改文件扩展名。

编辑:你的运气,我目前很无聊。以下内容可以使用 OpenIL:

#include <fstream>
#include <cmath>
#include <IL/il.h>
void encode(const char *infile, const char *outfile)
{
    std::ifstream in(infile);
    in.seekg(0, std::ios_base::end);
    unsigned int size = in.tellg();
    unsigned int width = (size+6) / 3;
    width = int(sqrt(double(width))) + 1;
    char *data = new char[width*width*3];
    *(unsigned int*)data = size;
    in.seekg(0);
    in.read(data+sizeof(unsigned int), size);
    unsigned int image;
    ilGenImages(1, &image);
    ilBindImage(image);
    ilTexImage(width, width, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, data);
    ilSaveImage(outfile);
    ilDeleteImages(1, &image);
    delete[] data;
}
void decode(const char *infile, const char *outfile)
{
    unsigned int image;
    ilGenImages(1, &image);
    ilBindImage(image);
    ilLoadImage(infile);
    ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
    unsigned char *data = ilGetData();
    unsigned int size = *(unsigned int*)data;
    std::ofstream out(outfile);
    out.write((char*)data+sizeof(unsigned int), size);
    ilDeleteImages(1, &image);
}
int main(int argc, char *argv[])
{
    ilInit();
    ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
    ilEnable(IL_ORIGIN_SET);
    ilEnable(IL_FILE_OVERWRITE);
    encode(argv[1], argv[2]);
    decode(argv[2], argv[3]);
    return 0;
}

编辑:添加了测试程序的完整源代码。在我的 Windows 系统上使用此代码作为输入和 PNG 格式对其进行了测试,它可以工作。

只需将文件扩展名更改为.png或任何您想要的内容即可。 操作系统不知道文件中的内容及其含义;只有打开它时使用什么程序。

如果您需要安全性,我建议您阅读一些有关加密的信息。