Spring MVC 메소드에요
실제로는 날씨api도 파싱하고 현재시간도 넣고 많이했는데 기본적인 틀만 올려요.
File font1f=null;
@RequestMapping(value = "/#주소#", method = RequestMethod.GET)
public ResponseEntity<byte[]> img(HttpServletRequest request,HttpServletResponse response) throws IOException, FontFormatException {
String filename="#리턴할파일명#.png";
//입력부분
ServletContext context = request.getSession().getServletContext();
InputStream is=request.getSession().getServletContext().getResourceAsStream("#수정할 png 이미지 경로#");
InputStream font1is=request.getSession().getServletContext().getResourceAsStream("#폰트경로#/NanumPen.ttf");
if(font1f==null){
font1f=File.createTempFile("font1t", ".tmp");
FileOutputStream fos=new FileOutputStream(font1f);
IOUtils.copy(font1s, fos);
fos.close();
}
BufferedImage img = ImageIO.read(is);
is.close();
//그래픽 시작 초기화
Graphics2D g2d=img.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Font font = Font.createFont(Font.TRUETYPE_FONT, font1if);
font=font.deriveFont(20.0f);
g2d.setFont(font);
g2d.setPaint(Color.white);
//본격 시작
String ex = "이미지에 추가할 예제스트링";
g2d.drawString(ex, #x좌표#, #y좌표#);
//출력부분
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( img, "png", baos );
baos.flush();
byte[] image = baos.toByteArray();
baos.close();
final HttpHeaders headers = new HttpHeaders();
headers.set("Content-Disposition", "inline; filename="+ new String(filename.getBytes("UTF-8"), "ISO-8859-1"));
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(image, headers, HttpStatus.OK);
}