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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀