本文整理匯總了Golang中image/jpeg.Decode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Decode函數的具體用法?Golang Decode怎麽用?Golang Decode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Decode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Fuzz
func Fuzz(data []byte) int {
cfg, err := jpeg.DecodeConfig(bytes.NewReader(data))
if err != nil {
return 0
}
if cfg.Width*cfg.Height > 1e6 {
return 0
}
img, err := jpeg.Decode(bytes.NewReader(data))
if err != nil {
return 0
}
for q := 0; q <= 100; q += 10 {
var w bytes.Buffer
err = jpeg.Encode(&w, img, &jpeg.Options{q})
if err != nil {
panic(err)
}
img1, err := jpeg.Decode(&w)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(img.Bounds(), img1.Bounds()) {
panic("bounds changed")
}
}
return 1
}
示例2: CropImage
func (c Creature) CropImage(width, height, max int, ctx appengine.Context) (image.Image, error) {
url, _ := aeimg.ServingURL(ctx, c.BlobKey, &aeimg.ServingURLOptions{Size: max, Crop: true})
client := urlfetch.Client(ctx)
resp, _ := client.Get(url.String())
// Just in case
if resp.StatusCode != 200 {
return nil, errors.New("Blob not found")
}
// Do we need further cropping?
if width == height {
return jpeg.Decode(resp.Body)
}
src, err := jpeg.Decode(resp.Body)
dest := image.NewRGBA(image.Rect(0, 0, width, height))
if err != nil {
return nil, err
}
graphics.Thumbnail(dest, src)
return dest, nil
}
示例3: TestDelete
// Test the delete function.
func TestDelete(t *testing.T) {
store := New()
// Add some images.
addA, _ := jpeg.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(imgA)))
addB, _ := jpeg.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(imgB)))
hashA, _ := CreateHash(addA)
hashB, _ := CreateHash(addB)
store.Add("imgA", hashA)
store.Add("imgB", hashB)
// Delete one.
store.Delete("imgA")
// Query should only return imgB.
query, _ := jpeg.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(imgC)))
queryHash, _ := CreateHash(query)
matches := store.Query(queryHash)
if len(matches) != 1 {
t.Errorf("Invalid query result set size, expected 1, is %d", len(matches))
return
}
if matches[0].ID != "imgB" {
t.Errorf("Query found %s but should have found imgB", matches[0].ID)
}
}
示例4: main
func main() {
f, err := os.Open("Lenna50.jpg")
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
i50, err := jpeg.Decode(f)
if err != nil {
fmt.Println(err)
return
}
if f, err = os.Open("Lenna100.jpg"); err != nil {
fmt.Println(err)
return
}
defer f.Close()
i100, err := jpeg.Decode(f)
if err != nil {
fmt.Println(err)
return
}
if i50.ColorModel() != i100.ColorModel() {
fmt.Println("different color models")
return
}
b := i50.Bounds()
if !b.Eq(i100.Bounds()) {
fmt.Println("different image sizes")
return
}
var sum int64
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
r1, g1, b1, _ := i50.At(x, y).RGBA()
r2, g2, b2, _ := i100.At(x, y).RGBA()
if r1 > r2 {
sum += int64(r1 - r2)
} else {
sum += int64(r2 - r1)
}
if g1 > g2 {
sum += int64(g1 - g2)
} else {
sum += int64(g2 - g1)
}
if b1 > b2 {
sum += int64(b1 - b2)
} else {
sum += int64(b2 - b1)
}
}
}
nPixels := (b.Max.X - b.Min.X) * (b.Max.Y - b.Min.Y)
fmt.Printf("Image difference: %f%%\n",
float64(sum*100)/(float64(nPixels)*0xffff*3))
}
示例5: main
func main() {
op := gif.Options{256, nil, nil}
gifWriter, err := os.Create("new.gif")
if err != nil {
fmt.Println("something went wrong")
}
image1, _ := os.Open("image1.jpeg")
image2, _ := os.Open("image2.jpeg")
jpeg1, _ := jpeg.Decode(image1)
jpeg2, _ := jpeg.Decode(image2)
gif.Encode(gifWriter, jpeg1, &op)
gif.Encode(gifWriter, jpeg2, &op)
}
示例6: loader
func loader(inChan chan loaderRequest, outChan chan *Image, errChan chan *Error, stopChan chan int) {
for {
select {
case req := <-inChan:
switch strings.ToLower(path.Ext(req.name)) {
case ".jpg":
if img, err := jpeg.Decode(req.reader); err == nil {
outChan <- &Image{Name: req.name, Data: img}
} else {
errChan <- &Error{Name: req.name, Message: err.Error()}
}
case ".png":
if img, err := png.Decode(req.reader); err == nil {
outChan <- &Image{Name: req.name, Data: img}
} else {
errChan <- &Error{Name: req.name, Message: err.Error()}
}
}
if err := req.reader.Close(); err != nil {
fmt.Errorf("%s\n", err)
}
case <-stopChan:
return
}
}
}
示例7: decodeMJPEG
// decodeMJPEG turns gstreamer 'jpegenc' output into a stream of images.
func decodeMJPEG(input io.Reader) chan image.Image {
stream := make(chan image.Image)
go func() {
in := newReader(bufio.NewReaderSize(input, 64*1024*1024))
//in := newReader(input)
for {
printStats()
tDec.Start()
img, err := jpeg.Decode(in)
tDec.Stop()
//if *flagRec != "" {
// record(img)
//}
if err != nil {
if err.Error() == "unexpected EOF" {
close(stream)
}
if err.Error() != "invalid JPEG format: missing SOI marker" {
exit(err)
}
nErrors++
continue
}
select {
default:
nDropped++
case stream <- img:
nProcessed++
}
}
}()
return stream
}
示例8: read
func read(filename string) (image.Image, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return jpeg.Decode(file)
}
示例9: thumb
func thumb() image.Image {
imageName := "test.jpg"
imageFile, err := os.Open(imageName)
if err != nil {
log.Fatal(err)
}
var myImage *image.Image
switch strings.ToLower(path.Ext(imageName)) {
case ".jpg", ".jpeg":
img, err := jpeg.Decode(imageFile)
if err != nil {
log.Fatal(err)
}
myImage = &img
case ".png":
img, err := png.Decode(imageFile)
if err != nil {
log.Fatal(err)
}
myImage = &img
}
imageFile.Close()
m := resize.Resize(0, 200, *myImage, resize.MitchellNetravali)
return m
}
示例10: Decode
// Decode reads a CR2 image from r and returns the embedded JPEG thumbnail as
// an image.Image.
func Decode(r io.Reader) (image.Image, error) {
r, err := NewReader(r)
if err != nil {
return nil, err
}
return jpeg.Decode(r)
}
示例11: createSmallPic
func createSmallPic(file io.Reader, fileSmall string, w, h int) error {
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
return err
}
b := img.Bounds()
if w > b.Dx() {
w = b.Dx()
}
if h > b.Dy() {
h = b.Dy()
}
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(uint(w), uint(h), img, resize.Lanczos3)
out, err := os.Create(fileSmall)
if err != nil {
return err
}
defer out.Close()
// write new image to file
return jpeg.Encode(out, m, nil)
}
示例12: ResizeImage
func ResizeImage(src, dst string) (err error) {
// open "test.jpg"
file, err := os.Open(src)
if err != nil {
// mlog.Error(err)
return err
}
defer file.Close()
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
// mlog.Error(err)
return err
}
// file.Close()
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(80, 0, img, resize.Lanczos3)
out, err := os.Create(dst)
if err != nil {
// mlog.Error(err)
return err
}
defer out.Close()
// write new image to file
return jpeg.Encode(out, m, nil)
}
示例13: decodingImage
//This private method provides decoding image
//Returns decoded image. If during decoding process occurred problem, return error
func decodingImage(data []byte) (image.Image, error) {
rimg, err := jpeg.Decode(bytes.NewReader(data))
if err != nil {
return nil, err
}
return rimg, nil
}
示例14: main
func main() {
var source string
flag.StringVar(&source, "source", "./test.jpg", "image to read")
var dest string
flag.StringVar(&dest, "dest", "./out.jpg", "image to output")
var sizeStr string
flag.StringVar(&sizeStr, "size", "100w", "size to resize to")
flag.Parse()
file, err := os.Open(source)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Decode the image.
m, err := jpeg.Decode(file)
if err != nil {
fmt.Printf("error decoding image\n")
log.Fatal(err)
}
outputImage := resize.Resize(m, sizeStr)
fl, err := os.OpenFile(dest, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println("couldn't write", err)
return
}
defer fl.Close()
jpeg.Encode(fl, outputImage, nil)
}
示例15: openImage
func openImage(t *testing.T, path string) image.Image {
fireTruck, err := os.Open(path)
assert.NoError(t, err, "error openenig image")
img, err := jpeg.Decode(fireTruck)
assert.NoError(t, err, "error decoding image")
return img
}