drll42469 2015-04-04 23:29
浏览 241
已采纳

在Go中将Blob转换为图像

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:

enter image description here

Tiles fields types

enter image description here

  • 写回答

1条回答 默认 最新

  • duanmorong9597 2015-04-13 06:56
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?