使用 QImage (qt) 生成文本图像

发布于 2024-12-06 10:09:35 字数 438 浏览 5 评论 0原文

我正在尝试使用 QImage 从文本创建图像。

根据此处的文档: http://doc.qt.io/qt-5/qimage.html #Format-enum

我们不能将 QImage::Format_Indexed8 与 QImage 一起使用。 我无法使用 QImage::Format_MonoQImage::Format_MonoLSB 由于其质量较低。

我的问题是:

  • 创建文本图像(批处理)的最佳方法是什么 我们可以用最小的文件大小获得不错的质量吗?
  • 有没有办法在创建图像后通过QT进行图像压缩以减小文件大小?

I am trying to create images from text using QImage.

As per the documentation here:
http://doc.qt.io/qt-5/qimage.html#Format-enum

We cannot use QImage::Format_Indexed8 with QImage.
I cannot use QImage::Format_Mono or QImage::Format_MonoLSB due to its low quality.

My question is:

  • What is the best way to create textual image (batch processing) so
    that we can get decent quality with minimum file size?
  • Is there is any way to do image compression through QT once the image is created to reduce the file size?

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

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

发布评论

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

评论(2

白云不回头 2024-12-13 10:09:35

下面是执行此操作的示例代码:

QImage image(100, 50, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&image);
painter.fillRect(image.rect(), Qt::yellow);
painter.drawText(image.rect(), Qt::AlignCenter | Qt::AlignVCenter, "hello, world");
image.save("output.png");

它创建此图像:

在此处输入图像描述

输出格式为 PNG,因此它将具有良好的压缩效果而不会损失任何质量。

Here is a sample code that does it:

QImage image(100, 50, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&image);
painter.fillRect(image.rect(), Qt::yellow);
painter.drawText(image.rect(), Qt::AlignCenter | Qt::AlignVCenter, "hello, world");
image.save("output.png");

It creates this image:

enter image description here

The output format is PNG, so it will have good compression without losing any quality.

憧憬巴黎街头的黎明 2024-12-13 10:09:35

这个例子向您展示了如何使用 QPainter::drawText 并使用字体:

http://doc.qt.io/archives/qt-4.7/painting-fontsampler.html

QImage::save支持多种格式和质量级别:

http://doc.qt.io/archives/qt-4.7/qimage.html#reading-and-writing-image-files

虽然QImage在QtCore中, QPainter 和文本绘制例程位于 QtGUI 中。因此,在 Linux 系统上,这需要运行 X 服务器:

http: //www.qtcentre.org/threads/1758-QPainter-in-console-apps

There's this example, which shows you how to use QPainter::drawText and work with fonts:

http://doc.qt.io/archives/qt-4.7/painting-fontsampler.html

QImage::save has support for a variety of formats and quality levels:

http://doc.qt.io/archives/qt-4.7/qimage.html#reading-and-writing-image-files

Although QImage is in QtCore, QPainter and the text drawing routines are in QtGUI. So on a Linux system this will require an X server to be running:

http://www.qtcentre.org/threads/1758-QPainter-in-console-apps

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