-
I would like to view the image data received by subscriber but don't know where to start. Can someone please help me? Example code: package main
import (
"github.com/aler9/goroslib"
"github.com/aler9/goroslib/pkg/msgs/sensor_msgs"
)
func main() {
// create a node and connect to the master
n, err := goroslib.NewNode(goroslib.NodeConf{
Name: "goroslib_sub",
MasterAddress: "127.0.0.1:11311",
})
if err != nil {
panic(err)
}
defer n.Close()
onMessage := func(msg *sensor_msgs.Image) {
// TODO: Do something to view the image
}
// create a subscriber
sub, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
Node: n,
Topic: "goroslib_sub",
Callback: onMessage,
})
if err != nil {
panic(err)
}
defer sub.Close()
// freeze main loop
select {}
} |
Beta Was this translation helpful? Give feedback.
Answered by
aler9
Sep 8, 2021
Replies: 1 comment
-
Here's a working example, that works with the package main
import (
"image"
"bytes"
"image/jpeg"
"io/ioutil"
"time"
"github.com/aler9/goroslib"
"github.com/aler9/goroslib/pkg/msgs/sensor_msgs"
)
func main() {
// create a node and connect to the master
n, err := goroslib.NewNode(goroslib.NodeConf{
Name: "goroslib_sub",
MasterAddress: "127.0.0.1:11311",
})
if err != nil {
panic(err)
}
defer n.Close()
onMessage := func(msg *sensor_msgs.Image) {
if msg.Encoding != "rgba" {
panic("not rgba")
}
r := image.Rectangle{image.Point{0, 0}, image.Point{int(msg.Width), int(msg.Height)}}
i := &image.RGBA{
Pix: msg.Data,
Stride: 4 * r.Dx(),
Rect: r,
}
var buf bytes.Buffer
err := jpeg.Encode(&buf, i, &jpeg.Options{
Quality: 80,
})
if err != nil {
panic(err)
}
err = ioutil.WriteFile(time.Now().Format("myimage.jpg"), buf.Bytes(), 0644)
if err != nil {
panic(err)
}
}
// create a subscriber
sub, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
Node: n,
Topic: "goroslib_sub",
Callback: onMessage,
})
if err != nil {
panic(err)
}
defer sub.Close()
// freeze main loop
select {}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Maverobot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a working example, that works with the
rgba
encoding (if it's different, you need to convert it)