當前位置: 首頁>>代碼示例>>Golang>>正文


Golang jpeg.Decode函數代碼示例

本文整理匯總了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
}
開發者ID:sjn1978,項目名稱:go-fuzz,代碼行數:28,代碼來源:jpeg.go

示例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
}
開發者ID:shaunduncan,項目名稱:goplacecreature,代碼行數:26,代碼來源:model.go

示例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)
	}
}
開發者ID:postfix,項目名稱:duplo,代碼行數:27,代碼來源:duplo_test.go

示例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))
}
開發者ID:travis1230,項目名稱:RosettaCodeData,代碼行數:57,代碼來源:percentage-difference-between-images-1.go

示例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)

}
開發者ID:haetze,項目名稱:DemoGO,代碼行數:14,代碼來源:createAgif.go

示例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
		}
	}
}
開發者ID:dbratus,項目名稱:impack,代碼行數:27,代碼來源:loading.go

示例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
}
開發者ID:barnex,項目名稱:ev3cam,代碼行數:34,代碼來源:gstreamer.go

示例8: read

func read(filename string) (image.Image, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	return jpeg.Decode(file)
}
開發者ID:najeira,項目名稱:go_jpeg_to_png,代碼行數:7,代碼來源:main.go

示例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
}
開發者ID:brejoc,項目名稱:gosizr,代碼行數:29,代碼來源:gosizr.go

示例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)
}
開發者ID:rfistman,項目名稱:camlistore,代碼行數:9,代碼來源:reader.go

示例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)
}
開發者ID:gusa1120,項目名稱:beego_sample,代碼行數:26,代碼來源:article.go

示例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)
}
開發者ID:jbrodriguez,項目名稱:mediagui,代碼行數:31,代碼來源:utils.go

示例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
}
開發者ID:billyboar,項目名稱:GCSolutions,代碼行數:9,代碼來源:main.go

示例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)
}
開發者ID:thraxil,項目名稱:resize,代碼行數:32,代碼來源:resize_main.go

示例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
}
開發者ID:maleck13,項目名稱:jigsaws,代碼行數:7,代碼來源:jigsaw_test.go


注:本文中的image/jpeg.Decode函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。