将Blob转换为Go中的图像

huangapple go评论97阅读模式
英文:

Convert Blob to Image in Go

问题

我正在尝试在Go语言中创建一个mbtiles服务器。该文件遵循mbtiles规范
tile_data字段是一个BLOB,我正在尝试查询数据库并获取相应的图像(以blob类型存储)。

到目前为止,查询是正确的,但是我得到了损坏的tile_data结果。
我不确定如何将SQLite的BLOB映射到正确的Go数据结构。

到目前为止,我尝试使用var tileData []byte,但是没有成功,参考了这个github gombtiles示例。返回值只给我一个只有4个字节的数组,我怀疑这是正确的,因为当我在SQLite GUI中将BLOB视为文本类型时,也只给我4个字符(类似于ÿØÿà)。

总结一下:
tile_data是一个BLOB(存储图像),我无法将其正确转换为Go变量。
如何在Go中将BLOB转换为图像?

更新:

ÿØÿà字符是0xFF 0xD8 0xFF 0xE0,确实是JPEG文件的开头。我正在使用与通过Mapbox iOS SDK在iOS应用程序中使用的相同的MBTile文件,所以文件没有损坏,我实际上可以使用SQLite GUI看到BLOB图像。MBTiles规范指出该字段是一个BLOB类型,并且确实,这是我正在使用的字段类型。
再次强调,数据库是正常的,是外部iOS应用程序使用的相同数据库。我甚至可以成功查询其他数据。

为了提供上下文,这是我隔离的代码。

func TilesHandler(w http.ResponseWriter, r *http.Request) {

  db, _ := sql.Open("sqlite3", "./mapSource.mbtiles")
  defer db.Close()

  rows, _ := db.Query("SELECT tile_data FROM tiles WHERE zoom_level = 10 AND tile_column = 309 AND tile_row = 569")
  defer rows.Close()

  var tileData []byte
  for rows.Next() {
    rows.Scan(&tileData)
  }

  w.Write(tileData)
}

缩放级别和坐标是硬编码的,以简化问题。tiledata返回了上述提到的4个字节。

更新2:
这是tiles表的一个特定记录的输出。请注意,tile_data字段是一个BLOB,在底部的GUI中显示为4个字符。这正是我在Go代码中接收到的[]byte数组。

**所以我的问题是:**我在数据库中有这个BLOB图像文件,它是一个JPEG图像。我如何读取它并在我的网页中显示为图像?我想读取该BLOB并在我的HTTP请求中返回一个图像。

Tiles行示例:

将Blob转换为Go中的图像

Tiles字段类型

将Blob转换为Go中的图像

英文:

I'm trying to create an mbtiles server in Go. The file follows the mbtiles spec.
The tile_data field is a BLOB and i'm trying to query the database and get the corresponding images (stored as blob types).

So far the queries are OK but i'm getting corrupted tile_data results.
I'm not sure how to map a SQLite BLOB to a proper Go data structure.

So far i tried using var tileData []byte without success following this github gombtiles sample. The return value gets me an array of only 4 bytes which i suspect is kinda true since when i see the BLOB as Text type in a SQLite GUI it gets me 4 characters too (something like ÿØÿà)

To summarize:
The tile_data is a BLOB (storing an image) and i can't get a proper conversion to a Go var.
How can i convert a BLOB to Image in Go ?

UPDATE:

The ÿØÿà characters are 0xFF 0xD8 0xFF 0xE0 which are indeed the start of the JPEG file. I'm using the same MBTile file that i use in an iOS app through the Mapbox iOS SDK, so the file isn't corrupted and i can actually see the BLOB images using a SQLite GUI. The MBTiles spec says that the field is a BLOB type, and indeed, that's the field type that i'm using.
Again, the database is fine, is the same one used by an external iOS app. I can even query other data with success.

For context this is my isolated code.

func TilesHandler(w http.ResponseWriter, r *http.Request) {

  db, _ := sql.Open("sqlite3", "./mapSource.mbtiles")
  defer db.Close()

  rows, _ := db.Query("SELECT tile_data FROM tiles WHERE zoom_level = 10 AND tile_column = 309 AND tile_row = 569")
  defer rows.Close()

  var tileData []byte
  for rows.Next() {
    rows.Scan(&tileData)
  }

  w.Write(tileData)
}

The zoom and coordinates are hardcoded for simplicity. tiledata is returning the mentioned 4 bytes.

UPDATE 2:
Here is the output of one specific record of the tiles table. Notice that the tile_data field is a BLOB and at the bottom the GUI shows that 4 characters. That are the exact one that i'm receiving in the []byte array at my Go code.

So my question is: I have this BLOB image file in my database which is a jpeg image. How i read it and show it as an image in my web page ? I want to read that BLOB and return an image in my http request.

Tiles row sample:

将Blob转换为Go中的图像

Tiles fields types

将Blob转换为Go中的图像

答案1

得分: 1

这是sqlite3驱动程序中的一个错误。它没有正确处理包含NUL字符的字符串。现在已经修复了。

此外,数据没有正确插入到数据库中。它们的数据类型应该是BLOB,而不是模式所要求的TEXT。你可以在sqlite shell中使用以下命令查看:SELECT typeof(tile_data) FROM tiles;1

英文:

This was a bug in the sqlite3 driver. It wasn't handling strings that contain NUL characters properly. It is now fixed.

Also the data have not been inserted correctly into the database. The datatype for them is TEXT and not BLOB as required by the schema. You can see this in the sqlite shell with: SELECT typeof(tile_data) FROM tiles;. 1

huangapple
  • 本文由 发表于 2015年4月5日 07:29:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/29452538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定