go-vgo / robotgo

RobotGo, Go Native cross-platform RPA and GUI automation @vcaesar
Apache License 2.0
9.61k stars 881 forks source link

Convert image []byte to C.MMBitmapRef #217

Open InfeCtlll3 opened 5 years ago

InfeCtlll3 commented 5 years ago

Hello, I'm trying to use this Lib to search blob images from a Sqlite3 db in a bigger Bitmap.

When I fetch these images from the db, they come as []byte (db store them as blobs) and I need to convert those images to C.MMBitmapRef in some way. I have tried converting them myself but had no success.

The work around I'm using is: Convert []byte to Image.image type, save it to png and then open using OpenBitmap in order to use FindBitmap.

Is there a way to convert either from []byte or Image.image type directly to C.MMBitmapRef type without having to save them to png and then openning again?

My sample code for fetching my blobs from the Database:

package main
import (
    "bytes"
    "database/sql"
    "image"
    "image/png"
    _ "github.com/mattn/go-sqlite3"
)
func main() {
    database, _ := sql.Open("sqlite3", "info.db")
    rows := database.QueryRow("SELECT image FROM marks where id=3")
        // variable id is holding the blob image file as type []bytes
    var id []byte
    rows.Scan(&id)
        // process to convert []bytes to Image.image type and then save it to png.
    img, _, _ := image.Decode(bytes.NewReader(id))
    out, err := os.Create("./output.png")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    err = png.Encode(out, img)
}
wilon commented 5 years ago

Same question. func ToBitmapBytes(bit C.MMBitmapRef) []byte convert C.MMBitmapRef to []byte. But how to convert []byte to C.MMBitmapRef?

deepio commented 4 years ago

It's not yet implemented, this is the only options currently. https://github.com/go-vgo/robotgo/blob/b49f16ed0a323a6a4bd980e8c9a7eacc6c9e87b8/base/bmp_io_c.h#L144-L246