Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public interface RobotController {
/**
* Captures an image from the camera.
*
* @return String The captured image as a base64 encoded string.
* @return The path to the captured image.
*/
String captureImage();

Expand All @@ -318,7 +318,7 @@ public interface RobotController {
*
* @param angleHorizontal The horizontal angle to rotate the camera to. The angle must be between -90 and 90.
* @param angleVertical The vertical angle to rotate the camera to. The angle must be between -90 and 90.
* @return String The captured image as a base64 encoded string.
* @return The path to the captured image.
*/
String captureImage(int angleHorizontal, int angleVertical);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import io.github.francwhite.raros.client.connector.*;

import java.io.FileOutputStream;
import java.io.IOException;

class RobotControllerImpl implements RobotController {
private final ActionAwaiter actionAwaiter;
private final MagnetService magnetService;
Expand Down Expand Up @@ -243,17 +246,38 @@ public void turnRightAsync(double angle, double radius) {

@Override
public String captureImage() {
return this.cameraService.captureImage().join();
var image = this.cameraService.captureImage().join();
return this.saveImage(image);
}

@Override
public String captureImage(int angleHorizontal, int angleVertical) {
this.cameraService.rotateCamera(angleHorizontal, angleVertical).join();
return this.cameraService.captureImage().join();
var image = this.cameraService.captureImage().join();
return this.saveImage(image);
}

@Override
public void rotateCamera(int angleHorizontal, int angleVertical) {
this.cameraService.rotateCamera(angleHorizontal, angleVertical).join();
}

private String saveImage(String imageBase64) {
var imageBytes = java.util.Base64.getDecoder().decode(imageBase64);
var outputDirectory = new java.io.File("images");
//noinspection ResultOfMethodCallIgnored
outputDirectory.mkdir();

var filePath = outputDirectory.getAbsolutePath()
+ "/image-"
+ java.time.LocalDateTime.now().toString().replace(":", "-")
+ ".jpg";

try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(imageBytes);
return filePath;
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}