Skip to content

Commit

Permalink
Raspberry Piにて初回画像取得でタイムアウトが発生する場合があり、タイムアウトした場合でもエラーとならないように修正。
Browse files Browse the repository at this point in the history
  • Loading branch information
onozaty committed Jan 22, 2018
1 parent c191f28 commit dcf6962
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import lombok.RequiredArgsConstructor;
Expand All @@ -17,7 +18,8 @@ public class SpringBootCaptureClientApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringBootCaptureClientApplication.class);
application.setWebEnvironment(false);
application.run(args);
ConfigurableApplicationContext context = application.run(args);
context.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ public void startCapture() throws IOException, InterruptedException {
while (true) {

if (webSocketHandler.isConnected()) {
BufferedImage image = webcam.getImage();

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ImageIO.write(image, "jpeg", outputStream);
webSocketHandler.sendMessage(outputStream.toByteArray());
BufferedImage image = webcam.getImage();

// Occasionally time out when the load is high. At that time it becomes null.
if (image != null) {

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ImageIO.write(image, "jpeg", outputStream);
webSocketHandler.sendMessage(outputStream.toByteArray());
}
}
}

if (!webcam.isOpen()) {
throw new IOException("Camera closed.");
}

Thread.sleep(intervalMillis);
}
}
Expand Down

0 comments on commit dcf6962

Please sign in to comment.