removed unnessary imports and main in ImageProcessing and started besoin 8

This commit is contained in:
keahorvath authored and Theophyl Hoguet committed 2023-03-15 19:53:01 +01:00
1 parent 7819411212
commit 24a3e9d6e7
2 files changed
+106 -100

No files matched your search

@@ -6,12 +6,17 @@ import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import javax.imageio.ImageIO;
import javax.print.attribute.standard.Media;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import boofcv.io.image.ConvertBufferedImage;
import boofcv.struct.image.GrayU8;
import boofcv.struct.image.Planar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
@@ -27,85 +32,113 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
@RestController
public class ImageController {
@Autowired
private ObjectMapper mapper;
@Autowired
private ObjectMapper mapper;
private final ImageDao imageDao;
private final ImageDao imageDao;
public ImageController(ImageDao imageDao) {
this.imageDao = imageDao;
}
public ImageController(ImageDao imageDao) {
this.imageDao = imageDao;
}
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE || MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
@DeleteMapping(value = "/images/{id}")
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id);
Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) {
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
return ResponseEntity.ok().contentType(image.getMediaType()).body(new InputStreamResource(inputStream));
}
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
}
if (image.isPresent()) {
imageDao.delete(image.get());
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
}
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
}
@DeleteMapping(value = "/images/{id}")
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
@PostMapping(value = "/images")
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
Optional<Image> image = imageDao.retrieve(id);
String contentType = file.getContentType();
if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString())
&& !contentType.equals(MediaType.IMAGE_PNG.toString())) {
return new ResponseEntity<>("Only JPEG/PNG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}
try {
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
} catch (IOException e) {
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
}
if (image.isPresent()) {
imageDao.delete(image.get());
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
}
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
}
@GetMapping(value = "/images", produces = "application/json")
@ResponseBody
public ArrayNode getImageList() {
List<Image> images = imageDao.retrieveAll();
ArrayNode nodes = mapper.createArrayNode();
for (Image image : images) {
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("id", image.getId());
objectNode.put("name", image.getName());
objectNode.put("size", image.getSize());
objectNode.put("type", image.getMediaType().toString());
nodes.add(objectNode);
}
return nodes;
}
@PostMapping(value = "/images")
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
@GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE,
MediaType.IMAGE_PNG_VALUE })
public ResponseEntity<?> applyAlgorithm(@PathVariable("id") long id,
@RequestParam(required = false) String algorithm,
@RequestParam(required = false) Long p1, @RequestParam(required = false) Long p2) {
String contentType = file.getContentType();
if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString()) && !contentType.equals(MediaType.IMAGE_PNG.toString())) {
return new ResponseEntity<>("Only JPEG/PNG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}
try {
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
} catch (IOException e) {
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
}
Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) {
if (algorithm == null) {
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream));
@GetMapping(value = "/images", produces = "application/json")
@ResponseBody
public ArrayNode getImageList() {
List<Image> images = imageDao.retrieveAll();
ArrayNode nodes = mapper.createArrayNode();
for (Image image : images) {
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("id", image.getId());
objectNode.put("name", image.getName());
objectNode.put("size", image.getSize());
objectNode.put("type", image.getMediaType().toString());
nodes.add(objectNode);
}
return nodes;
}
}
if (algorithm.equals("changeLuminosity")) {
BufferedImage bufImg;
try {
bufImg = ImageIO.read(new ByteArrayInputStream(image.get().getData()));
} catch (IOException e) {
return new ResponseEntity<>("Image not readable",
HttpStatus.INTERNAL_SERVER_ERROR);
}
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null,
true, GrayU8.class);
ImageProcessing.changeLuminosity(input, (int) p1.longValue());
@GetMapping(value = "/images/{id}?algorithm={X}&p1={Y}&p2={Z}", produces = MediaType.IMAGE_JPEG_VALUE || MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<?> applyAlgorithm(@PathVariable("id") long id, @PathVariable("X") string algorithm, @PathVariable("Y") int firstParam, @PathVariable("Z") int secondParam) {
int width = input.getWidth();
int height = input.getHeight();
int numChannels = input.getNumBands();
Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) {
if (algorithm.equals("changeLuminosity")){
Optional<Image> changedImage = createSameShape(image);
ImageProcessing.changeLuminosity(changedImage, firstParam);
}
InputStream inputStream = new ByteArrayInputStream(changedImage.get().getData());
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
}
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
}
// Allocate a byte array to hold the pixel data
byte[] imageData = new byte[width * height * numChannels];
// Iterate over the planes and copy the pixel data into the byte array
for (int i = 0; i < numChannels; i++) {
byte[] planeData = input.getBand(i).getData();
System.arraycopy(planeData, 0, imageData, i * width * height, width *
height);
}
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
// InputStream inputStream = new ByteArrayInputStream(imageData);
System.out.println("hi");
return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream));
}
}
return new ResponseEntity<>("Image id=" + id + " not found.",
HttpStatus.NOT_FOUND);
}
}
@@ -1,10 +1,6 @@
package pdl.backend;
import java.awt.image.BufferedImage;
import boofcv.io.image.UtilImageIO;
import boofcv.io.image.ConvertBufferedImage;
import boofcv.struct.image.GrayU8;
import boofcv.struct.border.BorderType;
import boofcv.struct.image.Planar;
public class ImageProcessing {
@@ -14,8 +10,8 @@ public class ImageProcessing {
* Retourne les valeurs r, g et b d'un pixel d'une image placés en paramètres
*
* @param input L'image
* @param x La ligne
* @param y La colonne
* @param x La ligne
* @param y La colonne
*/
public static int[] getRGBValue(Planar<GrayU8> input, int x, int y) {
int[] rgb = new int[3];
@@ -30,9 +26,9 @@ public class ImageProcessing {
* Colore le pixel donné de l'image donnée avec les valeurs rgb données
*
* @param input L'image
* @param x La ligne
* @param y La colonne
* @param rgb Tableau de taille 3 avec les valeurs de r, g, et b
* @param x La ligne
* @param y La colonne
* @param rgb Tableau de taille 3 avec les valeurs de r, g, et b
*/
public static void setRGBValue(Planar<GrayU8> input, int x, int y, int[] rgb) {
for (int i = 0; i < rgb.length; i++) {
@@ -127,7 +123,6 @@ public class ImageProcessing {
}
}
/**
*
* Passe un pixel de l'espace de couleurs RGB à l'espace de couleurs HSV :
@@ -249,7 +244,7 @@ public class ImageProcessing {
for (int x = 0; x < input.width; x++) {
if (y < half || x < half || y >= input.height - half || x >= input.width - half) {
int gl = inputGray.getBand(0).get(x, y);
int[] rgb = {gl,gl,gl};
int[] rgb = { gl, gl, gl };
setRGBValue(output, x, y, rgb);
continue;
}
@@ -271,22 +266,19 @@ public class ImageProcessing {
value = 0;
if (value > 255)
value = 255;
int[] rgb = {value, value, value};
int[] rgb = { value, value, value };
setRGBValue(output, x, y, rgb);
}
}
}
/**
* Applique un filtre moyenneur de taille indiquée à l'image
* passée en paramètre (le bord de l'image n'est pas modifié)
*
* @param input image d'entrée
* @param output image de sortie
* @param size taille du filtre à appliquer
* @param size taille du filtre à appliquer
*/
public static void meanFilter(Planar<GrayU8> input, Planar<GrayU8> output, int size) {
// Vérification que la taille est impaire
@@ -372,23 +364,4 @@ public class ImageProcessing {
}
}
}
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("missing input or output image filename");
System.exit(-1);
}
final String inputPath = args[0];
BufferedImage image = UtilImageIO.loadImage(inputPath);
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(image, null, true, GrayU8.class);
Planar<GrayU8> output = input.createSameShape();
long beginning = System.nanoTime();
histogram(input);
long end = System.nanoTime();
System.out.println("Duration : " + ((end - beginning) / 1000000) + "ms");
// save output image
final String outputPath = args[1];
UtilImageIO.saveImage(input, outputPath);
System.out.println("Image saved in: " + outputPath); }
}