如何使用libcurl保存图像
How to save image using libcurl
我想使用libcurl的一个项目,涉及从网页获取图像。URL看起来像这样:
http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg使用命令行cURL可以使用
检索图像$curl -o sampleimage.jpg http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg
我想知道这段代码在libcurl中的等价,因为我现在快疯了。我在网上找到了这个示例源代码,它可以编译,但是我在任何地方都看不到图像文件。
这是代码:
#include <iostream>
#include <curl/curl.h>
#include <stdio.h>
using namespace std;
int main(){
CURL *image;
CURLcode imgresult;
FILE *fp;
image = curl_easy_init();
if( image ){
// Open file
fp = fopen("google.jpg", "wb");
if( fp == NULL ) cout << "File cannot be opened";
curl_easy_setopt(image, CURLOPT_URL, "http://192.168.16.25/cgi-bin/viewer/video.jpg");
curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
// Grab image
imgresult = curl_easy_perform(image);
if( imgresult ){
cout << "Cannot grab the image!n";
}
}
// Clean up the resources
curl_easy_cleanup(image);
// Close the file
fclose(fp);
return 0;
}
顺便说一句,我用的是Mac电脑,我在XCode上编译这段代码,XCode有一个libcurl库。
* 编辑: *固定问题。我只是为fopen()使用了一个完整的路径。谢谢垫!请回答问题,以便我选择你的作为正确答案。谢谢!
在open call中使用完整路径,以便您知道在哪里查找。
你还应该看看perror
函数,这样你就可以打印出打开失败的原因——节省了一些麻烦。
最后一件事:初始化fp
为null,或者只有fclose(fp)
,如果它真的是打开的。目前,如果curl_easy_init
失败,您将尝试fclose
一个随机指针。
相关文章:
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- 是否可以通过每次在内存中仅保存一个平铺来处理完整的平铺 tiff 图像?
- 将图像魔术 blob 保存到节点缓冲区
- 是否可以将图像列表保存到文件中?
- 在opencv c++目录中从/加载/保存多个图像
- 将 JPG 编码数组从 ROS sensor_msgs/压缩图像保存到 roscpp 中的文件
- 我想将 OpenCV 中处理后的图像保存为“input_name+enhanced.input_image_format
- 无法将图像保存在具有白色背景的JPG中 OpenCV
- c++:尝试将创建的图像保存到.jpg文件,但图像无法打开
- 将许多图像保存到矢量中,但它们不显示
- QT:将整个QSCrollarea内容作为图像保存
- 打开CV图像保存以较小的尺寸,无需压缩
- 将图像保存到内存
- Boost::GIL如何将图像保存为JPEG或PNG为char*
- c++ 32位tiff图像保存和加载
- 如何将图像保存为字符串的c++源代码
- 使用opencv将图像保存为ycbcr格式