今天小编给大家分享一下go语言怎么实现屏幕截图的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
go get github.com/kbinani/screenshot
func Capture(x, y, width, height int) (*image.RGBA, error)
返回指定桌面区域的屏幕截图。x,y是起点坐标, width,height 是图片的宽和高。
func CaptureDisplay(displayIndex int) (*image.RGBA, error)
返回全屏截图。 displayIndex 是显示编号,默认屏幕是0,如果外接多个显示,则是1,2,3,4 … 。
func NumActiveDisplays()int
返回活动的显示器的数量。
func GetDisplayBounds(displayIndex int) image.Rectangle
GetDisplayBounds返回displayIndex的显示范围, 范围从(0,0) 坐标开始,当前屏幕分辨率结束 ,例如:(0,0)-(1920,1080)。
func CaptureRect(rect image.Rectangle) (*image.RGBA, error)
捕获桌面的指定区域。跟Capture类似,主要搭配GetDisplayBounds 使用。
参数是一个矩形,即两个点,一个最小点,一个最大点
package main
import (
"fmt"
"github.com/kbinani/screenshot"
"image"
"image/png"
"os"
)
// save *image.RGBA to filePath with PNG format.
func save(img *image.RGBA, filePath string) {
file, err := os.Create(filePath)
if err != nil {
panic(err)
}
defer file.Close()
png.Encode(file, img)
}
func main() {
//自定义截图
img, err := screenshot.Capture(0, 0, 500, 500)
if err != nil {
panic(err)
}
save(img, "自定义.png")
//获取所有活动屏幕
n := screenshot.NumActiveDisplays()
if n <= 0 {
panic("没有发现活动的显示器")
}
//全屏截取所有活动屏幕
if n > 0 {
for i := 0; i < n; i++ {
img, err = screenshot.CaptureDisplay(i)
if err != nil {
panic(err)
}
fileName := fmt.Sprintf("第%d屏幕截图.png", i)
save(img, fileName)
}
}
//使用 Rectangle 自定义截图
// 获取第一个屏幕显示范围
var new image.Rectangle = image.Rect(0, 0, 600, 700)
img, err = screenshot.CaptureRect(new)
if err != nil {
panic(err)
}
save(img, "new.png")
//使用 GetDisplayBounds获取指定屏幕显示范围,全屏截图
bounds := screenshot.GetDisplayBounds(0)
img, err = screenshot.CaptureRect(bounds)
if err != nil {
panic(err)
}
save(img, "all.png")
//使用Union获取指定屏幕 矩形最小点和最大点
var all image.Rectangle = image.Rect(0, 0, 0, 0)
bounds1 := screenshot.GetDisplayBounds(0)
all = bounds1.Union(all)
fmt.Println(all.Min.X, all.Min.Y, all.Dx(), all.Dy())
}
以上就是“go语言怎么实现屏幕截图”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。