# image.dll 使用教程 ## 1.创建DLL工具实例 ### 加载DLL文件 ```gotemplate // 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 } } dll,err := InitImageDll() ``` ### 获取C字符串 ```gotemplate // 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) } ``` ## 2.使用dll函数示例 ```gotemplate 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 } ``` # 接口详情 ## 1.检测图片纯白占比--ProcessImage ### 请求信息 ```gotemplate dll.ProcessImage(jsonConfig) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|------------| | jsonConfig | string | 是 | json字符串 | ### json字符串结构体 ```gotemplate // Config 配置结构体 type Config struct { OutputDir string // 输出目录路径 FileName string // 文件名 MatchDir string // 满足条件的图片目录名 UnmatchDir string // 不满足条件的图片目录名 EqualHeightDir string // 等高的图片目录名 WhiteDir string // 白色底图的图片目录名 WhiteBorderPngDir string // 去白边转PNG的图片目录名 MinWhitePct float64 // 纯白占比下限(0-1) MaxWhitePct float64 // 纯白占比上限(0-1) Extensions []string // 支持的图片扩展名 } ``` ### 响应示例 ``` string ``` ## 2.根据原始图片生成新的白底图片--CreateWhiteBottomCenteredImage ### 请求信息 ```gotemplate dll.CreateWhiteBottomCenteredImage(jsonConfig,width,height) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--------|--|---------| | jsonConfig | string | 是 | json字符串 | | width | int | 是 | 宽度 | | height | int | 是 | 高度 | ### json字符串结构体 ```gotemplate // Config 配置结构体 type Config struct { OutputDir string // 输出目录路径 FileName string // 文件名 MatchDir string // 满足条件的图片目录名 UnmatchDir string // 不满足条件的图片目录名 EqualHeightDir string // 等高的图片目录名 WhiteDir string // 白色底图的图片目录名 WhiteBorderPngDir string // 去白边转PNG的图片目录名 MinWhitePct float64 // 纯白占比下限(0-1) MaxWhitePct float64 // 纯白占比上限(0-1) Extensions []string // 支持的图片扩展名 } ``` ### 响应示例 ``` 文件名称:string ``` ## 3.根据高度生成等比例图片--ResizeToHeightQuality ### 请求信息 ```gotemplate dll.ResizeToHeightQuality(jsonConfig,targetHeight) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--------|--|---------| | jsonConfig | string | 是 | json字符串 | | targetHeight | int | 是 | 等比例高度 | ### json字符串结构体 ```gotemplate // Config 配置结构体 type Config struct { OutputDir string // 输出目录路径 FileName string // 文件名 MatchDir string // 满足条件的图片目录名 UnmatchDir string // 不满足条件的图片目录名 EqualHeightDir string // 等高的图片目录名 WhiteDir string // 白色底图的图片目录名 WhiteBorderPngDir string // 去白边转PNG的图片目录名 MinWhitePct float64 // 纯白占比下限(0-1) MaxWhitePct float64 // 纯白占比上限(0-1) Extensions []string // 支持的图片扩展名 } ``` ### 响应示例 ``` 文件名称:string ``` ## 4.去掉白边并转PNG图片工具--RemoveWhiteBorderAndPNG ### 请求信息 ```gotemplate dll.RemoveWhiteBorderAndPNG(jsonConfig) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--------|--|---------| | jsonConfig | string | 是 | json字符串 | ### json字符串结构体 ```gotemplate // Config 配置结构体 type Config struct { OutputDir string // 输出目录路径 FileName string // 文件名 MatchDir string // 满足条件的图片目录名 UnmatchDir string // 不满足条件的图片目录名 EqualHeightDir string // 等高的图片目录名 WhiteDir string // 白色底图的图片目录名 WhiteBorderPngDir string // 去白边转PNG的图片目录名 MinWhitePct float64 // 纯白占比下限(0-1) MaxWhitePct float64 // 纯白占比上限(0-1) Extensions []string // 支持的图片扩展名 } ``` ### 响应示例 ``` 文件名称:string ``` ## 9.释放C字符串内存--FreeCString ### 请求信息 ```gotemplate dll.FreeCString(str) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|----------| | str | string | 是 | 需要释放的字符串 |