qimage loadfromdata not of load我的图像的数据

发布于 2025-02-05 16:12:59 字数 2029 浏览 1 评论 0原文

我是QT的新手,我一直在尝试开发一个简单的视频流应用程序。

在开发我的应用程序时,我一直面临着一个我认为一开始可能会忽略的问题,但事实并非如此。实际上,这个问题正在减慢我的整个应用程序,并且使其无法正常工作。

这是我的问题:当我尝试显示许多图像时,我有2个选择:

  1. 使用qimage构造函数qimage(uchar *data,int width,int height,qimage ::格式格式)来构造图像并显示。它按预期工作,但非常非常慢

  2. 或用qimage :: loadfromdata(uchar *,int len)加载图像的数据,这根本不起作用。

这是我的工作代码:

void MainWindow::startCapturing()
{
  timer->start();
  // on press of a button, the timer will call repeatedly the generateImage() function
}

void MainWindow::generateImage()
{
  det->GetImage(&image);    // fulfill my image with data

  myImage=QImage((uchar*)image.GetValuePointer(),image.GetSize().height,image.GetSize().width, 
  QImage::Format_Grayscale16);
  // here GetValuePointer returns a void *, that's why we had to cast it.

  ui->img_label->setPixmap(QPixmap::fromImage(myImage));
}

不工作代码

void MainWindow::startCapturing2()
{
   
  myImage=QImage((uchar*)image.GetValuePointer(),image.GetSize().height,image.GetSize().width, 
  QImage::Format_Grayscale16);

  ui->img_label->setPixmap(QPixmap::fromImage(myImage));
  timer->start();
  // on press of a button, the timer will call repeatedly the generateImage() function
}

void MainWindow::generateImage2()
{
   det->GetImage(&image);

   myImage.loadFromData((uchar *)image.GetValuePointer(), image.GetSizeInBytes());
   // same cast of GetValuePointer as above

   ui->img_label->setPixmap(QPixmap::fromImage(myImage));
}

这是我对代码的

  • 信息:

    det-> getimage(& image)在此处符合我的映像对象。

  • 图像是我自己的类型,因为我正在使用特定的摄像头,必须这样做,它更多地在硬件方面,不要打扰。

  • image.getValuePointer()将图像的数据返回,作为void *的数据。

  • 也不要打扰timer()和startcaptring(),它在起作用,这不是主题的重点。

  • 在startcapturing2中,我正在尝试创建qimage,并设置pixmap,以便如果我使用loadfromdata更新myImage,它将更改显示的图像。

例如,我一直在尝试很多事情,例如其中的很多东西,例如使用QBYTEARRAY,但它要么不起作用,要么根本无法做到。

如果您有任何疑问,请问他们,我会尽力而为地回答!谢谢!

I'm new to Qt and I have been trying to develop a simple video streaming application.

While developing my app, I've been facing a problem I thought could be ignored at first, but that's not the case. In fact, this problem is slowing down my whole application and is making it not working.

Here's my problem: When I'm trying to display many images I have 2 choices:

  1. Using the QImage constructor QImage(uchar *data, int width, int height, QImage::Format format) to construct the image and display it. It's working as intended but is VERY, VERY slow

  2. Or load the data of the images with QImage::loadFromData(uchar *, int len), which is simply not working.

Here's my working code:

void MainWindow::startCapturing()
{
  timer->start();
  // on press of a button, the timer will call repeatedly the generateImage() function
}

void MainWindow::generateImage()
{
  det->GetImage(&image);    // fulfill my image with data

  myImage=QImage((uchar*)image.GetValuePointer(),image.GetSize().height,image.GetSize().width, 
  QImage::Format_Grayscale16);
  // here GetValuePointer returns a void *, that's why we had to cast it.

  ui->img_label->setPixmap(QPixmap::fromImage(myImage));
}

Here's my not working code

void MainWindow::startCapturing2()
{
   
  myImage=QImage((uchar*)image.GetValuePointer(),image.GetSize().height,image.GetSize().width, 
  QImage::Format_Grayscale16);

  ui->img_label->setPixmap(QPixmap::fromImage(myImage));
  timer->start();
  // on press of a button, the timer will call repeatedly the generateImage() function
}

void MainWindow::generateImage2()
{
   det->GetImage(&image);

   myImage.loadFromData((uchar *)image.GetValuePointer(), image.GetSizeInBytes());
   // same cast of GetValuePointer as above

   ui->img_label->setPixmap(QPixmap::fromImage(myImage));
}

INFORMATIONS ABOUT THE CODE:

  • det->GetImage(&image) fulfill my Image object named image here.

  • Image is my own type because I'm working with specific cameras and had to do so, it's more on the hardware side, don't bother with that.

  • image.GetValuePointer() returns the data of the image as a void * to be cast in anything you want.

  • don't bother with timer() and startCapturing() as well, it's working, that's not the point of the topic.

  • in StartCapturing2, I'm trying to create the QImage, and set the pixmap so that if I update myImage with loadFromData, it will change the image displayed.

I've been trying many things, like a lot of them, using a QByteArray for example but it's either not working or simply not what I want.

If you have any question, please ask them I will try to answer as fast and best as I can! Thanks!

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

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

发布评论

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

评论(1

绿萝 2025-02-12 16:12:59

您尚未为格式 loadfromdata() (默认值为nullptr),因此很可能qimage尝试找到图像标头,找不到任何并放弃,无法处理以下数据。

加载程序尝试使用指定的格式,例如,PNG或JPG读取图像。如果未指定格式(这是默认的),则加载程序将探测标题以猜测文件格式的文件。


附带说明,您的第二个选项可能比第一个选项慢,因为它复制数据时,第一个选项使用图像中的现有数据。参见构造函数文档(强调):


qimage ::格式,
qimagecleanupfunction callupfunction = nullptr,
void *clearupinfo = nullptr)

构造使用现有读取的内存缓冲区的图像[...] data

You haven't provided any value for the format parameter for loadFromData() (default value is nullptr) so most likely QImage tries to find a image header, doesn't find any and gives up, not being able to handle the following data.

The loader attempts to read the image using the specified format, e.g., PNG or JPG. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.


On a side note, your second option might be even slower than the first because it copies the data while the first one uses the existing data from image. See constructor documentation (emphasis mine):

QImage::QImage(const uchar *data, int width, int height,
               QImage::Format format,
               QImageCleanupFunction cleanupFunction = nullptr,
               void *cleanupInfo = nullptr)

Constructs an image [...] that uses an existing read-only memory buffer, data.

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