大佬教程收集整理的这篇文章主要介绍了在 Mac 上的终端或 IDE 中运行简单的 GOLANG 应用程序之间的区别,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用 golang 语言制作我的 mac 桌面的屏幕截图。有一个很好且简单的工具:https://github.com/kbinani/screenshot 我已经使用它有一段时间了,但最近我尝试再次使用它,并注意到我的两台 mACBook(big sur 和 catalina)上有一个奇怪的行为。
这是一个简单的代码:
package main
import (
"fmt"
"github.com/kbinani/screenshot"
"image/png"
"os"
)
func main(){
bounds := screenshot.GetdisplayBounds(0)
img,err := screenshot.CaptureRect(bounds)
if err != nil {
panic(err)
}
filename := fmt.Sprintf("%d_%dx%d.png",bounds.Dx(),bounds.Dy())
file,_ := os.Create(fileName)
defer file.Close()
png.Encode(file,img)
fmt.Printf("#%d : %v \"%s\"\n",bounds,fileName)
}
上面的代码应该捕获第一个屏幕并将文件保存在二进制文件附近,如“0_2560x1440.png”。 当我从 jetbrains Goland IDE 运行二进制文件或当我使用 go run main.go 命令在 jetbrains Goland IDE 终端内一切正常。但是,如果我从默认终端(或 iTerm)运行二进制文件或 go run main.go,我将收到没有任何窗口的屏幕截图,只是一个没有任何 windows 或文件夹的普通桌面。
这是什么原因?从 IDE 终端或 OS 终端运行二进制文件有什么区别?
经过一番考虑,我认为可能在 golang 屏幕截图库中的某个地方存在错误。我尝试使用 OSX API 运行另一个代码:
package main
// To use the two librarIEs we need to define the respective flags,include the required header files and import "C" immediately after
import (
// #cgo LDFLAGS: -framework CoreGraphics
// #cgo LDFLAGS: -framework CoreFoundation
// #include <CoreGraphics/CoreGraphics.h>
// #include <CoreFoundation/CoreFoundation.h>
"C"
"image"
"image/png"
"os"
"reflect"
"unsafe"
// other packages...
)
func main() {
displayID := C.CGMaindisplayID()
wIDth := int(C.CGdisplayPixelsWIDe(displayID))
height := int(C.CGdisplayPixelsHigh(displayID))
rawData := C.CGDataProvIDercopyData(C.CGImageGetDataProvIDer(C.CGdisplayCreateImage(displayID)))
length := int(C.CFDataGetLength(rawData))
ptr := unsafe.Pointer(C.CFDataGetBytePtr(rawData))
var slice []byte
hdrp := (*reflect.Sliceheader)(unsafe.Pointer(&slicE))
hdrp.Data = uintptr(ptr)
hdrp.Len = length
hdrp.Cap = length
imageBytes := make([]byte,length)
for i := 0; i < length; i += 4 {
imageBytes[i],imageBytes[i+2],imageBytes[i+1],imageBytes[i+3] = slice[i+2],slice[i],slice[i+1],slice[i+3]
}
//C.CFRelease(rawData)
img := &image.RGBA{Pix: imageBytes,StriDe: 4 * wIDth,Rect: image.Rect(0,wIDth,height)}
// There we go,we can Now save or process the image further
file,err := os.Create("file.png")
if err != nil {
panic(err)
}
defer file.Close()
if err := png.Encode(file,img); err != nil {
panic(err)
}
}
这段代码给出了相同的结果。在 IDE 终端中运行 - 正常行为。在其他地方运行 - 屏幕截图现在有内容。
请帮我揭开它的神秘面纱。
经过一番调查,结果发现问题只是 OSX 权限。有录屏权限。 IDE 允许,但终端不允许。
没有弹出窗口或类似的东西。
以上是大佬教程为你收集整理的在 Mac 上的终端或 IDE 中运行简单的 GOLANG 应用程序之间的区别全部内容,希望文章能够帮你解决在 Mac 上的终端或 IDE 中运行简单的 GOLANG 应用程序之间的区别所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。