下载地址:https://www.pan38.com/share.php?code=bRtMK 提取码:8888
声明:仅供娱乐学习测试~请勿用于非法用途
如何使用Java生成自定义图片
图像水印添加技术
合法的电子凭证生成方案
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageGenerator {
public static void createDemoImage(String outputPath) throws Exception {
int width = 800;
int height = 600;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 24));
g.drawString("教学用示例图片", 50, 50);
g.drawString("生成时间: " + new java.util.Date(), 50, 100);
g.dispose();
ImageIO.write(image, "png", new File(outputPath));
}
AI 代码解读
}
class Main {
public static void main(String[] args) {
try {
ImageGenerator.createDemoImage("demo.png");
System.out.println("已生成教学示例图片");
} catch (Exception e) {
System.err.println("图片生成失败: " + e.getMessage());
}
}
}
import javax.swing.;
import java.awt.;
import java.awt.image.BufferedImage;
public class ImageEditor {
private BufferedImage currentImage;
public ImageEditor(int width, int height) {
this.currentImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
initWhiteBackground();
}
private void initWhiteBackground() {
Graphics2D g = currentImage.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, currentImage.getWidth(), currentImage.getHeight());
g.dispose();
}
public void addText(String text, int x, int y, Font font, Color color) {
Graphics2D g = currentImage.createGraphics();
g.setFont(font);
g.setColor(color);
g.drawString(text, x, y);
g.dispose();
}
public void saveAsPNG(String filePath) throws Exception {
ImageIO.write(currentImage, "png", new File(filePath));
}
AI 代码解读
}
public class TextLayer {
private String content;
private Font font;
private Color color;
private Point position;
public TextLayer(String content, Font font, Color color, Point position) {
this.content = content;
this.font = font;
this.color = color;
this.position = position;
}
public void applyToImage(BufferedImage image) {
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(font);
g.setColor(color);
g.drawString(content, position.x, position.y);
g.dispose();
}
AI 代码解读
}
class MainApp {
public static void main(String[] args) {
try {
ImageEditor editor = new ImageEditor(800, 600);
Font titleFont = new Font("微软雅黑", Font.BOLD, 24);
editor.addText("教学示例图片", 50, 50, titleFont, Color.BLACK);
Font warningFont = new Font("宋体", Font.ITALIC, 16);
editor.addText("本图片仅用于Java图形编程教学", 50, 100, warningFont, Color.RED);
editor.saveAsPNG("output.png");
System.out.println("图片已生成");
} catch (Exception e) {
e.printStackTrace();
}
}
AI 代码解读
}
Java 2D图形编程官方文档
OpenCV图像处理库
数字水印技术实现