下载地址:https://www.pan38.com/share.php?code=pvvmX 提取码:8888 【仅供学习娱乐测试】
报告单P图软件的Java实现,包含多个模块,仅供娱乐用途。代码实现了基本的图像处理和文本叠加功能。
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class MedicalReportGenerator {
private static final String TEMPLATE_PATH = "template.jpg";
private static final String OUTPUT_PATH = "output/";
private static final String FONT_NAME = "Microsoft YaHei";
private BufferedImage templateImage;
private Map<String, Point> textPositions;
private Random random;
public MedicalReportGenerator() {
this.random = new Random();
loadTemplate();
initTextPositions();
}
private void loadTemplate() {
try {
this.templateImage = ImageIO.read(new File(TEMPLATE_PATH));
} catch (IOException e) {
System.err.println("无法加载模板图片: " + e.getMessage());
System.exit(1);
}
}
private void initTextPositions() {
textPositions = new HashMap<>();
textPositions.put("hospitalName", new Point(300, 100));
textPositions.put("patientName", new Point(200, 180));
textPositions.put("patientGender", new Point(400, 180));
textPositions.put("patientAge", new Point(500, 180));
textPositions.put("patientID", new Point(200, 220));
textPositions.put("examDate", new Point(500, 220));
textPositions.put("examType", new Point(300, 260));
textPositions.put("diagnosis", new Point(200, 300));
textPositions.put("doctorName", new Point(200, 500));
textPositions.put("reportDate", new Point(500, 500));
}
public void generateReport(PatientInfo patientInfo) {
BufferedImage outputImage = new BufferedImage(
templateImage.getWidth(),
templateImage.getHeight(),
BufferedImage.TYPE_INT_RGB
);
Graphics2D graphics = outputImage.createGraphics();
graphics.drawImage(templateImage, 0, 0, null);
graphics.setColor(Color.BLACK);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// 绘制医院名称
drawText(graphics, "hospitalName", patientInfo.getHospitalName(), 28, Font.BOLD);
// 绘制患者基本信息
drawText(graphics, "patientName", "姓名: " + patientInfo.getName(), 16, Font.PLAIN);
drawText(graphics, "patientGender", "性别: " + patientInfo.getGender(), 16, Font.PLAIN);
drawText(graphics, "patientAge", "年龄: " + patientInfo.getAge(), 16, Font.PLAIN);
drawText(graphics, "patientID", "身份证号: " + patientInfo.getIdNumber(), 16, Font.PLAIN);
// 绘制检查日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
drawText(graphics, "examDate", "检查日期: " + sdf.format(patientInfo.getExamDate()), 16, Font.PLAIN);
// 绘制检查类型
drawText(graphics, "examType", "检查项目: " + patientInfo.getExamType(), 18, Font.BOLD);
// 绘制诊断结果
drawMultiLineText(graphics, "diagnosis", "诊断结果: " + patientInfo.getDiagnosis(), 16, Font.PLAIN, 600);
// 绘制医生签名和报告日期
drawText(graphics, "doctorName", "医师签名: " + patientInfo.getDoctorName(), 16, Font.PLAIN);
drawText(graphics, "reportDate", "报告日期: " + sdf.format(new Date()), 16, Font.PLAIN);
graphics.dispose();
saveImage(outputImage, patientInfo.getName());
}
private void drawText(Graphics2D graphics, String positionKey, String text, int fontSize, int fontStyle) {
Point position = textPositions.get(positionKey);
if (position != null) {
Font font = new Font(FONT_NAME, fontStyle, fontSize);
graphics.setFont(font);
graphics.drawString(text, position.x, position.y);
}
}
private void drawMultiLineText(Graphics2D graphics, String positionKey, String text, int fontSize, int fontStyle, int maxWidth) {
Point position = textPositions.get(positionKey);
if (position == null) return;
Font font = new Font(FONT_NAME, fontStyle, fontSize);
graphics.setFont(font);
FontMetrics metrics = graphics.getFontMetrics();
String[] words = text.split(" ");
StringBuilder currentLine = new StringBuilder();
int y = position.y;
for (String word : words) {
if (metrics.stringWidth(currentLine + " " + word) < maxWidth) {
currentLine.append(word).append(" ");
} else {
graphics.drawString(currentLine.toString(), position.x, y);
y += metrics.getHeight();
currentLine = new StringBuilder(word + " ");
}
}
if (!currentLine.isEmpty()) {
graphics.drawString(currentLine.toString(), position.x, y);
}
}
private void saveImage(BufferedImage image, String patientName) {
File outputDir = new File(OUTPUT_PATH);
if (!outputDir.exists()) {
outputDir.mkdirs();
}
String fileName = OUTPUT_PATH + patientName + "_" +
new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";
try {
ImageIO.write(image, "jpg", new File(fileName));
System.out.println("报告单已生成: " + fileName);
} catch (IOException e) {
System.err.println("保存图片失败: " + e.getMessage());
}
}
public static void main(String[] args) {
MedicalReportGenerator generator = new MedicalReportGenerator();
PatientInfo patient = new PatientInfo();
patient.setName("张三");
patient.setGender("男");
patient.setAge(35);
patient.setIdNumber("123456198801011234");
patient.setHospitalName("北京协和医院");
patient.setExamDate(new Date());
patient.setExamType("胸部CT平扫");
patient.setDiagnosis("1. 右肺上叶见一结节影,大小约1.2cm×1.0cm,边界尚清,建议3个月后复查;2. 双肺纹理增粗;3. 心脏大小形态正常;4. 纵隔内未见明显肿大淋巴结。");
patient.setDoctorName("李四");
generator.generateReport(patient);
}
}
import java.util.Date;
public class PatientInfo {
private String name;
private String gender;
private int age;
private String idNumber;
private String hospitalName;
private Date examDate;
private String examType;
private String diagnosis;
private String doctorName;
// 省略getter和setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public String getHospitalName() {
return hospitalName;
}
public void setHospitalName(String hospitalName) {
this.hospitalName = hospitalName;
}
public Date getExamDate() {
return examDate;
}
public void setExamDate(Date examDate) {
this.examDate = examDate;
}
public String getExamType() {
return examType;
}
public void setExamType(String examType) {
this.examType = examType;
}
public String getDiagnosis() {
return diagnosis;
}
public void setDiagnosis(String diagnosis) {
this.diagnosis = diagnosis;
}
public String getDoctorName() {
return doctorName;
}
public void setDoctorName(String doctorName) {
this.doctorName = doctorName;
}
}