Page 330 - 완) I MDP 프로젝트 작품 보고서(전체과 1학년)1.6
P. 330
3. 소프트웨어 개요
JAVA를 이용하였다.
○ MJPEG STREAMING 영상 받아오기(JAVA)
한 프레임당 http 헤더를 아래와 같이 불러온다.
--boundarydonotcross
Content-Type: image/jpeg
Content-Length: 23666
-Timestamp: 0.000000
???¢
http 헤더는 이미지 파일에는 필요 없기 때문에 http 헤더를 제거 해야한다.
class Webcam implements Runnable {
private static final String CONTENT_LENGTH = "Content-Length:";
private InputStream urlStream;
private StringWriter stringWriter;
private boolean processing = true;
public Webcam(URL url) throws IOException {
URLConnection urlConn = url.openConnection();
urlConn.setReadTimeout(10000); //리드타임아웃을 10초로 설정
urlConn.connect();
urlStream = urlConn.getInputStream();
stringWriter = new StringWriter(128);
}
private byte[] retrieveNextImage() throws IOException {
boolean haveHeader = false;
int currByte = -1;
String header = null;
while ((currByte = urlStream.read()) > -1 && !haveHeader) {
stringWriter.write(currByte);
String tempString = stringWriter.toString();
int indexOf = tempString.indexOf(CONTENT_LENGTH);
if (indexOf > -1 && currByte == '\n') {
haveHeader = true;
header = tempString;
}
} //http 헤더를 읽어 이미지크기를 구한다
while ((currByte=urlStream.read()) != 255) {
}//jpeg는 255라는 숫자 부터 이미지라고 한다.
int contentLength = contentLength(header);// 헤더로부터 이미지 크기를 구한다
byte[] imageBytes = new byte[contentLength + 1];//구해진 이미지 크기를 바탕으로 이미지를 저장할 공간을 만
든다
imageBytes[0] = (byte) 255; // 255부터 jpeg 이미지 이므로 0번째 배열에 255를 넣음
int offset = 1;
int numRead = 0;
while (offset < imageBytes.length
- 323 -