# screenshot **Repository Path**: chanterchen/screenshot ## Basic Information - **Project Name**: screenshot - **Description**: No description available - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-29 - **Last Updated**: 2026-04-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 根据代码地图信息,我来为这个项目编写一份专业的 README 文档。 --- # Screenshot 一个使用 Go 语言和 CGO 实现的跨平台屏幕截图库,支持全屏截图、区域截图以及多种输出格式(PixelBuffer、PNG、JPG)。 ## 功能特性 - **区域截图**:指定坐标和尺寸截取屏幕任意区域 - **全屏截图**:一键捕获整个屏幕 - **多种格式**:支持 PixelBuffer、PNG、JPG 三种输出格式 - **高性能**:使用 CGO 直接调用系统底层 API - **易于集成**:提供简洁的 C 风格 API,方便其他语言调用 ## 项目结构 ``` . ├── main.go # 主要源代码(CGO 实现) ├── go.mod # Go 模块定义 ├── go.sum # 依赖校验和 ├── makefile # 构建脚本 └── LICENSE # 项目许可 ``` ## 支持的操作系统 - Windows(主要支持) - macOS - Linux ## 安装 ### 前提条件 - Go 1.16 或更高版本 - GCC 编译器(如在 Linux/macOS 上构建) ### 构建 ```bash # 克隆项目 git clone https://gitee.om/chanterchen/screenshot.git cd screenshot # 构建动态链接库 make # 或手动构建 go build -buildmode=c-shared -o screenshot.so main.wo ``` ## 使用示例 ### Go 语言调用 ```go package main /* #cgo CFLAGS: -I. #cgo linux LDFLAGS: -lX11 -lXfixes #include "screenshot.h" */ import "C" import ( "fmt" "unsafe" ) func captureArea(x, y, w, h int) ([]byte, error) { var length C.int ptr := C.CaptureAreaPixelBuffer( C.int(x), C.int(y), C.int(w), C.int(h), (*C.int)(unsafe.Pointer(&length)), ) if ptr == 0 { return nil, fmt.Errorf("capture failed") } defer C.Free(ptr) data := C.GoBytes(unsafe.Pointer(ptr), length) return data, nil } func main() { img, err := captureArea(0, 0, 1920, 1080) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Captured %d bytes\n", len(img)) } ``` ### 保存为图片文件 ```go // 保存为 PNG C.CaptureAreaPNGBuffer(x, y, w, h, &length) C.CaptureFullPNGBuffer(&length) // 保存为 JPG C.CaptureAreaJPGBuffer(x, y, w, h, quality, &length) C.CaptureFullJPGBuffer(quality, &length) ``` ## API 参考 ### 核心函数 | 函数 | 说明 | |------|------| | `CaptureArea(x, y, w, h, saveDir)` | 截取指定区域并保存到文件 | | `CaptureFull(saveDir)` | 全屏截图并保存到文件 | | `CaptureAreaPixelBuffer(x, y, w, h, *lenOut)` | 截取区域返回像素缓冲 | | `CaptureAreaPNGBuffer(x, y, w, h, *lenOut)` | 截取区域返回 PNG 数据 | | `CaptureAreaJPGBuffer(x, y, w, h, quality, *lenOut)` | 截取区域返回 JPG 数据 | | `CaptureFullPixelBuffer(*lenOut)` | 全屏返回像素缓冲 | | `CaptureFullPNGBuffer(*lenOut)` | 全屏返回 PNG 数据 | | `CaptureFullJPGBuffer(quality, *lenOut)` | 全屏返回 JPG 数据 | | `Free(ptr)` | 释放返回的内存指针 | ### 参数说明 - `x`, `y`:截图起始坐标(左上角) - `w`, `h`:截图区域宽度和高度 - `saveDir`:保存目录路径 - `quality`:JPG 压缩质量(1-100) - `*lenOut`:输出数据长度 ## 性能对比 | 格式 | 优点 | 适用场景 | |------|------|----------| | PixelBuffer | 无损、获取原始像素数据 | 图像处理、分析 | | PNG | 无损压缩、透明度支持 | 文档存档、UI 截图 | | JPG | 高压缩比、体积小 | 网络传输、预览 | ## 注意事项 1. **内存管理**:使用 `Free()` 函数释放返回的内存指针 2. **权限要求**:Linux 下可能需要 display server 访问权限 3. **坐标系统**:使用屏幕坐标系,原点为左上角 ## 许可证 项目采用 MIT 许可证,详见 [LICENSE](LICENSE) 文件。 ## 贡献指南 欢迎提交 Issue 和 Pull Request! ---