# 文字转化图片
# 效果1
package com.travel.util;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class DirectionImage extends Application {
private static final int WIDTH = 450;
private static final int HEIGHT = 200;
@Override
public void start(Stage primaryStage) {
// Input locations
String location1 = "广州 ——>";
String location2 = "上海";
// Create a canvas
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFont(Font.font("宋体", FontWeight.BOLD, 45));
// Draw text
gc.setFill(Color.BLUE);
gc.fillText(location1, 50, 100);
gc.fillText(location2, 300, 100);
// Save the canvas as an image
saveAsImage(canvas);
}
private void saveAsImage(Canvas canvas) {
WritableImage writableImage = new WritableImage(WIDTH, HEIGHT);
canvas.snapshot(new SnapshotParameters(), writableImage);
// Write the image to file
File file = new File("output.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null), "png", file);
System.out.println("Image saved as: " + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
// Close the application
System.exit(0);
}
public static void main(String[] args) {
launch(args);
}
}
# 效果2
添加随机背景偏淡颜色
package com.travel.util;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class DirectionImage extends Application {
private static final int WIDTH = 450;
private static final int HEIGHT = 200;
@Override
public void start(Stage primaryStage) {
// Input locations
String location1 = "广州 ——>";
String location2 = "上海";
// Create a canvas
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
// Fill canvas with a randomly light background color
Random random = new Random();
Color backgroundColor = Color.rgb(random.nextInt(150), random.nextInt(150), random.nextInt(150), 0.5);
gc.setFill(backgroundColor);
gc.fillRect(0, 0, WIDTH, HEIGHT);
gc.setFont(Font.font("宋体", FontWeight.BOLD, 45));
// Draw text
gc.setFill(Color.BLUE);
gc.fillText(location1, 50, 100);
gc.fillText(location2, 300, 100);
// Save the canvas as an image
saveAsImage(canvas);
}
private void saveAsImage(Canvas canvas) {
WritableImage writableImage = new WritableImage(WIDTH, HEIGHT);
canvas.snapshot(new SnapshotParameters(), writableImage);
// Write the image to file
File file = new File("output.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null), "png", file);
System.out.println("Image saved as: " + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
// Close the application
System.exit(0);
}
public static void main(String[] args) {
launch(args);
}
}
# 补充
字符串包含
表情包
也行的