将图像转换为文本

发布于 2024-09-18 16:33:59 字数 412 浏览 11 评论 0原文

我希望能够将图像另存为 xml 文件中的文本,但我无法找到有效的方法来做到这一点!

到目前为止,我尝试过:

QByteArray ImageAsByteArray;
QBuffer ImageBuffer(&ImageAsByteArray);
ImageBuffer.open(QIODevice::WriteOnly);
rImage.save(&ImageBuffer, "PNG"); 

return QString(ImageAsByteArray.toBase64());

尽管它正在工作,但结果是一个巨大的文件!我尝试在其中添加一些 Qcompress,但没有取得太大成功...实际上 Qcompress 似乎没有压缩任何东西...

我认为我做错了,但是有人可以照亮我的道路吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-09-25 16:33:59

您是否将图像文件加载到 QImage,然后从该 QImage 获取字节?如果是,那么您正在对原始图像进行 Base64 编码。在这种情况下,原始图像文件的压缩程度根本不重要。

您应该将原始图像文件(png 或 jpg)作为二进制流读取,并对该流进行 Base64 编码。示例:

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();

我的测试图像的 originalSize 是 1028558 字节,encodedSize 是 1371412 字节,比原始大小多 33%(请参阅 Jérôme 对您问题的评论)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文