ImageUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package cn.com.ctop.common.utils;
  2. import javax.imageio.IIOException;
  3. import javax.imageio.ImageIO;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.font.FontRenderContext;
  7. import java.awt.font.GlyphVector;
  8. import java.awt.font.TextLayout;
  9. import java.awt.geom.AffineTransform;
  10. import java.awt.image.BufferedImage;
  11. import java.io.*;
  12. import java.util.ArrayList;
  13. import java.util.UUID;
  14. import java.util.List;
  15. public class ImageUtils {
  16. public BufferedImage toBufferedImage(Image image) {
  17. if (image instanceof BufferedImage) {
  18. return (BufferedImage) image;
  19. }
  20. // This code ensures that all the pixels in the image are loaded
  21. image = new ImageIcon(image).getImage();
  22. // Determine if the image has transparent pixels; for this method's
  23. // implementation, see e661 Determining If an Image Has Transparent Pixels
  24. //boolean hasAlpha = hasAlpha(image);
  25. // Create a buffered image with a format that's compatible with the screen
  26. BufferedImage bimage = null;
  27. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  28. try {
  29. // Determine the type of transparency of the new buffered image
  30. int transparency = Transparency.OPAQUE;
  31. /* if (hasAlpha) {
  32. transparency = Transparency.BITMASK;
  33. }*/
  34. // Create the buffered image
  35. GraphicsDevice gs = ge.getDefaultScreenDevice();
  36. GraphicsConfiguration gc = gs.getDefaultConfiguration();
  37. bimage = gc.createCompatibleImage(
  38. image.getWidth(null), image.getHeight(null), transparency);
  39. } catch (HeadlessException e) {
  40. // The system does not have a screen
  41. }
  42. if (bimage == null) {
  43. // Create a buffered image using the default color model
  44. int type = BufferedImage.TYPE_INT_RGB;
  45. //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
  46. /*if (hasAlpha) {
  47. type = BufferedImage.TYPE_INT_ARGB;
  48. }*/
  49. bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
  50. }
  51. // Copy image to buffered image
  52. Graphics g = bimage.createGraphics();
  53. // Paint the image onto the buffered image
  54. g.drawImage(image, 0, 0, null);
  55. g.dispose();
  56. return bimage;
  57. }
  58. public void resize(int width, int height, String sourcePath, String targetPath) {
  59. File inputFile = new File(sourcePath);
  60. File outputFile = new File(targetPath);
  61. try {
  62. Image sourceImage = ImageIO.read(inputFile);
  63. int sourceWidth = sourceImage.getWidth(null);
  64. int sourceHeight = sourceImage.getHeight(null);
  65. int newWidth = 0;
  66. int newHeight = 0;
  67. if (sourceWidth / sourceHeight >= width / height) {
  68. //放大高度
  69. newHeight = height;
  70. newWidth = (int) (((float) height / (float) sourceHeight) * sourceWidth);
  71. } else {
  72. //放大宽度
  73. newWidth = width;
  74. newHeight = (int) (((float) width / (float) sourceWidth) * sourceHeight);
  75. }
  76. Image resizeImage = sourceImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
  77. BufferedImage bufferedImage = toBufferedImage(resizeImage);
  78. bufferedImage = bufferedImage.getSubimage((newWidth - width) / 2, (newHeight - height) / 2, width, height);
  79. ImageIO.write(bufferedImage, "jpg", outputFile);
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. public void addTextInImage(InputStream input, String content, Color color, Font font) throws IOException {
  85. Image image = ImageIO.read(input);
  86. int width = image.getWidth(null);
  87. int height = image.getHeight(null);
  88. BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  89. Graphics2D graphics = bufferedImage.createGraphics();
  90. graphics.drawImage(image, 0, 0, width, height, null);
  91. graphics.setColor(color);
  92. graphics.setFont(font);
  93. Image borderImage = ImageIO.read(new File("D:/border/images/white01.png"));
  94. // BufferedImage bufferedImage2 = new BufferedImage(borderImage.getWidth(null),borderImage.getHeight(null),BufferedImage.TYPE_INT_RGB);
  95. graphics.drawImage(borderImage, 50, height / 2 + 100, null);
  96. // graphics2D.setColor(Color.BLUE.darker().darker());
  97. // graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  98. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  99. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  100. graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
  101. graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  102. // graphics2D.setStroke(new BasicStroke(5.0f ,
  103. // BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 15.0f, new float[] {10.0f,10.0f}, 5.0f));
  104. FontRenderContext frc = graphics.getFontRenderContext();
  105. int x = 150;
  106. int y = height / 2 + 220;
  107. if (content.length() < 9) {
  108. // TextLayout tl = new TextLayout(content, font, frc);
  109. // Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(5,25));
  110. // graphics2D.setStroke(new BasicStroke(3.0f));
  111. // graphics2D.setColor(Color.black);
  112. // graphics2D.draw(sha);
  113. // graphics2D.setColor(Color.white);
  114. // graphics2D.fill(sha);
  115. // graphics2D.dispose();
  116. graphics.drawString(content, x, y);
  117. } else {
  118. graphics.drawString(content.substring(0, 8), x, y);
  119. graphics.drawString(content.substring(8), x, y + 60);
  120. // TextLayout tl = new TextLayout(content.substring(0,8), font, frc);
  121. // Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(5,25));
  122. // graphics2D.setStroke(new BasicStroke(3.0f));
  123. // graphics2D.setColor(Color.WHITE);
  124. // graphics2D.draw(sha);
  125. // graphics2D.setColor(Color.BLACK);
  126. // graphics2D.fill(sha);
  127. //// graphics2D.dispose();
  128. //
  129. // TextLayout t2 = new TextLayout(content.substring(8), font, frc);
  130. // Shape sha2 = t2.getOutline(AffineTransform.getTranslateInstance(5,25));
  131. // graphics2D.setStroke(new BasicStroke(3.0f));
  132. // graphics2D.setColor(Color.WHITE);
  133. // graphics2D.draw(sha2);
  134. // graphics2D.setColor(Color.BLACK);
  135. // graphics2D.fill(sha);
  136. //// graphics2D.dispose();
  137. }
  138. graphics.dispose();
  139. FileOutputStream fileOutputStream = new FileOutputStream("D:/image/cover/" + UUID.randomUUID() + ".jpg");
  140. ImageIO.write(bufferedImage, "jpg", fileOutputStream);
  141. }
  142. public int getContentLength(String content, Graphics2D graphics) {
  143. return graphics.getFontMetrics(graphics.getFont()).charsWidth(content.toCharArray(), 0, content.length());
  144. }
  145. public static void main(String[] args) {
  146. ImageUtils i = new ImageUtils();
  147. Font font = new Font("微软雅黑", Font.BOLD + Font.PLAIN, 35);
  148. // String content = "测试封面文字内容\n最多可以十五字";
  149. Color color = new Color(0, 0, 0, 255);
  150. List<String> list = i.getContentList();
  151. // File file = new File("D:/bg1.jpg");
  152. File file = new File("D:/image/target");
  153. String[] files = file.list();
  154. for (String str : files) {
  155. String imagePath = file.getAbsolutePath() + "/" + str;
  156. try {
  157. File imageFile = new File(imagePath);
  158. for (String content : list) {
  159. InputStream inputStream = new FileInputStream(imageFile);
  160. i.addTextInImage(inputStream, content, color, font);
  161. }
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. }
  165. // new ImageUtils().resize(720,1280,imageFile,"D:/image/target/"+ UUID.randomUUID()+".jpg");
  166. }
  167. System.out.println();
  168. //
  169. }
  170. public List<String> getContentList() {
  171. List<String> list = new ArrayList<String>();
  172. list.add("想尝试一下这种风格");
  173. list.add("想看一下效果");
  174. list.add("这是个狠人");
  175. list.add("难得一见");
  176. list.add("每一个都很饱满");
  177. list.add("土豪吃法");
  178. list.add("怕了怕了");
  179. list.add("我反正不敢");
  180. list.add("贪吃的后果");
  181. list.add("求打败,在线等");
  182. list.add("女人心海底针");
  183. list.add("撕开赚钱的真相");
  184. list.add("这是啥味儿");
  185. list.add("爪子的妙用");
  186. list.add("把聊天记录删除后..");
  187. list.add("章鱼,太狠了");
  188. list.add("先下手为强");
  189. list.add("爪子的妙用");
  190. list.add("听说这是村里的帅哥");
  191. list.add("听说这是村里的美女");
  192. list.add("没想到,饭还能这样吃");
  193. list.add("没想到,水还能这样喝");
  194. list.add("没想到,衣服还能这样穿");
  195. list.add("没想到,车还能这样骑");
  196. list.add("没想到,妆还能这样画");
  197. list.add("她扒拉我");
  198. list.add("三者必有一伤");
  199. list.add("最新机械皮肤");
  200. list.add("妹子,这吃的啥");
  201. list.add("活下来,算我输");
  202. list.add("不哭算我输");
  203. list.add("不摔算我输");
  204. list.add("送礼物吓傻女友");
  205. list.add("是谁在唱歌");
  206. list.add("表情包不够,大长腿来凑");
  207. list.add("这场面真没见过");
  208. list.add("这蔬菜得回家供着");
  209. list.add("挖掘机哪家强");
  210. list.add("高手过招");
  211. list.add("还没怕过谁");
  212. list.add("手有点干巴");
  213. list.add("准备装箱");
  214. list.add("手把手教学");
  215. list.add("别怪我不客气");
  216. list.add("放大招了!");
  217. list.add("难怪我这么穷");
  218. list.add("招招毙命");
  219. list.add("随时准备辞职");
  220. list.add("不秀的李白");
  221. list.add("没大的黄忠");
  222. list.add("总是碰见猴子的鲁班");
  223. list.add("这样哄女友很管用");
  224. list.add("三年的聊天记录");
  225. list.add("用它不怕女神追不到");
  226. list.add("就靠你了");
  227. list.add("比谁快!");
  228. list.add("城里人真会玩");
  229. list.add("斗图我还没输过谁");
  230. list.add("开局斗图,把它斗哭了");
  231. list.add("敢和我来斗图");
  232. list.add("一起斗图啊");
  233. list.add("斗图你怕了吗");
  234. list.add("斗图输了的结果");
  235. list.add("斗图王者重出江湖");
  236. list.add("斗图还是我最强");
  237. list.add("打字就是表情包");
  238. list.add("现在表白都靠斗图了");
  239. list.add("斗图啊,王者在线等");
  240. list.add("斗图吗,年轻人");
  241. list.add("斗图能赚零花钱");
  242. list.add("确认过眼神,是我斗不过的人");
  243. list.add("年轻人爱的斗图软件");
  244. list.add("教你一招,包情包用不完");
  245. list.add("从此人生很有趣");
  246. list.add("我不认输,我只斗图");
  247. list.add("表情包不够用?");
  248. list.add("斗图,你过来呀!");
  249. list.add("斗图输了不开心");
  250. list.add("斗图输了不要怕");
  251. list.add("还怕表情包不够用?");
  252. list.add("表情包太少的后果");
  253. list.add("斗图秒赢!");
  254. list.add("是谁还在盗图?");
  255. return list;
  256. }
  257. }