我在尝试从MJPEG流接收图像(JPEG)时遇到问题。 我正在连接到Pi并打开一个套接字和缓冲读取器来读取行,考虑到标题响应和BoudaryStrings。 我遇到的问题是当我读取图像的所有行并将其保存为.jpg文件时,我将其视为腐败的彩色混乱。 在这种情况下,任何人都有办法帮助我吗?或任何建议?
我的最终目标是通过重新绘制图像让Swing显示在Swing GUI上。
public class clientReceive {
public static void main(String args[]) throws UnknownHostException, IOException {
String ContentType;
int ContentLength;
int count = 0, bString = 0;
int i = 0;
File outputFile = new File("./images/" + i + ".jpg");
BufferedImage bufferedImage = null;
String formatName = "jpeg";
byte[] b = new byte[30000];
Socket s = new Socket("192.168.1.18", 8081);
InputStream is = s.getInputStream();
// Read response from web server, which will trigger the multipart HTTP request
// to be sent.
BufferedReader httpResponseReader = new BufferedReader(new InputStreamReader(is));
String lineRead;
while ((lineRead = httpResponseReader.readLine()) != null) {
PrintStream out = new PrintStream(new FileOutputStream("./images/" + i + ".jpg", true));
/*How every Image Frame is Separated
* --BoundaryString
* Content-type: image/jpeg
* Content-Length: 26051
*/
if (lineRead.equalsIgnoreCase("--BoundaryString")) {
lineRead = httpResponseReader.readLine();
if (lineRead.equalsIgnoreCase("Content-type: image/jpeg")) {
lineRead = httpResponseReader.readLine();
if (lineRead.contains("Content-Length:")) {
lineRead = httpResponseReader.readLine();
// System.out.println(lineRead);
while (!lineRead.equalsIgnoreCase("--BoundaryString")) {
lineRead = httpResponseReader.readLine();
System.out.println(lineRead);
out.println(lineRead);
// bufferedImage=ImageIO.read(is);
}
// System.out.println(lineRead);
// out.println(lineRead);
// ImageIO.write(bufferedImage,formatName,outputFile);
i++;
}
}
}
}
}