http mjpeg 图像读取
http mjpeg 图像格式
网络摄像头的视频流解析直接使用通过http的Mjpeg是具有边界帧信息的multipart/x-mixed-replace,而jpeg数据只是以二进制形式发送。因此,实际上不需要关心HTTP协议标头。所有jpeg帧均以marker开头,0xff 0xd8并以0xff 0xd9 结尾。因此,代码可以从http流中提取此类帧,解码,编码h264 ,重新发送。
抓包
GET /stream/mjpeg HTTP/1.1
Host: 192.168.0.66:2401
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace;boundary=----WebKitFormBoundaryIZDrYHwuf2VJdpHw
Cache-Control: no-cache
------WebKitFormBoundaryIZDrYHwuf2VJdpHw
Content-Type: image/jpg
Content-Length: 61108
…JFIF…
1、python opencv来读 http mjpeg
以下使用python来读http mjpeg,因为python 方便快速,可以做原型方法的测试
import cv2
import requests
import numpy as npr = requests.get('http://192.168.1.xx/mjpeg.cgi', auth=('user', 'password'), stream=True)
if(r.status_code == 200):bytes = bytes()for chunk in r.iter_content(chunk_size=1024):bytes += chunka = bytes.find(b'\xff\xd8')b = bytes.find(b'\xff\xd9')if a != -1 and b != -1:jpg = bytes[a:b+2]bytes = bytes[b+2:]i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)cv2.imshow('i', i)if cv2.waitKey(1) == 27:exit(0)
else:print("Received unexpected status code {}".format(r.status_code))
2、java读取
java读取也是很方便的
package net.thistleshrub.mechwarfare.mjpeg;import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;import javax.imageio.ImageIO;public class MjpegRunner implements Runnable
{private static final String CONTENT_LENGTH = "Content-length: ";private static final String CONTENT_TYPE = "Content-type: image/jpeg";private MJpegViewer viewer;private InputStream urlStream;private StringWriter stringWriter;private boolean processing = true;public MjpegRunner(MJpegViewer viewer, URL url) throws IOException{this.viewer = viewer;URLConnection urlConn = url.openConnection();// change the timeout to taste, I like 1 secondurlConn.setReadTimeout(1000);urlConn.connect();urlStream = urlConn.getInputStream();stringWriter = new StringWriter(128);}public synchronized void stop(){processing = false;}@Overridepublic void run(){while(processing){try{byte[] imageBytes = retrieveNextImage();ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);BufferedImage image = ImageIO.read(bais);viewer.setBufferedImage(image);viewer.repaint();}catch(SocketTimeoutException ste){System.err.println("failed stream read: " + ste);viewer.setFailedString("Lost Camera connection: " + ste);viewer.repaint();stop();}catch(IOException e){System.err.println("failed stream read: " + e);stop();}}// close streamstry{urlStream.close();}catch(IOException ioe){System.err.println("Failed to close the stream: " + ioe);}}/*** Using the urlStream get the next JPEG image as a byte[]* @return byte[] of the JPEG* @throws IOException*/private byte[] retrieveNextImage() throws IOException{boolean haveHeader = false; int currByte = -1;String header = null;// build headers// the DCS-930L stops it's headerswhile((currByte = urlStream.read()) > -1 && !haveHeader){stringWriter.write(currByte);String tempString = stringWriter.toString(); int indexOf = tempString.indexOf(CONTENT_TYPE);if(indexOf > 0){haveHeader = true;header = tempString;}} // 255 indicates the start of the jpeg imagewhile((urlStream.read()) != 255){// just skip extras}// rest is the bufferint contentLength = contentLength(header);byte[] imageBytes = new byte[contentLength + 1];// since we ate the original 255 , shove it back inimageBytes[0] = (byte)255;int offset = 1;int numRead = 0;while (offset < imageBytes.length&& (numRead=urlStream.read(imageBytes, offset, imageBytes.length-offset)) >= 0) {offset += numRead;} stringWriter = new StringWriter(128);return imageBytes;}// dirty but it works content-length parsingprivate static int contentLength(String header){int indexOfContentLength = header.indexOf(CONTENT_LENGTH);int valueStartPos = indexOfContentLength + CONTENT_LENGTH.length();int indexOfEOL = header.indexOf('\n', indexOfContentLength);String lengthValStr = header.substring(valueStartPos, indexOfEOL).trim();int retValue = Integer.parseInt(lengthValStr);return retValue;}
}
3、c++读取
未完待续
http mjpeg 图像读取相关推荐
- python代码转换为pytorch_python、PyTorch图像读取与numpy转换
python.PyTorch图像读取与numpy转换 发布时间:2018-06-15 16:27, 浏览次数:1147 , 标签: python PyTorch numpy Tensor转为numpy ...
- python、PyTorch图像读取与numpy转换
原文:https://blog.csdn.net/yskyskyer123/article/details/80707038 python.PyTorch图像读取与numpy转换 Tensor转为nu ...
- 【opencv系列02】OpenCV4.X图像读取与显示
点击上方"AI搞事情"关注我们 一.读取图片 opencv中采用imread() 函数读取图像 imread(filename, flags=None) filename ...
- youcans 的 OpenCV 学习课—2.图像读取与显示
youcans 的 OpenCV 学习课-2.图像读取与显示 本系列面向 Python 小白,从零开始实战解说 OpenCV 项目实战. 本节介绍图像的读取.保存和显示.除基本方法和例程外,还给出了从 ...
- python 3.8.0版本的skimage库是什么_python的skimage库 图像读取显示
单幅图像读取并显示 代码 """ 读取图像并显示 """ import matplotlib.pyplot as plt import ma ...
- skimage 图像读取显示
单幅图像读取并显示 代码 """ 读取图像并显示 """ import matplotlib.pyplot as plt import ma ...
- 图像读取、显示和保存
使用opencv库进行演示 1. 图像读取 cv2.imread( ) 注:cv2.imread的返回结果是按照bgr顺序排列的 2. 图像显示 cv2.imshow(windowName,img ) ...
- opencv入门基础——图像读取,图像显示,图像保存
一,图像读取 如上图所示,从文件中导入图像用这个函数 retval=cv.imread(文件名,[,显示控制参数]) 显示控制参数,主要是这几个: cv.IMREAD_UNCHANGED cv.IMR ...
- C#使用EmguCV库(图像读取、显示、保存)(二)
使用C#+EmguCV处理图像入门(图像读取_显示_保存)二 上个随笔已经介绍EmguCV的一些常用库和程序安装以及环境变量的配置,这次写的是如何使用这个类库对图像进行操作. EmguCV图像处理系统 ...
最新文章
- 19-7-15学习笔记
- .NET Core的日志[4]:将日志写入EventLog
- jsp stc_为什么说jsp的本质是servlet?
- Java基础复习笔记系列 九 网络编程
- C#——继承[模拟Server类]初始化过程顺序DMEO
- spring jmx_JMX和Spring –第3部分
- F5 BIGip 负载均衡 IP算法解密工具
- 1.4 测试各阶段(单元、集成、系统 、Alpha、Beta、验收)
- 【渝粤题库】广东开放大学 秘书理论与实务(1) 形成性考核
- k8s查看pod的yaml文件_k8s监控系统prometheus-operator
- 如何速成java_极*Java速成教程 - (4)
- 新华三PRIMERA,开启存储新纪元
- A*算法和dijkstra算法
- 《葬经》郭璞 高清彩色版手抄欣赏
- 你也可以掌控EMI:EMI基础及无Y电容手机充电器设计
- 【Java毕设】基于SpringBoot实现新冠疫情统计系统(Idea+Navicat)
- 基于51单片机的数码管闹钟设计
- wordpress启动_如何通过7个简单步骤正确地启动WordPress博客(2020)
- 深入理解Activity的生命周期
- 移动通信调制技术的进展 转