HOWTO:实现从http读取并显示图像?

听海拉拉 2010-08-26 11:54:47
HOWTO:实现从http读取并显示图像?
Hi ALL,
比如有一个web site http://127.0.0.1:8080/?action=stream&ignored.mjpg。要从上面读取jpeg的图像,并显示出来。
而且不断的读取,成视频流。
用c++怎么实现呢?或者MFC也行。
谢谢
...全文
212 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
听海拉拉 2010-08-30
  • 打赏
  • 举报
回复
up 自己顶一下
听海拉拉 2010-08-28
  • 打赏
  • 举报
回复
是内存还是什么问题,能显示图像,但有一异常,请帮忙修改。
char url[1000] = "http://192.168.131.129:8080/?action=snapshot";


//cout<<"目标地址 URL:"<<url<<endl;

/*
unsigned char *buffer;
void *pImageMemory;
*/
buffer=new BYTE[100000];


HINTERNET internet= InternetOpen("HTTP Downloader",INTERNET_OPEN_TYPE_PRECONFIG,
NULL,NULL, NULL);

if( !internet )
return ;

HINTERNET file_handle = InternetOpenUrl(internet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0);
if( !file_handle)
return ;

pImageMemory=GlobalLock(buffer);
BOOL b = InternetReadFile(file_handle,pImageMemory,100000,&bytes_read);
GlobalUnlock(pImageMemory); //解锁内存

if(!b)
return;

CDC* pDC = GetDlgItem(IDC_ShowImg) ->GetDC();
HDC hDC = pDC ->GetSafeHdc();
CRect rect;
GetDlgItem(IDC_ShowImg) ->GetClientRect( &rect );



IStream *pIStream;//创建一个IStream接口指针,用来保存图片流
IPicture *pIPicture;//创建一个IPicture接口指针,表示图片对象
CreateStreamOnHGlobal(pImageMemory, false, &pIStream); //用全局内存初使化IStream接口指针
OleLoadPicture(pIStream, 0, false, IID_IPicture, (LPVOID*)&(pIPicture));//用OleLoadPicture获得IPicture接

long hmWidth;
long hmHeight;

pIPicture->get_Width(&hmWidth); //用接口方法获得图片的宽和高
pIPicture->get_Height(&hmHeight);
pIPicture->Render(hDC,0,0,600,400,0,hmHeight,hmWidth,-hmHeight,NULL);



GlobalFree(pImageMemory); //释放全局内存
pIStream->Release(); //释放pIStream
pIPicture->Release(); //释放pIPicture

::DeleteDC(hDC);
pDC->DeleteDC();

InternetCloseHandle(internet);

CDialog::OnPaint();
听海拉拉 2010-08-28
  • 打赏
  • 举报
回复
直接用vs的IStream *pIStream;//创建一个IStream接口指针,用来保存图片流
IPicture *pIPicture;//创建一个IPicture接口指针,表示图片对象
也可以,但内存读写有问题
听海拉拉 2010-08-27
  • 打赏
  • 举报
回复
怎么调用,试过了,出了错
听海拉拉 2010-08-27
  • 打赏
  • 举报
回复
BOOL b = InternetReadFile(file_handle,buffer,100000,&bytes_read);
读进内存了,怎么来显示呢,能否提示一下
mhm0902 2010-08-27
  • 打赏
  • 举报
回复
下载CxImage,加进工程,定义一个CxImage的对象,用对象的Load()函数试试
听海拉拉 2010-08-26
  • 打赏
  • 举报
回复
没有用过CxImage,
能不能帮忙改改
// HttpTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "HttpTest.h"
#include <wininet.h>
#include <highgui.h>
#include <cv.h>


#pragma comment(lib, "Wininet.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object
void Download(char url[]);
CWinApp theApp;



using namespace std;

//BYTE * lpBmpData; //保存图像文件像素数据
//LONG ImageWidth; //图像宽度
//LONG ImageHeight; //图像高度
//PBITMAPINFO info; //文件信息头
//IplImage * temp;
//IplImage * pImg;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;




//cout<<"nihao"<<endl;
char url[1000] = "http://192.168.131.129:8080/?action=snapshot";
while(1)
{
try
{
cout<<"目标地址 URL:"<<url<<endl;
Download(url);
cout<<"已经显示"<<endl;
}

catch(char *e)
{
cout<<e<<endl<<endl;
}

}
return nRetCode;
}


void Download(char url[])
{
char buffer[100000];
DWORD bytes_read;
HINTERNET internet= InternetOpen("HTTP Downloader",INTERNET_OPEN_TYPE_PRECONFIG,
NULL,NULL, NULL);

if( !internet )
throw "InternetOpen error!";

HINTERNET file_handle = InternetOpenUrl(internet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0);
if( !file_handle)
throw "InternetOpenUrl error! - Maybe you should add Http:// or Ftp://";

BOOL b = InternetReadFile(file_handle,buffer,100000,&bytes_read);
if(!b)
throw "InternetReadFile error!";


buffer[bytes_read]=0;


InternetCloseHandle(internet);
}



lizhigang34 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zouwen198317 的回复:]
显示片要不要编码解码这些步骤呢
[/Quote]
JPG已经是编码后的了,你可以用CxImage直接显示
听海拉拉 2010-08-26
  • 打赏
  • 举报
回复
显示片要不要编码解码这些步骤呢
听海拉拉 2010-08-26
  • 打赏
  • 举报
回复
"MJPG-streamer", is a command line application that copied JPG-frame from a single input plugin to multiple output plugins. It can be used to stream JPEG files over an IP-based network from the webcam to a viewer like Firefox, Cambozola, Videolanclient or even to a Windows Mobile device running the TCPMP-Player.

It was written for embedded devices with very limited ressources in terms of RAM and CPU. Its origin, the "uvc_streamer" was written, because Linux-UVC compatible cameras directly produce JPEG-data, allowing fast and perfomant M-JPEG streams even from an embedded device running OpenWRT. The input module "input_uvc.so" captures such JPG frames from a connected webcam.

This tool can be modified and distributed according to the terms of the GPL v2.

Currently no issues are known, but since this software is quite young and not used widely it may cause problems. You must really know what you are doing, if you use this software. If you want to use the software you are obliged to check if the sourcecode does what you expect it to do and take the risk yourself to use it.

To view the stream use VLC or Firefox and open the URL:
http://127.0.0.1:8080/?action=stream

To view a single JPEG just call:
http://127.0.0.1:8080/?action=snapshot

To compile and start the tool:
# tar xzvf mjpg-streamer.tgz
# cd mjpg-streamer
# make clean all
# export LD_LIBRARY_PATH=.
# ./mjpg_streamer -o "output_http.so -w ./www"

More examples can be found in the start.sh bash script.

In case of error:
* the input plugin "input_uvc.so" depends on libjpeg, make sure it is installed.

Dependencies for the input plugin "input_uvc.so":
* libjpeg
* recent Linux-UVC driver (newer then revision #170)

Dependencies for the output plugin "output_autofocus.so":
* libmath


To play the HTTP M-JPEG stream with mplayer:
# mplayer -fps 30 -demuxer lavf "http://127.0.0.1:8080/?action=stream&ignored.mjpg"

It might be necessary to configure mplayer to prefer IPv4 instead of IPv6
# vi ~./mplayer/config
add or change the option: prefer-ipv4=yes


相关的Opensource
tubo_true 2010-08-26
  • 打赏
  • 举报
回复
先找cinternetconnect代码,http下载url文件
在用cximage,freeimage库显示图片


google 搜索即可 cinternetconnect
听海拉拉 2010-08-26
  • 打赏
  • 举报
回复
原理比较明白,只是差参考的code
听海拉拉 2010-08-26
  • 打赏
  • 举报
回复
有没有参考的源码呢?
晓灬佩恩 2010-08-26
  • 打赏
  • 举报
回复
跟从本地读取差不多。边下载边播放。在接收端定义个缓冲区缓冲数据
fengrx 2010-08-26
  • 打赏
  • 举报
回复
拆分开:
1.下载。
2.显示。

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧