dsc6517 2014-12-31 06:30
浏览 38
已采纳

将额外的数据写入io.Writer

I am trying to figure out how to change the behaviour of jpeg.Encode API (http://golang.org/pkg/image/jpeg/#Encode) that takes an io.Writer as a parameter and writes the file to it.

I need to insert a set of bytes after the first two bytes written in to the file, and I think it is possible to do without keeping everything in memory. I am new to Golang, I found io.Pipe that connects a reader and a writer, but I do not understand how to use it for what I need.

Could someone help?

EDIT: should I just implement my own io.Writer interface?

  • 写回答

2条回答 默认 最新

  • duanran6441 2014-12-31 09:56
    关注

    If you know upfront the bytes you need to add in the stream (whatever the stream, image or not), building a Writer object for this specific purpose is not really hard.

    Here is an example:

    package main
    
    import (
        "fmt"
        "image"
        "image/draw"
        "image/jpeg"
        "image/color"
        "os"
        "io"
    )
    
    type ByteInserter struct {
        n   int
        b   []byte
        pos int
        w   io.Writer
    }
    
    func NewByteInserter(w io.Writer) *ByteInserter {
        return &ByteInserter{w:w}
    }
    
    func (bi* ByteInserter) Set( b[]byte, pos int ) {
        bi.b = b
        bi.pos = pos
    }
    
    func (bi *ByteInserter) Write( p []byte ) (n int, err error) {
        if bi.n>bi.pos || bi.n+len(p)<=bi.pos {
            n, err = bi.w.Write(p)
            bi.n += n
        } else {
            cut := bi.pos-bi.n
            if cut > 0 {
                n,err = bi.w.Write(p[:cut])
                bi.n += n
                if err != nil {
                    return
                }
            }
            _,err = bi.w.Write(bi.b)
            if err != nil {
                return
            }
            n2 := 0
            n2, err = bi.w.Write(p[cut:])
            bi.n += n2
            n += n2
        }
        return
    }
    
    func main() {
    
        // Blue rectangle, stolen from Nigel Tao's post
        // http://blog.golang.org/go-imagedraw-package
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
        blue := color.RGBA{0, 0, 255, 255}
        draw.Draw(img, img.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)
    
        file, err := os.Create("file.jpg")
        if err != nil {
            panic(err)
        }
    
        bi := NewByteInserter(file)
        bi.Set( []byte("XXX"), 2 )   // Three bytes added at position 2
        jpeg.Encode(bi,img,nil)
        file.Close()
    
        fmt.Println("Written!")
    }
    

    That said, editing a stream to customize an image seems really hackish to me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀