图片上添加水印
public void drawTextInImg(HttpServletResponse response, String startTime, String endTime,String filePath) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image img = imgIcon.getImage();
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(getColor("#AF2222"));
g.setBackground(Color.white);
g.drawImage(img, 0, 0, null);
Font font = new Font("Arial", Font.PLAIN, 27);
g.setFont(font);
int left = 330;
int top = 1079;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawString(startTime, left, top);
g.drawString(endTime, left, top+65);
g.dispose();
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(bimage, "png", imageOutput);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
long length = imageOutput.length();
response.setContentType("image/png");
response.setContentLength((int)length);
response.setHeader("Content-Disposition","attachment;filename="+new String("license.png".getBytes("gbk"),"iso-8859-1"));
byte[] bytes = new byte[1024];
OutputStream outputStream = response.getOutputStream();
long count = 0;
while(count < length){
int len = inputStream.read(bytes, 0, 1024);
count +=len;
outputStream.write(bytes, 0, len);
}
outputStream.flush();
} catch (Exception e){
e.printStackTrace();
}
}
public static Color getColor(String color) {
if (color.charAt(0) == '#') {
color = color.substring(1);
}
if (color.length() != 6) {
return null;
}
try {
int r = Integer.parseInt(color.substring(0, 2), 16);
int g = Integer.parseInt(color.substring(2, 4), 16);
int b = Integer.parseInt(color.substring(4), 16);
return new Color(r, g, b);
} catch (NumberFormatException nfe) {
return null;
}
}