duangang3832 2014-03-21 17:27
浏览 13
已采纳

通过马提尼酒解码后的图像

I am currently playing around with golang and Martini and such and wanted to dynamically serve some manipulated/generated images. Here's a minimal example:

package main

import (
    "github.com/codegangsta/martini"
    "github.com/nfnt/resize"
    "image"
    "image/jpeg"
    "log"
    "os"
)

func thumb() image.Image {
    file, err := os.Open("test.jpg")
    if err != nil {
        log.Fatal(err)
    }

    img, err := jpeg.Decode(file)
    if err != nil {
        log.Fatal(err)
    }
    file.Close()

    m := resize.Resize(0, 200, img, resize.MitchellNetravali)

    return m
}

func main() {
    m := martini.Classic()
    m.Get("/") image.Image {
        return thumb()
    })
    m.Run()
}

That compiles fine, but instead of serving an image, I get some "Content-Type:text/plain; charset=utf-8" that looks like this:

<*image.RGBA64 Value>

I am pretty sure that I need to encode the image again and then serve it. But im not quite sure how to do this without saving the image to the disk...

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dtp0760 2014-03-21 18:06
    关注

    You can write to the ResponseWriter directly because it implements the io.Writer interface, no need to use a buffer or copy the image to disk.

    You were almost there, just needed to set the content type and like you mentioned encode the image.Image object back into a jpeg. Luckily, the jpeg.Encode() method needed a writer to write to and you have the ResponseWriter available at your disposal to do just that thanks to Martini having the ability to inject it into your handler.

    Note: you will probably want to do a more robust job of error handling than I have provided. This is just to get the ball rolling. ;)

    package main
    
    import (
        "image"
        "image/jpeg"
        "log"
        "net/http"
        "os"
    
        "github.com/codegangsta/martini"
        "github.com/nfnt/resize"
    )
    
    func thumb() image.Image {
        file, err := os.Open("test.jpg")
        if err != nil {
            log.Fatal(err)
        }
    
        img, err := jpeg.Decode(file)
        if err != nil {
            log.Fatal(err)
        }
        file.Close()
    
        m := resize.Resize(0, 200, img, resize.MitchellNetravali)
    
        return m
    }
    
    func main() {
        m := martini.Classic()
    
        m.Get("/", func(res http.ResponseWriter, req *http.Request) {
            res.Header().Set("Content-Type", "image/jpeg")
            err := jpeg.Encode(res, thumb(), &jpeg.Options{100})
            if err != nil {
                res.WriteHeader(500)
            } else {
                res.WriteHeader(200)
            }
        })
        m.Run()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊