1. package org.jeecg.modules.sysapi.util;
    2. import javax.imageio.ImageIO;
    3. import java.awt.*;
    4. import java.awt.geom.AffineTransform;
    5. import java.awt.image.AffineTransformOp;
    6. import java.awt.image.BufferedImage;
    7. import java.io.File;
    8. import java.io.IOException;
    9. public final class ImageUtils {
    10. /**
    11. * 缩放
    12. * @param filePath 图片路径
    13. * @param height 高度
    14. * @param width 宽度
    15. * @param bb 比例不对时是否需要补白
    16. */
    17. public static void resize(String filePath, int height, int width, boolean bb) {
    18. try {
    19. double ratio = 0.0; //缩放比例
    20. File f = new File(filePath);
    21. BufferedImage bi = ImageIO.read(f);
    22. Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
    23. //计算比例
    24. if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
    25. if (bi.getHeight() > bi.getWidth()) {
    26. ratio = (new Integer(height)).doubleValue() / bi.getHeight();
    27. } else {
    28. ratio = (new Integer(width)).doubleValue() / bi.getWidth();
    29. }
    30. AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
    31. itemp = op.filter(bi, null);
    32. }
    33. if (bb) {
    34. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    35. Graphics2D g = image.createGraphics();
    36. g.setColor(Color.BLACK);
    37. g.fillRect(0, 0, width, height);
    38. if (width == itemp.getWidth(null))
    39. g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
    40. else
    41. g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
    42. g.dispose();
    43. itemp = image;
    44. }
    45. ImageIO.write((BufferedImage) itemp, "jpg", f);
    46. } catch (IOException e) {
    47. e.printStackTrace();
    48. }
    49. }
    50. /**
    51. * 文字水印
    52. * @param pressText 水印文字
    53. * @param targetImg 目标图片
    54. * @param fontName 字体名称
    55. * @param fontStyle 字体样式
    56. * @param color 字体颜色
    57. * @param fontSize 字体大小
    58. * @param x 修正值
    59. * @param y 修正值
    60. * @param alpha 透明度
    61. */
    62. public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {
    63. try {
    64. File img = new File(targetImg);
    65. Image src = ImageIO.read(img);
    66. int width = src.getWidth(null);
    67. int height = src.getHeight(null);
    68. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    69. Graphics2D g = image.createGraphics();
    70. g.drawImage(src, 0, 0, width, height, null);
    71. g.setColor(color);
    72. g.setFont(new Font(fontName, fontStyle, fontSize));
    73. g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
    74. g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
    75. g.dispose();
    76. ImageIO.write((BufferedImage) image, "jpg", img);
    77. } catch (Exception e) {
    78. e.printStackTrace();
    79. }
    80. }
    81. /**
    82. * 图片水印
    83. * @param pressImg 水印图片
    84. * @param targetImg 目标图片
    85. * @param x 修正值 默认在中间
    86. * @param y 修正值 默认在中间
    87. * @param alpha 透明度
    88. */
    89. public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
    90. try {
    91. File img = new File(targetImg);
    92. Image src = ImageIO.read(img);
    93. int wideth = src.getWidth(null);
    94. int height = src.getHeight(null);
    95. BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
    96. Graphics2D g = image.createGraphics();
    97. g.drawImage(src, 0, 0, wideth, height, null);
    98. //水印文件
    99. Image src_biao = ImageIO.read(new File(pressImg));
    100. int wideth_biao = src_biao.getWidth(null);
    101. int height_biao = src_biao.getHeight(null);
    102. g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
    103. g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);
    104. //水印文件结束
    105. g.dispose();
    106. ImageIO.write((BufferedImage) image, "jpg", img);
    107. } catch (Exception e) {
    108. e.printStackTrace();
    109. }
    110. }
    111. /**
    112. * 这个方法用来得到文字的长度,全角一个字,半角算半个字,但是感觉这种方法不太好,不知道有没有更好地方法
    113. * @param text
    114. * @return
    115. */
    116. public static int getLength(String text) {
    117. int length = 0;
    118. for (int i = 0; i < text.length(); i++) {
    119. if (new String(text.charAt(i) + "").getBytes().length > 1) {
    120. length += 2;
    121. } else {
    122. length += 1;
    123. }
    124. }
    125. return length / 2;
    126. }
    127. public static void main(String[] args) {
    128. //为图片添加文字水印
    129. pressText( "我们网网站","f:/111.jpg", "黑体",36,Color.red, 80,0,0,0.3f);
    130. //为图片添加图片水印
    131. //pressImage("F:/222.jpg", "F:/111.jpg", 0, 0,0.2f);
    132. //缩放图片设置的宽高不够时边框补白
    133. //resize("F:/111.jpg", 500, 500, true);
    134. }
    135. }