Go程序在Windows上显示图像的最简单方法是什么?我有一个基于教程的片段:
package main
import (
"image"
"image/color"
"image/draw"
)
func main() {
m := image.NewRGBA(image.Rect(0, 0, 640, 480))
blue := color.RGBA{0, 0, 255, 255}
draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)
}
Run Code Online (Sandbox Code Playgroud)
但是如何显示对象m?我想弹出一个窗口并在那里显示图像,而不是先将其写入文件.
小智 7
包gxui中有一个图像查看器示例,它显示从命令行frags中选择的图像.结果可以在这里看到.使用go来呈现这个gui可能是更简单的方法之一.
请注意gxui是经验性的,未来的更新可能会破坏您的代码.
对于您的请求,代码如下.它会生成一个显示图像的窗口,一个完整的蓝色图像.
package main
import (
"image"
"image/color"
"image/draw"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/themes/dark"
)
func appMain(driver gxui.Driver) {
width, height := 640, 480
m := image.NewRGBA(image.Rect(0, 0, width, height))
blue := color.RGBA{0, 0, 255, 255}
draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)
// The themes create the content. Currently only a dark theme is offered for GUI elements.
theme := dark.CreateTheme(driver)
img := theme.CreateImage()
window := theme.CreateWindow(width, height, "Image viewer")
texture := driver.CreateTexture(m, 1.0)
img.SetTexture(texture)
window.AddChild(img)
window.OnClose(driver.Terminate)
}
func main() {
gl.StartDriver(appMain)
}
Run Code Online (Sandbox Code Playgroud)
我确认这适用于Windows和Linux.它可能适用于MacOSX.
如果是用于生产,您可能需要查看更稳定的包.例如,ComputerFellow提到的go-qml或go-qt5
| 归档时间: |
|
| 查看次数: |
4514 次 |
| 最近记录: |