• java 生成相亲信息海报


     

    1. package com.company;
    2. import sun.font.FontDesignMetrics;
    3. import javax.imageio.ImageIO;
    4. import java.awt.*;
    5. import java.awt.image.BufferedImage;
    6. import java.io.*;
    7. import java.net.URL;
    8. /**
    9. * Created by zj on 2018/10/18.
    10. */
    11. public class haibao2 {
    12. public static void main(String[] args) {
    13. // String backgroundPath = "http://xqapi.haoyingsheng.top/profile/upload/2022/09/06/458db93c05d3156a6c3381823bf317dc%20-%20%E5%89%AF%E6%9C%AC_20220906115542A002.jpg";
    14. // String qrCodePath = "http://xqapi.haoyingsheng.top/profile/upload/2022/09/06/222878_20220906115731A003.jpg";
    15. // String people_img="http://xqapi.haoyingsheng.top/profile/upload/2022/08/22/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220822102619_20220822102729A007.png";
    16. // String people_img1="http://xqapi.haoyingsheng.top/profile/upload/2022/08/22/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220822102619_20220822102729A007.png";
    17. String backgroundPath = "C:\\Users\\Administrator\\Desktop\\海报\\haibaobeijing.jpg";
    18. String qrCodePath = "C:\\Users\\Administrator\\Desktop\\海报\\xiaochnegxuma.jpg";
    19. String people_img="C:\\Users\\Administrator\\Desktop\\海报\\renwu1.png";
    20. String people_img1="C:\\Users\\Administrator\\Desktop\\海报\\renwu1.png";
    21. String message01 ="山东地方个地方官地方官,方大哥大地飞歌电饭锅电饭锅电饭,锅电饭锅地方官方的,个地方官的山东地方个地方官地!!";
    22. String message02 = "";
    23. String outPutPath="D:\\ruoyi\\qr1.jpg";
    24. String age="23";
    25. String mHeight="180";
    26. String aihao="旅行";
    27. String xueli="博士";
    28. String zhiye="行政";
    29. String xianju="北京";
    30. overlapImage(backgroundPath,qrCodePath,people_img,people_img1,message01,message02,outPutPath,
    31. age,mHeight,aihao,xueli,zhiye,xianju
    32. );
    33. }
    34. public static String overlapImage(String backgroundPath,String qrCodePath,
    35. String people_img,String people_img1,
    36. String message01,String message02,
    37. String outPutPath,String age,
    38. String mHeight,String aihao,
    39. String xueli,String zhiye,
    40. String xianju
    41. ){
    42. int width=500;
    43. int height=888;
    44. try {
    45. //设置图片大小
    46. InputStream inputStream = new FileInputStream(backgroundPath);
    47. BufferedImage bi = ImageIO.read(inputStream);
    48. BufferedImage background = resizeImage(width,height, bi);
    49. InputStream inputStream1 = new FileInputStream(qrCodePath);
    50. BufferedImage bi1 = ImageIO.read(inputStream1);
    51. BufferedImage qrCode = resizeImage(60,60, bi1);
    52. InputStream inputStream2 = new FileInputStream(people_img);
    53. BufferedImage bi2 = ImageIO.read(inputStream2);
    54. BufferedImage peopleImg = resizeImage(261,381, bi2);
    55. InputStream inputStream3 = new FileInputStream(people_img1);
    56. BufferedImage bi3 = ImageIO.read(inputStream3);
    57. BufferedImage peopleImg1 = resizeImage(309,216, bi3);
    58. //在背景图片中添加入需要写入的信息,例如:扫描下方二维码,欢迎大家添加我的淘宝返利机器人,居家必备,省钱购物专属小秘书!
    59. Graphics2D g = background.createGraphics();
    60. Color color1=new Color(142,127,132);
    61. g.setColor(color1);
    62. g.setFont(new Font("",Font.BOLD,35));
    63. // 计算文字长度,计算居中的x点坐标
    64. Font font=new Font("",Font.BOLD,35);
    65. FontMetrics fm = g.getFontMetrics(font);
    66. int textWidth = fm.stringWidth(message02);
    67. int widthX = (width - textWidth) / 2;
    68. g.drawString(message02,widthX ,50);
    69. int fontsize2=16;
    70. g.setFont(new Font("",Font.PLAIN,fontsize2));
    71. Font font1=new Font("",Font.PLAIN,fontsize2);
    72. // 画简介
    73. drawWordAndLineFeed(g,font1,message01,70,755,255);
    74. // 构建年龄
    75. g.setFont(new Font("",Font.BOLD,fontsize2));
    76. Color color2=new Color(241,77,128);
    77. g.setColor(color2);
    78. FontMetrics fm1 = g.getFontMetrics(font1);
    79. textWidth =fm1.stringWidth(age);
    80. drawWordAndLineFeed(g,font1,age,126+(52-textWidth)/2,673,52);
    81. // 身高
    82. textWidth =fm1.stringWidth(mHeight);
    83. drawWordAndLineFeed(g,font1,mHeight,250+(52-textWidth)/2,673,52);
    84. // 爱好
    85. textWidth =fm1.stringWidth(aihao);
    86. drawWordAndLineFeed(g,font1,aihao,375+(52-textWidth)/2,673,52);
    87. // 学历
    88. textWidth =fm1.stringWidth(xueli);
    89. drawWordAndLineFeed(g,font1,xueli,126+(52-textWidth)/2,718,52);
    90. // 职业
    91. textWidth =fm1.stringWidth(mHeight);
    92. drawWordAndLineFeed(g,font1,zhiye,250+(52-textWidth)/2,718,52);
    93. // 现居
    94. textWidth =fm1.stringWidth(xueli);
    95. drawWordAndLineFeed(g,font1,xianju,375+(52-textWidth)/2,718,52);
    96. g.drawImage(peopleImg, 71, 65, peopleImg.getWidth(), peopleImg.getHeight(), null);
    97. g.drawImage(peopleImg1, 121, 361, peopleImg1.getWidth(), peopleImg1.getHeight(), null);
    98. //在背景图片上添加二维码图片
    99. g.drawImage(qrCode, 416, 804, qrCode.getWidth(), qrCode.getHeight(), null);
    100. g.dispose();
    101. System.out.printf("outPutPath"+outPutPath);
    102. ImageIO.write(background, "jpg", new File(outPutPath));
    103. }catch (Exception e){
    104. e.printStackTrace();
    105. }
    106. return null;
    107. }
    108. private static int drawWordAndLineFeed(Graphics2D g2d, Font font, String words, int wordsX, int wordsY, int wordsWidth) {
    109. FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
    110. // 获取字符的最高的高度
    111. int height = metrics.getHeight();
    112. int width = 0;
    113. int count = 0;
    114. int total = words.length();
    115. System.out.println("几行字--"+total);
    116. String subWords = words;
    117. int b = 0;
    118. int zuidi=0;
    119. for (int i = 0; i < total; i++) {
    120. // 统计字符串宽度 并与 预设好的宽度 作比较
    121. if (width <= wordsWidth) {
    122. width += metrics.charWidth(words.charAt(i)); // 获取每个字符的宽度
    123. count++;
    124. } else {
    125. // 画 除了最后一行的前几行
    126. String substring = subWords.substring(0, count);
    127. // g2d.drawString(substring, wordsX, wordsY + (b * height));
    128. MyDrawString(substring, wordsX, wordsY + (b * height),1.2,g2d);
    129. subWords = subWords.substring(count);
    130. b++;
    131. width = 0;
    132. count = 0;
    133. }
    134. // 画 最后一行字符串
    135. if (i == total - 1) {
    136. g2d.drawString(subWords, wordsX, wordsY + (b * height));
    137. }
    138. zuidi=wordsY + (b * height);
    139. }
    140. System.out.println("高度--"+zuidi+25);
    141. return zuidi+25;
    142. }
    143. public static void MyDrawString(String str,int x,int y,double rate,Graphics g){
    144. String tempStr=new String();
    145. int orgStringWight=g.getFontMetrics().stringWidth(str);
    146. int orgStringLength=str.length();
    147. int tempx=x;
    148. int tempy=y;
    149. while(str.length()>0)
    150. {
    151. tempStr=str.substring(0, 1);
    152. str=str.substring(1, str.length());
    153. g.drawString(tempStr, tempx, tempy);
    154. tempx=(int)(tempx+(double)orgStringWight/(double)orgStringLength*rate);
    155. }
    156. }
    157. private static File getFile(String url) throws Exception {
    158. //对本地文件命名
    159. String fileName = url.substring(url.lastIndexOf("."),url.length());
    160. File file = null;
    161. URL urlfile;
    162. InputStream inStream = null;
    163. OutputStream os = null;
    164. try {
    165. file = File.createTempFile("net_url", fileName);
    166. //下载
    167. urlfile = new URL(url);
    168. inStream = urlfile.openStream();
    169. os = new FileOutputStream(file);
    170. int bytesRead = 0;
    171. byte[] buffer = new byte[8192];
    172. while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
    173. os.write(buffer, 0, bytesRead);
    174. }
    175. } catch (Exception e) {
    176. e.printStackTrace();
    177. } finally {
    178. try {
    179. if (null != os) {
    180. os.close();
    181. }
    182. if (null != inStream) {
    183. inStream.close();
    184. }
    185. } catch (Exception e) {
    186. e.printStackTrace();
    187. }
    188. }
    189. return file;
    190. }
    191. public static BufferedImage resizeImage(int x, int y, BufferedImage bfi){
    192. BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
    193. bufferedImage.getGraphics().drawImage(
    194. bfi.getScaledInstance(x, y, Image.SCALE_DEFAULT), 0, 0, null);
    195. return bufferedImage;
    196. }
    197. }

  • 相关阅读:
    docker-compose安装ES7.14和Kibana7.14(有账号密码)
    金仓数据库KingbaseES数据库参考手册(服务器配置参数1. 设置参数)
    线性表
    暗黑破坏神词缀实现思路2.0
    BartForConditionalGeneration的使用细节
    智能化物流管理:全国快递物流查询API的角色与优势
    探究Socks5代理和代理IP在技术领域的多重应用
    linux服务器中安装mysql时候,远程访问的时候的乱码
    抖音自动引流软件的运行分享,与开发需要到的技术分析
    iOS实时监控与报警器
  • 原文地址:https://blog.csdn.net/qq_36935391/article/details/126725376