Getting data from WImage or WFileResource
I can not reach the data of loaded image neither from WImage nor WFileResource.
Any suggestion please.
Did anyone not need that trivial requirement before? I can not understand for what WResouce does not generate byte array from image data. Or I could not see that conversion.
Any help is going to be appreciated so much. I can not resume my project on this part.
Thanks in advance.
Do you mean like pixel data?
WImage corresponds to an
<img> tag. You give it a
WLink, which may or may not happen to be served from the machine that Wt runs on.
If you want access to the actual pixel data there is
WRasterImage, which you could draw the image onto, and then use getData().
Of course, for more advanced use cases, there are also many other libraries that you can use to read and manipulate image files. The one in Wt is kept rather simple, since Wt is still primarily a web framework.
Thank you for your assistance. That's the scenario. User load an image with an action and image show immediately on the screen via WImage and WFileResource pair. There is no problem so far. However the system is going to save that image data to re-show later with WImage and WMemoryResource pair. So, there is a need extract the data of image as you mentioned actual pixel data. I think WMemoryResource initialize with that data "const std::vector< unsigned char >&" I checked the WRasterImage class when I saw the your respond right away. However that class creates a canvas to draw a image. Yes, maybe I'll also need that if WMemoryResource can not create an image with data I will be hopely get.
In brief, I need image data to save of which creates image later.
I solved the problem with ugly solution. However I did not find any other chic solution. But yet, I like to share with you.
I inserted some code the "uploaded" signal belongs to the "Wt::WFileResource" like:
std::ofstream outFile("FILE NAME", std::ios_base::binary); fileResource->write(outFile); outFile.close();
Why here? Because after you set the resource WImage and after Wt::WFileResource object died, you can not reach the data from WImage -> WLink -> WResource. So you have to create a image file with above code.
After then, you can read the file like following code.
std::ifstream imageFile("FILE NAME", std::ios_base::binary); imageFile >> std::noskipws; std::vector<unsigned char> imageData((std::istream_iterator<unsigned char>(imageFile)), (std::istream_iterator<unsigned char>()));
You can re-create image as a resource to set WImage
Wt::WImage *image; ... image->setImageLink(Wt::WLink(std::make_unique<Wt::WMemoryResource>("MIME TYPE", imageData)));
PS: Do not forget the delete image file to avoid filling the disk.
RE: Getting data from WImage or WFileResource - Added by Wim Dumon over 1 year ago
doesn't WFileResource's filename() method return the path to the file that is streamed by the resource? Likewise, WMemoryResource has a data() method?
I did not understand what you did mean indeed. I think you criticize me. However I like to explain myself.
After I select a file from local disk by Wt::WFileUpload, I create a Wt::WFileResource with temporary path, filename and mimetype served by Wt::WFileUpload. I set the Wt::WFileResource to display image into Wt::WImage. No problem so far. However I need the data of image to save further time. Because it is a data which is I am going to use that again later. However Wt::WFileResource can not serve a data as a byte array to keep. Just the abstract class of that Wt::WResource has a method "write" taking std::ostream object. So that is the only solution I found to get data of image. And I use that data to display again with Wt::WMemoryResource.
So, what do you suggest anything else?
PS: Wt::WFileResource can not reach the image again to write a file, after it left the name space of Wt::WFileResource object and Wt::WFileResource object got destroyed.