⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createimage.java

📁 java图片加水印生成~自己写的~学习为主
💻 JAVA
字号:
package test;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Random;
import javax.imageio.*;
import java.awt.font.*;
import java.awt.geom.*;
public class CreateImage {
 /*
  * email:jackqn@msn.com v 1.0
  */
 String value = "";
 String filepath = "";
  public static void main(String[] args) throws Exception {
  CreateImage test = new CreateImage();
  test.setValue("文章内容        文章内容         文章内容        文章内容        文章内容        文章内容        文章内容        文章内容        文章内容        文章内容        文章内容 ");
  test.setPath("F:/xul/image.jpg");
  test.getImage();
 }
  private  void setValue(String v) {
  value = v;
 }
  private  void setPath(String v) {
  filepath = v;
 }
  private  void getImage() {
  int width = 700;
  int height = 600;
  int num = 25; // 每行字数
  int fontpx = 24; // 字号
  File file = new File(filepath);
  Font font = new Font("仿宋", Font.BOLD, fontpx); // 字体
  BufferedImage bi = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = (Graphics2D) bi.getGraphics();
  g2.setBackground(Color.WHITE); // 背景颜色
  g2.clearRect(0, 0, width, height);
  FontRenderContext context = g2.getFontRenderContext();
  Rectangle2D bounds = font.getStringBounds(value, context);
  double x = 20;//    
  double y = 20;//   
  double ascent = -bounds.getY();
  double baseY = y + ascent;
  // /////////////////////////////////////////////////////
  BasicStroke wideStroke = new BasicStroke(1.0f);
  g2.setStroke(wideStroke); // 定义线条样式
  Random random = new Random();
  int red = 0, green = 0, blue = 0;
  for (int i = 0; i < 155; i++) {
   red = random.nextInt(255);
   green = random.nextInt(255);
   blue = random.nextInt(255);
   g2.setColor(new Color(red, green, blue));
   int x1 = random.nextInt(width);
   int y1 = random.nextInt(height);
   int x2 = random.nextInt(50);
   int y2 = random.nextInt(50);
   g2.drawLine(x1, y1, x1 + x2, y1 + y2);
  }
  // //////////////////////////////////////////////////
  // ////////////////////////////////////////////
  g2.setFont(new Font("宋体", Font.PLAIN, 24)); // 水印字体
  String rand = "水印       水印"; // 水印
  g2.setColor(Color.green); // 水印颜色
  g2.rotate(31, width / 2, height / 2);
  for (int i = 0; i < 15; i++) {
   g2.drawString(rand, random.nextInt(width), random.nextInt(width)); // 加水印
  }
  g2.rotate(-31, width / 2, height / 2);
  // ////////////////////////////////////////////////////
  String aa = "";
  g2.setFont(font);
  g2.setPaint(Color.RED);
  do {
   if (value.length() > num) {
    aa = value.substring(0, 25);
    value = value.substring(num, value.length());
   } else
    break;
   g2.drawString(aa, (int) x, (int) baseY);
   baseY += num;
  } while (value.length() - 25 > 0);
  if (value.length() < num)
   g2.drawString(value, (int) x, (int) baseY);
  // ////////////////////////////////////////////////
  try {
   ImageIO.write(bi, "jpg", file);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -