https://www.pan38.com/share.php?code=pvvmX 提取码:8888
这个Java项目实现了医院诊断单的生成功能,包含三个主要类:MedicalReportGenerator是核心类,负责生成诊断单图片;MedicalRecord是数据模型类,存储诊断单相关信息;ReportTemplateGenerator用于创建基础模板图片。使用时需要先运行ReportTemplateGenerator生成模板,然后通过MedicalReportGenerator生成具体诊断单。
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 int REPORT_WIDTH = 800;
private static final int REPORT_HEIGHT = 1200;
private static final String FONT_NAME = "Microsoft YaHei";
private static final String OUTPUT_DIR = "reports/";
private Random random = new Random();
private Map<String, Point> fieldPositions = new HashMap<>();
public MedicalReportGenerator() {
initializeFieldPositions();
}
private void initializeFieldPositions() {
fieldPositions.put("hospitalName", new Point(300, 80));
fieldPositions.put("reportTitle", new Point(350, 150));
fieldPositions.put("patientInfo", new Point(100, 220));
fieldPositions.put("examInfo", new Point(100, 280));
fieldPositions.put("diagnosisTitle", new Point(100, 350));
fieldPositions.put("diagnosisContent", new Point(120, 400));
fieldPositions.put("adviceTitle", new Point(100, 700));
fieldPositions.put("adviceContent", new Point(120, 750));
fieldPositions.put("doctorInfo", new Point(100, 1000));
fieldPositions.put("reportDate", new Point(500, 1000));
fieldPositions.put("hospitalSeal", new Point(600, 1050));
}
public void generateReport(MedicalRecord record) throws IOException {
BufferedImage reportImage = createBaseImage();
Graphics2D g2d = reportImage.createGraphics();
// 设置抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// 绘制医院名称
drawHospitalHeader(g2d, record.getHospitalName());
// 绘制报告标题
drawReportTitle(g2d, record.getReportType());
// 绘制患者信息
drawPatientInfo(g2d, record);
// 绘制检查信息
drawExamInfo(g2d, record);
// 绘制诊断结果
drawDiagnosis(g2d, record);
// 绘制医嘱建议
drawMedicalAdvice(g2d, record);
// 绘制医生信息和日期
drawFooter(g2d, record);
// 绘制医院公章
drawHospitalSeal(g2d);
g2d.dispose();
// 保存报告
saveReportImage(reportImage, record.getPatientName());
}
private BufferedImage createBaseImage() {
BufferedImage image = new BufferedImage(REPORT_WIDTH, REPORT_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, REPORT_WIDTH, REPORT_HEIGHT);
g2d.dispose();
return image;
}
private void drawHospitalHeader(Graphics2D g2d, String hospitalName) {
Point pos = fieldPositions.get("hospitalName");
Font font = new Font(FONT_NAME, Font.BOLD, 28);
g2d.setFont(font);
g2d.setColor(Color.BLACK);
g2d.drawString(hospitalName, pos.x, pos.y);
// 绘制下划线
int lineY = pos.y + 5;
g2d.drawLine(pos.x, lineY, pos.x + 200, lineY);
}
private void drawReportTitle(Graphics2D g2d, String reportType) {
Point pos = fieldPositions.get("reportTitle");
Font font = new Font(FONT_NAME, Font.BOLD, 24);
g2d.setFont(font);
g2d.setColor(Color.BLACK);
g2d.drawString(reportType + "诊断报告单", pos.x, pos.y);
}
private void drawPatientInfo(Graphics2D g2d, MedicalRecord record) {
Point pos = fieldPositions.get("patientInfo");
Font font = new Font(FONT_NAME, Font.PLAIN, 16);
g2d.setFont(font);
String info = String.format("姓名: %s 性别: %s 年龄: %d 病历号: %s",
record.getPatientName(),
record.getPatientGender(),
record.getPatientAge(),
record.getMedicalRecordNumber());
g2d.drawString(info, pos.x, pos.y);
}
private void drawExamInfo(Graphics2D g2d, MedicalRecord record) {
Point pos = fieldPositions.get("examInfo");
Font font = new Font(FONT_NAME, Font.PLAIN, 16);
g2d.setFont(font);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
String examTime = sdf.format(record.getExamDate());
String info = String.format("检查项目: %s 检查时间: %s 科室: %s",
record.getExamType(),
examTime,
record.getDepartment());
g2d.drawString(info, pos.x, pos.y);
}
private void drawDiagnosis(Graphics2D g2d, MedicalRecord record) {
Point titlePos = fieldPositions.get("diagnosisTitle");
Font titleFont = new Font(FONT_NAME, Font.BOLD, 18);
g2d.setFont(titleFont);
g2d.drawString("诊断结果:", titlePos.x, titlePos.y);
Point contentPos = fieldPositions.get("diagnosisContent");
Font contentFont = new Font(FONT_NAME, Font.PLAIN, 16);
g2d.setFont(contentFont);
drawWrappedText(g2d, record.getDiagnosis(), contentPos.x, contentPos.y, 650, 20);
}
private void drawMedicalAdvice(Graphics2D g2d, MedicalRecord record) {
Point titlePos = fieldPositions.get("adviceTitle");
Font titleFont = new Font(FONT_NAME, Font.BOLD, 18);
g2d.setFont(titleFont);
g2d.drawString("医嘱建议:", titlePos.x, titlePos.y);
Point contentPos = fieldPositions.get("adviceContent");
Font contentFont = new Font(FONT_NAME, Font.PLAIN, 16);
g2d.setFont(contentFont);
drawWrappedText(g2d, record.getMedicalAdvice(), contentPos.x, contentPos.y, 650, 20);
}
private void drawFooter(Graphics2D g2d, MedicalRecord record) {
Point doctorPos = fieldPositions.get("doctorInfo");
Font font = new Font(FONT_NAME, Font.PLAIN, 16);
g2d.setFont(font);
g2d.drawString("医师签名: " + record.getDoctorName(), doctorPos.x, doctorPos.y);
Point datePos = fieldPositions.get("reportDate");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
g2d.drawString("报告日期: " + sdf.format(new Date()), datePos.x, datePos.y);
}
private void drawHospitalSeal(Graphics2D g2d) {
Point pos = fieldPositions.get("hospitalSeal");
int diameter = 120;
// 绘制圆形公章
g2d.setColor(new Color(200, 0, 0, 150));
g2d.fillOval(pos.x, pos.y, diameter, diameter);
// 绘制公章文字
g2d.setColor(Color.WHITE);
Font font = new Font(FONT_NAME, Font.BOLD, 16);
g2d.setFont(font);
// 这里简化公章文字绘制,实际实现可以更复杂
g2d.drawString("医院公章", pos.x + 20, pos.y + diameter/2);
}
private void drawWrappedText(Graphics2D g2d, String text, int x, int y, int maxWidth, int lineHeight) {
FontMetrics metrics = g2d.getFontMetrics();
String[] words = text.split(" ");
StringBuilder currentLine = new StringBuilder();
int currentY = y;
for (String word : words) {
if (metrics.stringWidth(currentLine.toString() + word) < maxWidth) {
currentLine.append(word).append(" ");
} else {
g2d.drawString(currentLine.toString(), x, currentY);
currentY += lineHeight;
currentLine = new StringBuilder(word + " ");
}
}
if (currentLine.length() > 0) {
g2d.drawString(currentLine.toString(), x, currentY);
}
}
private void saveReportImage(BufferedImage image, String patientName) throws IOException {
File outputDir = new File(OUTPUT_DIR);
if (!outputDir.exists()) {
outputDir.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String timestamp = sdf.format(new Date());
String filename = OUTPUT_DIR + patientName + "_" + timestamp + ".png";
ImageIO.write(image, "png", new File(filename));
System.out.println("诊断报告已生成: " + filename);
}
public static void main(String[] args) {
try {
MedicalReportGenerator generator = new MedicalReportGenerator();
MedicalRecord record = new MedicalRecord();
record.setHospitalName("北京协和医院");
record.setReportType("CT检查");
record.setPatientName("张三");
record.setPatientGender("男");
record.setPatientAge(45);
record.setMedicalRecordNumber("20230615001");
record.setExamType("胸部CT平扫");
record.setExamDate(new Date());
record.setDepartment("放射科");
record.setDiagnosis("1. 右肺上叶见一磨玻璃结节,直径约8mm,边界尚清,建议3个月后复查;2. 双肺散在纤维条索影,考虑陈旧性病变;3. 纵隔内未见明显肿大淋巴结;4. 心脏大小形态正常。");
record.setMedicalAdvice("1. 建议3个月后复查胸部CT;2. 如有咳嗽、胸痛等症状加重,请及时就诊;3. 戒烟,避免呼吸道刺激;4. 定期体检。");
record.setDoctorName("李四");
generator.generateReport(record);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.Date;
public class MedicalRecord {
private String hospitalName;
private String reportType;
private String patientName;
private String patientGender;
private int patientAge;
private String medicalRecordNumber;
private String examType;
private Date examDate;
private String department;
private String diagnosis;
private String medicalAdvice;
private String doctorName;
// Getter和Setter方法
public String getHospitalName() {
return hospitalName;
}
public void setHospitalName(String hospitalName) {
this.hospitalName = hospitalName;
}
public String getReportType() {
return reportType;
}
public void setReportType(String reportType) {
this.reportType = reportType;
}
public String getPatientName() {
return patientName;
}
public void setPatientName(String patientName) {
this.patientName = patientName;
}
public String getPatientGender() {
return patientGender;
}
public void setPatientGender(String patientGender) {
this.patientGender = patientGender;
}
public int getPatientAge() {
return patientAge;
}
public void setPatientAge(int patientAge) {
this.patientAge = patientAge;
}
public String getMedicalRecordNumber() {
return medicalRecordNumber;
}
public void setMedicalRecordNumber(String medicalRecordNumber) {
this.medicalRecordNumber = medicalRecordNumber;
}
public String getExamType() {
return examType;
}
public void setExamType(String examType) {
this.examType = examType;
}
public Date getExamDate() {
return examDate;
}
public void setExamDate(Date examDate) {
this.examDate = examDate;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDiagnosis() {
return diagnosis;
}
public void setDiagnosis(String diagnosis) {
this.diagnosis = diagnosis;
}
public String getMedicalAdvice() {
return medicalAdvice;
}
public void setMedicalAdvice(String medicalAdvice) {
this.medicalAdvice = medicalAdvice;
}
public String getDoctorName() {
return doctorName;
}
public void setDoctorName(String doctorName) {
this.doctorName = doctorName;
}
}
javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ReportTemplateGenerator {
public static void generateTemplate() {
int width = 800;
int height = 1200;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
// 设置背景色为白色
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
// 绘制报告单边框
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(50, 50, width - 100, height - 100);
// 绘制标题分隔线
g2d.drawLine(100, 180, width - 100, 180);
// 绘制患者信息分隔线
g2d.drawLine(100, 250, width - 100, 250);
// 绘制诊断结果分隔线
g2d.drawLine(100, 330, width - 100, 330);
// 绘制医嘱分隔线
g2d.drawLine(100, 680, width - 100, 680);
// 绘制页脚分隔线
g2d.drawLine(100, 980, width - 100, 980);
g2d.dispose();
try {
ImageIO.write(image, "png", new File("medical_template.png"));
System.out.println("模板图片已生成: medical_template.png");
} catch (IOException e) {
System.err.println("保存模板图片失败: " + e.getMessage());
}
}
public static void main(String[] args) {
generateTemplate();
}
}