[英]How load QImage from path?
I captured an Image with camera in my qt quick application.I want to send the path to my c++ code and load that Image in c++ QImage
.But the Path is image://camera/preview_1
and I don't know How work with that path?我在我的 qt 快速应用程序中用相机捕获了一个图像。我想将路径发送到我的 c++ 代码并在 c++ QImage
加载该图像。但是路径是image://camera/preview_1
我不知道如何使用那条路?
Camera {
id: camera
imageCapture {
onImageCaptured: {
console.log("Preview = "+preview);
photoPreview.source = preview
console.log("CapturedImagePath => "+camera.imageCapture.capturedImagePath);
up.loadImage(preview);
}
}
c++ class C++类
void UserProfile::loadImage(QString path)
{
QUrl imageUrl(path);
qWarning()<<"imageUrl.host()=>"<<imageUrl.host();
qWarning()<<"imageUrl.path()=>"<<imageUrl.path();
qWarning()<<"imageUrl.toLocalFile()=>"<<imageUrl.toLocalFile();
bool isOpend= m_image.load(path); //m_image is an QImage object
qWarning()<<"Image loaded=> "<<isOpend;
}
Application output应用输出
D MyApp: qml: Preview = image://camera/preview_1
D MyApp: qml: CapturedImagePath =>
W MyApp: imageUrl.host()=> "camera"
W MyApp: imageUrl.path()=> "/preview_1"
W MyApp: imageUrl.toLocalFile()=> ""
W MyApp: Image loaded=> false
The URL image://camera/preview_1
means that the image data is living in a QQuickImageProvider instance. URL image://camera/preview_1
表示图像数据存在于QQuickImageProvider实例中。 Probably, it's a QQuickImageProvider instance created by Camera
.可能是由Camera
创建的QQuickImageProvider实例。
As the UserProfile
instance is living in the same QQmlEngine as the camera
lives, you can由于UserProfile
实例与camera
位于同一个 QQmlEngine 中,因此您可以
void UserProfile::loadImage(const QString &path)
{
auto myQmlEngine = qmlEngine(this);
if(myQmlEngine==nullptr)
return;
QUrl imageUrl(path);
auto provider = reinterpret_cast<QQuickImageProvider*>( myQmlEngine->imageProvider(imageUrl.host()));
if (provider->imageType()==QQuickImageProvider::Image){
QImage img = provider->requestImage(imageUrl.path().remove(0,1),nullptr,QSize());
// do whatever you want with the image
}
}
Be careful with the reinterpret_cast
.小心reinterpret_cast
。 You also have to make sure that the QQuickImageProvider::imageType() is returning QQmlImageProviderBase::Image .您还必须确保QQuickImageProvider::imageType()返回QQmlImageProviderBase::Image 。
and you could also use capturedImagePath
instead of the preview
URL to prevent this complexity if it could be an option for your usecase.如果您的用例可以选择它,您也可以使用capturedImagePath
而不是preview
URL来防止这种复杂性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.