zouyu
2025-09-26 3fbbfcc8f509c352c58dc8a126220b49b72ed5a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.xindao.ocr.swingui.swing;
 
import com.xindao.ocr.swingui.service.OcrService;
import com.xindao.ocr.swingui.swing.jpanel.ContractNumberProcessPanel;
import com.xindao.ocr.swingui.swing.jpanel.MultipleAreaProcessPanel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
 
import javax.annotation.PostConstruct;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
 
@org.springframework.stereotype.Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 设置为原型作用域,每次获取创建新实例
public class FileProcessorApp extends JFrame {
 
    @Autowired
    private OcrService ocrService;
 
    private static final Font DEFAULT_FONT;
 
    // 颜色定义
    private static final Color BACKGROUND_COLOR = new Color(245, 245, 247);
    private static final Color TEXT_COLOR = new Color(51, 51, 51);
    private static final Color PRIMARY_COLOR = new Color(66, 133, 244);
    private static final Color TEXT_LIGHT = new Color(102, 102, 102);
 
    static {
        // 字体设置
        if (isFontAvailable("Microsoft YaHei")) {
            DEFAULT_FONT = new Font("Microsoft YaHei", Font.PLAIN, 12);
        } else if (isFontAvailable("SimHei")) {
            DEFAULT_FONT = new Font("SimHei", Font.PLAIN, 12);
        } else if (isFontAvailable("WenQuanYi Micro Hei")) {
            DEFAULT_FONT = new Font("WenQuanYi Micro Hei", Font.PLAIN, 12);
        } else {
            DEFAULT_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
        }
    }
 
    private static boolean isFontAvailable(String fontName) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = ge.getAvailableFontFamilyNames();
        for (String name : fontNames) {
            if (name.equals(fontName)) {
                return true;
            }
        }
        return false;
    }
 
    public FileProcessorApp() {
        setTitle("OCR图像处理工具");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(850, 700);
        setLocationRelativeTo(null);
        setResizable(true);
 
        // 设置全局字体和背景
        setFont(DEFAULT_FONT);
        getContentPane().setBackground(BACKGROUND_COLOR);
    }
 
    /**
     * 使用@PostConstruct确保依赖注入完成后再初始化UI
     * 避免构造函数中直接使用@Autowired的字段导致null值
     */
    @PostConstruct
    public void initialize() {
        initUI();
        setVisible(true);
    }
 
    private void initUI() {
        // 主面板使用边界布局
        JPanel mainPanel = new JPanel(new BorderLayout(15, 15));
        mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
        mainPanel.setBackground(BACKGROUND_COLOR);
 
        // 创建标签页面板
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setFont(DEFAULT_FONT);
 
        JPanel mainTab = new ContractNumberProcessPanel(BACKGROUND_COLOR, PRIMARY_COLOR, TEXT_COLOR,TEXT_LIGHT,DEFAULT_FONT,this,ocrService).initPanel();
        JPanel extensionPanel = new MultipleAreaProcessPanel(this,ocrService,BACKGROUND_COLOR, PRIMARY_COLOR, TEXT_COLOR, DEFAULT_FONT).initPanel();
        // 添加标签页到标签面板
        tabbedPane.addTab("文件处理", null, mainTab, "");
        tabbedPane.addTab("多区域识别", null, extensionPanel, "");
 
        // 添加标签面板到主面板
        mainPanel.add(tabbedPane, BorderLayout.CENTER);
 
        // 设置内容面板
        setContentPane(mainPanel);
 
    }
 
}