Add Android client support and unify cross-platform builds

This commit is contained in:
2026-03-22 21:25:09 +08:00
parent 6558d1acdb
commit 4210ab7675
44 changed files with 2241 additions and 328 deletions

View File

@@ -1,3 +1,5 @@
//go:build windows || linux || darwin
package utils
import (
@@ -9,34 +11,27 @@ import (
"github.com/kbinani/screenshot"
)
// CaptureScreenshot 捕获主屏幕截图
// quality: JPEG 质量 (1-100), 0 使用默认值 (75)
// 返回: JPEG 图片数据, 宽度, 高度, 错误
// CaptureScreenshot captures the primary display.
func CaptureScreenshot(quality int) ([]byte, int, int, error) {
// 默认质量
if quality <= 0 || quality > 100 {
quality = 75
}
// 获取活动显示器数量
n := screenshot.NumActiveDisplays()
if n == 0 {
return nil, 0, 0, fmt.Errorf("no active display found")
}
// 获取主显示器边界
bounds := screenshot.GetDisplayBounds(0)
if bounds.Empty() {
return nil, 0, 0, fmt.Errorf("failed to get display bounds")
}
// 捕获屏幕
img, err := screenshot.CaptureRect(bounds)
if err != nil {
return nil, 0, 0, fmt.Errorf("capture screen: %w", err)
}
// 编码为 JPEG
var buf bytes.Buffer
opts := &jpeg.Options{Quality: quality}
if err := jpeg.Encode(&buf, img, opts); err != nil {
@@ -46,40 +41,30 @@ func CaptureScreenshot(quality int) ([]byte, int, int, error) {
return buf.Bytes(), bounds.Dx(), bounds.Dy(), nil
}
// CaptureAllScreens 捕获所有屏幕并拼接
// quality: JPEG 质量 (1-100), 0 使用默认值 (75)
// 返回: JPEG 图片数据, 宽度, 高度, 错误
// CaptureAllScreens captures all active displays and stitches them together.
func CaptureAllScreens(quality int) ([]byte, int, int, error) {
// 默认质量
if quality <= 0 || quality > 100 {
quality = 75
}
// 获取活动显示器数量
n := screenshot.NumActiveDisplays()
if n == 0 {
return nil, 0, 0, fmt.Errorf("no active display found")
}
// 计算所有屏幕的总边界
var totalBounds image.Rectangle
for i := 0; i < n; i++ {
bounds := screenshot.GetDisplayBounds(i)
totalBounds = totalBounds.Union(bounds)
}
// 创建总画布
totalImg := image.NewRGBA(totalBounds)
// 捕获每个屏幕并绘制到总画布
for i := 0; i < n; i++ {
bounds := screenshot.GetDisplayBounds(i)
img, err := screenshot.CaptureRect(bounds)
if err != nil {
continue // 跳过失败的屏幕
continue
}
// 绘制到总画布
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
totalImg.Set(x, y, img.At(x-bounds.Min.X, y-bounds.Min.Y))
@@ -87,7 +72,6 @@ func CaptureAllScreens(quality int) ([]byte, int, int, error) {
}
}
// 编码为 JPEG
var buf bytes.Buffer
opts := &jpeg.Options{Quality: quality}
if err := jpeg.Encode(&buf, totalImg, opts); err != nil {

View File

@@ -0,0 +1,15 @@
//go:build !windows && !linux && !darwin
package utils
import "fmt"
// CaptureScreenshot is not available on this platform.
func CaptureScreenshot(quality int) ([]byte, int, int, error) {
return nil, 0, 0, fmt.Errorf("screenshot not supported on this platform")
}
// CaptureAllScreens is not available on this platform.
func CaptureAllScreens(quality int) ([]byte, int, int, error) {
return nil, 0, 0, fmt.Errorf("screenshot not supported on this platform")
}