将图像转换为文本
我希望能够将图像另存为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否将图像文件加载到 QImage,然后从该 QImage 获取字节?如果是,那么您正在对原始图像进行 Base64 编码。在这种情况下,原始图像文件的压缩程度根本不重要。
您应该将原始图像文件(png 或 jpg)作为二进制流读取,并对该流进行 Base64 编码。示例:
我的测试图像的
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:
My test image's
originalSize
is 1028558 bytes, andencodedSize
is 1371412 bytes, which is 33% more than the originalSize (see Jérôme's comment to your question).