daShangDao_kfzgw-info/image/imageDllTest.go
2025-12-22 19:09:56 +08:00

88 lines
2.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"syscall"
"unsafe"
)
// ImageDLL 图片处理DLL结构
type imageDLL struct {
dll *syscall.DLL
processImage *syscall.Proc
freeCString *syscall.Proc
}
// 初始化imageDLL
func InitImageDll() (*imageDLL, error) {
dllPath := filepath.Join("image", "dll", "image.dll")
if _, err := os.Stat(dllPath); os.IsNotExist(err) {
return nil, fmt.Errorf("image DLL 不存在: %s", dllPath)
}
if dll, err := syscall.LoadDLL(dllPath); err != nil {
return nil, fmt.Errorf("加载image DLL 失败: %s", err)
} else {
return &imageDLL{
dll: dll,
processImage: dll.MustFindProc("ProcessImage"),
freeCString: dll.MustFindProc("FreeCString"),
}, nil
}
}
func (m *imageDLL) ProcessImage(config *Config) error {
marshal, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("json转换失败: %s", err)
}
fromString, _ := syscall.BytePtrFromString(string(marshal))
info, _, _ := m.processImage.Call(uintptr(unsafe.Pointer(fromString)))
cStr(info)
return nil
}
// cStr 将 C 字符串指针转换为 Go 字符串
func cStr(ptr uintptr) string {
if ptr == 0 {
return ""
}
var b []byte
for {
c := *(*byte)(unsafe.Pointer(ptr))
if c == 0 {
break
}
b = append(b, c)
ptr++
}
return string(b)
}
//
//func main() {
//
// config := &Config{
// InputDir: "D:\\www\\wwwroot\\imageTool\\img\\image", // 输入图片目录
// OutputDir: "D:\\isbn_images\\result", // 输出根目录
// FileName: "D:\\isbn_images\\result\\9771671688095.jpg",
// MatchDir: "matched", // 满足条件的图片目录
// UnmatchDir: "unmatched", // 不满足条件的图片目录
// EqualHeightDir: "equalHeight",
// MinWhitePct: 0.1, // 纯白占比下限 10%
// MaxWhitePct: 0.65, // 纯白占比上限 90%
// Extensions: []string{"jpg", "jpeg", "png", "gif", "bmp", "webp"},
// }
//
// dll, err := InitImageDll()
// if err != nil {
// fmt.Println(err)
// }
// err = dll.ProcessImage(config)
// if err != nil {
// fmt.Println(err)
// }
//}