forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
572 B
28 lines
572 B
package jpeg
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"image/jpeg"
|
|
"io"
|
|
)
|
|
|
|
// Options represent JPEG encoding options.
|
|
type Options struct {
|
|
Quality int `json:"quality"`
|
|
}
|
|
|
|
// DecodeJPEG decodes a JPEG file and return an image.
|
|
func DecodeJPEG(r io.Reader) (image.Image, error) {
|
|
i, err := jpeg.Decode(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return i, nil
|
|
}
|
|
|
|
// EncodeJPEG encodes an image into JPEG and returns a buffer.
|
|
func EncodeJPEG(i image.Image, o *Options) (buf bytes.Buffer, err error) {
|
|
err = jpeg.Encode(&buf, i, &jpeg.Options{Quality: o.Quality})
|
|
return buf, err
|
|
}
|
|
|