Java tutorial
/** * The MIT License * Copyright (c) 2016 Orange Foundry * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.github.orangefoundry.gorender.controllers; import com.github.orangefoundry.gorender.colour.ColourToAWTColour; import com.github.orangefoundry.gorender.defaults.Default; import org.springframework.web.bind.annotation.*; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; @RestController public class ImageRenderController { @RequestMapping(value = "/", method = RequestMethod.GET, produces = "image/png") public @ResponseBody byte[] renderDefaultWithColour(@RequestParam(value = "c", required = false) String colour, @RequestParam(value = "s", required = false) String size) throws java.io.IOException { return renderPlaceHolder(size, colour); } private byte[] renderPlaceHolder(String size, String colour) throws IOException { if (size == null || size.isEmpty()) { size = Default.SIZE.getValue(); } if (colour == null || colour.isEmpty()) { colour = Default.COLOUR.getValue(); } String[] sizes = new String[2]; if (size != null && !size.isEmpty()) { sizes = size.toLowerCase().split("x"); } int width = Integer.parseInt(sizes[0]), height = Integer.parseInt(sizes[1]); final ColourToAWTColour colourToAWTColour = new ColourToAWTColour(); Color myColour = colourToAWTColour.getColour(colour); int rgb = myColour.getRGB(); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); int[] data = new int[width * height]; int i = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { data[i++] = rgb; } } bi.setRGB(0, 0, width, height, data, 0, width); ByteArrayOutputStream bao = new ByteArrayOutputStream(); ImageIO.write(bi, "png", bao); return bao.toByteArray(); } }