Created
August 22, 2018 08:20
-
-
Save johnnian/50d88d44943ffc226a043336ee7c4fd3 to your computer and use it in GitHub Desktop.
#图片处理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.shishu.service; | |
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.Graphics2D; | |
import java.awt.Image; | |
import java.awt.RenderingHints; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
public class GenerateDynamicHeathImage { | |
Log log = LogFactory.getLog(GenerateDynamicHeathImage.class); | |
/** | |
* 生成图片方法 | |
* @author WQ | |
* @param inPath | |
* @param outPath | |
* @param content | |
* @param img | |
* @param font | |
* @param start_point_x | |
* @param start_point_y | |
*/ | |
public void generateImage(String inPath, String outPath, | |
String[][] content, Image img, Font font, int start_point_x, | |
int start_point_y) { | |
try { | |
File file = new File(inPath); | |
Image image = ImageIO.read(file); | |
int height = image.getHeight(null); | |
int width = image.getWidth(null); | |
int text_x = 0; | |
int text_y = 0; | |
int img_x = 0; | |
int img_y = 0; | |
int line_y = 498; | |
Color color = new Color(89, 89, 89); | |
BufferedImage bufferedImage = new BufferedImage(width, height, | |
BufferedImage.TYPE_INT_RGB); | |
Graphics2D graphics = bufferedImage.createGraphics(); | |
//文字去锯齿 | |
graphics.drawImage(image, 0, 0, width, height, null); | |
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
RenderingHints.VALUE_ANTIALIAS_ON); | |
graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, | |
RenderingHints.VALUE_STROKE_NORMALIZE); | |
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, | |
RenderingHints.VALUE_RENDER_QUALITY); | |
if (img != null) { | |
graphics.setFont(font); | |
text_x = start_point_x; | |
text_y = start_point_y; | |
for (int i = 0; i < content.length; i++) { | |
String[] tempData = content[i]; | |
color = new Color(89, 89, 89); | |
text_x = start_point_x; | |
if (i == 1){ | |
text_y += 110; | |
img_y += 140; | |
} else if (i != 0 && i > 1){ | |
text_y += 100; | |
img_y += 130; | |
} | |
line_y += 132; | |
for (int j = 0; j < tempData.length; j++) { | |
if (j == 1) { | |
text_y += 50; | |
color = new Color(171, 171, 171); | |
graphics.drawLine(12, line_y, 750 - 12, line_y); | |
} else if (j == 2) { | |
text_x = width - 124; | |
text_y -= 20; | |
img_x = 50; | |
if (tempData[2].equals("过高")){ | |
color = new Color(255, 80, 80); | |
} else { | |
color = new Color(71, 207, 162); | |
} | |
graphics.drawImage(img, img_x, img_y, null); | |
} | |
graphics.setColor(color); | |
graphics.drawString(tempData[j], text_x, text_y); | |
} | |
} | |
} else { | |
System.out.println("构建数据的图片对象为空..."); | |
} | |
FileOutputStream fileOutputStream = new FileOutputStream(outPath); | |
ImageIO.write(bufferedImage, "png", fileOutputStream); | |
System.out.println("图片添加文字完成" + width + "---" + height); | |
fileOutputStream.flush(); | |
fileOutputStream.close(); | |
} catch (IOException e) { | |
log.info("io exception :" + e); | |
} | |
} | |
public static void main(String[] args) { | |
Font font = new Font("微软雅黑", Font.PLAIN, 30); | |
String inPath = "/Users/Johnnian/tmp/image/bg.png"; | |
String outPath = "/Users/Johnnian/tmp/image/out.png"; | |
/*String inPath = "D:/TestGenerateImage/DynamicImage/Test.jpg"; | |
String outPath = "D:/TestGenerateImage/DynamicImage/Test1.jpg";*/ | |
String[][] textConent = { | |
{ "血压(非空腹) : 6.0mmol/L", "时间: 2017年9月16日 10:52", "正常" }, | |
{ "血压(非空腹) : 13.0mmol/L", "时间: 2017年9月16日 15:47", "过高" }, | |
{ "血压(非空腹) : 6.3mmol/L", "时间: 2017年9月16日 16:13", "正常" } , | |
{ "血压(非空腹) : 6.6mmol/L", "时间: 2017年9月17日 10:24", "正常" }, | |
{ "血压(非空腹) : 15.1mmol/L", "时间: 2017年9月17日 11:10", "过高" } }; | |
int x = 0; | |
int y = 0; | |
try { | |
new GenerateDynamicHeathImage().generateImage(inPath, outPath, | |
textConent, ImageIO.read(new File( | |
"/Users/Johnnian/tmp/image/icon.png")), font, x, y); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment