Search in sources :

Example 1 with ImageFormat

use of ai.elimu.model.enums.content.ImageFormat in project webapp by elimu-ai.

the class ImageListController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model, HttpSession session) {
    logger.info("handleRequest");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    // To ease development/testing, auto-generate Images
    List<Image> imagesGenerated = generateImages(contributor.getLocale());
    for (Image image : imagesGenerated) {
        Image existingImage = imageDao.read(image.getTitle(), image.getLocale());
        if (existingImage == null) {
            ImageFormat imageFormat = ImageFormat.PNG;
            image.setContentType("image/png");
            String fileName = image.getTitle() + "." + imageFormat.toString().toLowerCase();
            logger.info("Looking up file \"" + image.getTitle() + "." + imageFormat.toString().toLowerCase() + "\"...");
            URL url = getClass().getResource(fileName);
            logger.info("url.getPath(): " + url.getPath());
            try {
                byte[] imageBytes = IOUtils.toByteArray(url);
                image.setBytes(imageBytes);
                image.setImageFormat(imageFormat);
                int[] dominantColor = ImageColorHelper.getDominantColor(image.getBytes());
                image.setDominantColor("rgb(" + dominantColor[0] + "," + dominantColor[1] + "," + dominantColor[2] + ")");
                imageDao.create(image);
            } catch (IOException ex) {
                logger.error(null, ex);
            }
        }
    }
    List<Image> images = imageDao.readAllOrdered(contributor.getLocale());
    model.addAttribute("images", images);
    return "content/multimedia/image/list";
}
Also used : Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) Image(ai.elimu.model.content.multimedia.Image) URL(java.net.URL) ImageFormat(ai.elimu.model.enums.content.ImageFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.Contributor)1 Image (ai.elimu.model.content.multimedia.Image)1 ImageFormat (ai.elimu.model.enums.content.ImageFormat)1 IOException (java.io.IOException)1 URL (java.net.URL)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1