gpt4 book ai didi

java - 从 mjpeg 流保存图像

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:44 33 4
gpt4 key购买 nike


我正在尝试从 MJPEG 流中捕获图像(jpeg)。
根据本教程http://www.walking-productions.com/notslop/2010/04/20/motion-jpeg-in-flash-and-java/我应该只保存从 Content-Length: 开始到 –myboundary 结束的日期。
但由于某种原因,当我打开保存的文件时,我收到此消息无法打开此图片,因为该文件似乎已损坏、损坏或太大。

public class MJPEGParser{

public static void main(String[] args){
new MJPEGParser("http://192.168.0.100/video4.mjpg");
}

public MJPEGParser(String mjpeg_url){
try{

URL url = new URL(mjpeg_url);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while((line = in.readLine()) != null){
System.out.println(line);
if(line.contains("Content-Length:")){
BufferedWriter out = new BufferedWriter(new FileWriter("out.jpeg"));
String content = in.readLine();
while(!content.contains("--myboundary")){
out.write(content);
System.out.println(content);
content = in.readLine();

}
out.close();
in.close();
System.exit(0);
}
}

in.close();

}catch(Exception e){
e.printStackTrace();
}
}
}

如果有任何提示,我将非常感激。

最佳答案

有很多事情可能会导致您的代码失败:

  • 并非全部 MJPEG流按照教程建议的类似 MIME 的方式进行编码。
    引用Wikipedia :

    […] there is no document that defines a single exact format that is universally recognized as a complete specification of “Motion JPEG” for use in all contexts.

    例如,可以使用 MJPEG 作为 AVI 的视频编解码器。 (即 RIFF )文件。因此,请确保您的输入文件采用教程描述的格式。

  • 文本--myboundary几乎可以肯定是一个占位符。在典型的multipart MIME message ,全局 MIME header 将提供一个 boundary 属性,指示什么字符串实际用于分隔各部分。您必须在此处给出的字符串前面添加 --
  • 当您使用 ReaderWriter 时,您操作的是字符,而不是字节。根据您的区域设置,这两者之间可能不存在 1:1 的对应关系,从而破坏了进程中数据的二进制格式。您应该对字节流进行操作,或者明确使用某些字符编码,例如 ISO-8859-1,它确实具有这样的 1:1 对应关系。

关于java - 从 mjpeg 流保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12584192/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com