dpwtr666638 2019-02-07 15:52
浏览 155

从HTTP Golang中的字符串提供图片

I need to display image with HTTP GET but the thing is i can only use String as the response body.

So for example (headers:image/png, body:Aeacxxffsaf(encoded representation or whatever) )

It's more or less like this web https://codebeautify.org/base64-to-image-converter, but i want the string to output into image when using http GET.

Some code snippets explanations here:

//string that is generated from image (encoded)
encString := "iVBORw0KGgoAAAANSUhEUgAAANIAAAAzCAYAAADigVZl..."

//set http headers to png
//and assign the encString to the body

Is there any way for that? By using string only to serve image

Sorry if my question is a bit confusing but it is the best i can describe it, i have been searching for the answer since several days ago

  • 写回答

2条回答 默认 最新

  • dsgdhf5674 2019-02-07 15:59
    关注

    It depends on how you interpret it on the other side. The encoded b64, b32, byte array, etc. string all represent the same pattern of bytes. To get it to display as an image on the other side all depends on how you interpret it from the other side.

    EDIT:

    I see what you mean now. Have a look at the image/jpeg package.

    Sadly I don't have a code snippet to share with you right now, but with this you should be able to load your image onto a buffer and decode it to get an image.Image object.

    Afterwards you can use that image.Image object to write it into your response body. Sadly I can't provide a code snippet right this second, but do let me know if it works for you.

    remember to set your writer's appropriate header.

    w.Header().Set("Content-Type", "image/jpeg")

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧