This commit is contained in:
Theophyl Hoguet committed 2023-03-15 18:21:11 +01:00
1 parent 50bb009e0d
commit aa9229f432
1 file changed
+175 -175
@@ -38,193 +38,193 @@ import java.awt.image.BufferedImage;
@RestController @RestController
public class ImageController { public class ImageController {
@Autowired @Autowired
private ObjectMapper mapper; private ObjectMapper mapper;
private final ImageDao imageDao; private final ImageDao imageDao;
public ImageController(ImageDao imageDao) { public ImageController(ImageDao imageDao) {
this.imageDao = imageDao; this.imageDao = imageDao;
} }
@DeleteMapping(value = "/images/{id}") @DeleteMapping(value = "/images/{id}")
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) { public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id); Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) { if (image.isPresent()) {
imageDao.delete(image.get()); imageDao.delete(image.get());
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK); return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
} }
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
} }
@PostMapping(value = "/images") @PostMapping(value = "/images")
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
String contentType = file.getContentType(); String contentType = file.getContentType();
if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString()) if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString())
&& !contentType.equals(MediaType.IMAGE_PNG.toString())) { && !contentType.equals(MediaType.IMAGE_PNG.toString())) {
return new ResponseEntity<>("Only JPEG/PNG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE); return new ResponseEntity<>("Only JPEG/PNG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
} }
try { try {
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes())); imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
} catch (IOException e) { } catch (IOException e) {
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT); return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
} }
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED); return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
} }
@GetMapping(value = "/images", produces = "application/json") @GetMapping(value = "/images", produces = "application/json")
@ResponseBody @ResponseBody
public ArrayNode getImageList() { public ArrayNode getImageList() {
List<Image> images = imageDao.retrieveAll(); List<Image> images = imageDao.retrieveAll();
ArrayNode nodes = mapper.createArrayNode(); ArrayNode nodes = mapper.createArrayNode();
for (Image image : images) { for (Image image : images) {
ObjectNode objectNode = mapper.createObjectNode(); ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("id", image.getId()); objectNode.put("id", image.getId());
objectNode.put("name", image.getName()); objectNode.put("name", image.getName());
objectNode.put("size", image.getSize()); objectNode.put("size", image.getSize());
objectNode.put("type", image.getMediaType().toString()); objectNode.put("type", image.getMediaType().toString());
nodes.add(objectNode); nodes.add(objectNode);
} }
return nodes; return nodes;
} }
@GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE, @GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE,
MediaType.IMAGE_PNG_VALUE }) MediaType.IMAGE_PNG_VALUE })
public ResponseEntity<?> getImage(@PathVariable("id") long id, public ResponseEntity<?> getImage(@PathVariable("id") long id,
@RequestParam(required = false) Map<String, String> parameters) { @RequestParam(required = false) Map<String, String> parameters) {
Optional<Image> image = imageDao.retrieve(id); Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) { if (image.isPresent()) {
if (parameters.isEmpty()) { if (parameters.isEmpty()) {
InputStream inputStream = new ByteArrayInputStream(image.get().getData()); InputStream inputStream = new ByteArrayInputStream(image.get().getData());
return ResponseEntity.ok().contentType(image.get().getMediaType()) return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream)); .body(new InputStreamResource(inputStream));
} }
if (parameters.containsKey("algorithm")) { if (parameters.containsKey("algorithm")) {
String algo = parameters.get("algorithm"); String algo = parameters.get("algorithm");
BufferedImage bufImg; BufferedImage bufImg;
try { try {
bufImg = ImageIO.read(new ByteArrayInputStream(image.get().getData())); bufImg = ImageIO.read(new ByteArrayInputStream(image.get().getData()));
} catch (IOException e) { } catch (IOException e) {
return new ResponseEntity<>("Image not readable", return new ResponseEntity<>("Image not readable",
HttpStatus.INTERNAL_SERVER_ERROR); HttpStatus.INTERNAL_SERVER_ERROR);
} }
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null, Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null,
true, GrayU8.class); true, GrayU8.class);
if (algo.equals("changeLuminosity")) { if (algo.equals("changeLuminosity")) {
if (parameters.containsKey("delta") if (parameters.containsKey("delta")
&& parameters.size() == 2) { && parameters.size() == 2) {
try { try {
int i = Integer.parseInt(parameters.get("delta")); int i = Integer.parseInt(parameters.get("delta"));
ImageProcessing.changeLuminosity(input, i); ImageProcessing.changeLuminosity(input, i);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'delta' should be an integer", return new ResponseEntity<>("Parameter 'delta' should be an integer",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else { } else {
return new ResponseEntity<>( return new ResponseEntity<>(
"Algorithm 'changeLuminosity' requires one parameter 'delta'(integer)", "Algorithm 'changeLuminosity' requires one parameter 'delta'(integer)",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else if (algo.equals("histogram")) { } else if (algo.equals("histogram")) {
if (parameters.size() == 1) { if (parameters.size() == 1) {
ImageProcessing.histogram(input); ImageProcessing.histogram(input);
} else { } else {
return new ResponseEntity<>( return new ResponseEntity<>(
"Algorithm 'histogram' requires no parameters", "Algorithm 'histogram' requires no parameters",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else if (algo.equals("colorFilter")) { } else if (algo.equals("colorFilter")) {
if (parameters.containsKey("hue") && parameters.size() == 2) { if (parameters.containsKey("hue") && parameters.size() == 2) {
try { try {
int i = Integer.parseInt(parameters.get("hue")); int i = Integer.parseInt(parameters.get("hue"));
ImageProcessing.colorFilter(i, input); ImageProcessing.colorFilter(i, input);
// hue entre 0 et 360? // hue entre 0 et 360?
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'hue' should be an integer", return new ResponseEntity<>("Parameter 'hue' should be an integer",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else { } else {
return new ResponseEntity<>( return new ResponseEntity<>(
"Algorithm 'colorFilter' requires one parameter 'hue'(integer)", "Algorithm 'colorFilter' requires one parameter 'hue'(integer)",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else if (algo.equals("meanFilter")) { } else if (algo.equals("meanFilter")) {
if (parameters.containsKey("size") && parameters.size() == 2) { if (parameters.containsKey("size") && parameters.size() == 2) {
try { try {
int i = Integer.parseInt(parameters.get("size")); int i = Integer.parseInt(parameters.get("size"));
if (i % 2 == 0) { if (i % 2 == 0) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer", return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
Planar<GrayU8> clone = input.clone(); Planar<GrayU8> clone = input.clone();
ImageProcessing.meanFilter(clone, input, i); ImageProcessing.meanFilter(clone, input, i);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer", return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else { } else {
return new ResponseEntity<>( return new ResponseEntity<>(
"Algorithm 'meanFilter' requires one parameter 'size'(odd positive integer)", "Algorithm 'meanFilter' requires one parameter 'size'(odd positive integer)",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else if (algo.equals("gaussienFilter")) { } else if (algo.equals("gaussienFilter")) {
if (parameters.containsKey("size") && parameters.size() == 2) { if (parameters.containsKey("size") && parameters.size() == 2) {
try { try {
int i = Integer.parseInt(parameters.get("size")); int i = Integer.parseInt(parameters.get("size"));
if (i % 2 == 0) { if (i % 2 == 0) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer", return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
Planar<GrayU8> clone = input.clone(); Planar<GrayU8> clone = input.clone();
ImageProcessing.gaussienFilter(clone, input, i); ImageProcessing.gaussienFilter(clone, input, i);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer", return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else { } else {
return new ResponseEntity<>( return new ResponseEntity<>(
"Algorithm 'gaussienFilter' requires one parameter 'size'(odd positive integer)", "Algorithm 'gaussienFilter' requires one parameter 'size'(odd positive integer)",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else if (algo.equals("contours")) { } else if (algo.equals("contours")) {
if (parameters.size() == 1) { if (parameters.size() == 1) {
Planar<GrayU8> clone = input.clone(); Planar<GrayU8> clone = input.clone();
ImageProcessing.gradientImageSobel(clone, input); ImageProcessing.gradientImageSobel(clone, input);
} else { } else {
return new ResponseEntity<>( return new ResponseEntity<>(
"Algorithm 'contours' requires no parameters", "Algorithm 'contours' requires no parameters",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} else { } else {
return new ResponseEntity<>( return new ResponseEntity<>(
"The requested algorithm '" + parameters.get("algorithm") + "' doesn't exist", "The requested algorithm '" + parameters.get("algorithm") + "' doesn't exist",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
bufImg = ConvertBufferedImage.convertTo_U8(input, null, true); bufImg = ConvertBufferedImage.convertTo_U8(input, null, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { try {
ImageIO.write(bufImg, image.get().getMediaType().getSubtype(), baos); ImageIO.write(bufImg, image.get().getMediaType().getSubtype(), baos);
} catch (IOException e) { } catch (IOException e) {
return new ResponseEntity<>("Image couldn't be converted", return new ResponseEntity<>("Image couldn't be converted",
HttpStatus.INTERNAL_SERVER_ERROR); HttpStatus.INTERNAL_SERVER_ERROR);
} }
byte[] imageData = baos.toByteArray(); byte[] imageData = baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream(imageData); InputStream inputStream = new ByteArrayInputStream(imageData);
return ResponseEntity.ok().contentType(image.get().getMediaType()) return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream)); .body(new InputStreamResource(inputStream));
} else { } else {
return new ResponseEntity<>("First parameter should be 'algorithm", return new ResponseEntity<>("First parameter should be 'algorithm",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} }
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
} }
} }