图片处理模块

This commit is contained in:
李琦
2026-06-30 12:30:49 +08:00
parent 3882f33058
commit 617760a319
36 changed files with 2293 additions and 2102 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"os/exec"
@@ -122,6 +123,29 @@ func (h *FileHandler) OpenFileDialog(title string, filters []string) (string, er
return path, err
}
func (h *FileHandler) OpenFilesDialog(title string, filters []string) ([]string, error) {
var fileFilters []wailsRuntime.FileFilter
if title == "" {
title = "选择文件"
}
if len(filters) == 0 {
filters = []string{"*"}
}
for _, f := range filters {
displayName := "所有文件 (*.*)"
switch f {
case "*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.tiff;*.webp;*.ico":
displayName = "图片文件 (*.jpg;*.png;*.gif;*.bmp;*.tiff;*.webp;*.ico)"
case "*.pdf":
displayName = "PDF 文件 (*.pdf)"
}
fileFilters = append(fileFilters, wailsRuntime.FileFilter{DisplayName: displayName, Pattern: f})
}
paths, err := wailsRuntime.OpenMultipleFilesDialog(h.ctx, wailsRuntime.OpenDialogOptions{Title: title, Filters: fileFilters})
return paths, err
}
func (h *FileHandler) OpenSaveDialog(title string, defaultFilename string, filters []string, defaultDir string) (string, error) {
if title == "" {
title = "保存文件"
@@ -165,6 +189,24 @@ func (h *FileHandler) OpenFolder(path string) error {
return cmd.Start()
}
// SaveBase64ToFile 将 base64 数据写入指定路径
func (h *FileHandler) SaveBase64ToFile(base64Data string, outputPath string) FileResult {
if base64Data == "" {
return FileResult{Success: false, Message: "数据为空"}
}
if outputPath == "" {
return FileResult{Success: false, Message: "输出路径为空"}
}
data, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
return FileResult{Success: false, Message: "解码失败: " + err.Error()}
}
if err := os.WriteFile(outputPath, data, 0644); err != nil {
return FileResult{Success: false, Message: "写入文件失败: " + err.Error()}
}
return FileResult{Success: true, Path: outputPath, Size: int64(len(data))}
}
func (h *FileHandler) GetConfig() AppConfig {
config := AppConfig{DefaultOutputDir: getDefaultOutputDir()}
data, err := os.ReadFile(getConfigPath())
@@ -695,6 +737,45 @@ func (h *FileHandler) ExtractPDFText(path string) (string, error) {
return h.pdfService.ExtractText(path)
}
func (h *FileHandler) StitchImages(inputPaths []string, direction string, gap int, bgColor string, outputPath string) FileResult {
if len(inputPaths) < 2 {
return FileResult{Success: false, Message: "至少需要 2 张图片才能拼接"}
}
// 确定输出路径
if outputPath == "" {
config := h.GetConfig()
outDir := config.DefaultOutputDir
if outDir == "" {
outDir = filepath.Dir(inputPaths[0])
}
outputPath = filepath.Join(outDir, "拼接长图.png")
}
resultPath, err := h.imageService.StitchImages(services.StitchRequest{
InputPaths: inputPaths,
Direction: direction,
Gap: gap,
BgColor: bgColor,
OutputPath: outputPath,
})
if err != nil {
return FileResult{Success: false, Message: err.Error()}
}
info, statErr := os.Stat(resultPath)
if statErr != nil {
return FileResult{Success: false, Message: fmt.Sprintf("获取文件信息失败: %v", statErr)}
}
return FileResult{
Success: true,
Message: fmt.Sprintf("拼接完成,共 %d 张图片", len(inputPaths)),
Path: resultPath,
Size: info.Size(),
}
}
func (h *FileHandler) LogError(msg string) {
h.writeLog("ERROR", msg)
}