Go获取当前执行路径及图片的加解码

// imagecode.go
package main

import (
	"bytes"
	"fmt"
	"os/exec"
	"path/filepath"
	"strings"
	"image"
	"image/jpeg"
	"io/ioutil"
	"os"
)

func main() {
	//读取一张图片,ff为[]byte类型
	ff, _ := ioutil.ReadFile("image1.jpg")
	//把ff写入文件中
	_ = ioutil.WriteFile("./output.jpg.txt", ff, 0666)
	//新建一个缓存
	bbb := bytes.NewBuffer(ff)
	//对byte进行解码
	m, _, _ := image.Decode(bbb)
	//新建一个文件
	f, _ := os.Create("output.jpg")
	fmt.Println("f name:", f.Name())
	defer f.Close()

	jpeg.Encode(f, m, nil)
	//获得当前执行路径
	str := getCurrentPath()
	fmt.Println("end:", str)
	fmt.Println("end:", str+f.Name())
}

func getCurrentPath() string {
	file, _ := exec.LookPath(os.Args[0])
	fmt.Println("file:", file)
	path, _ := filepath.Abs(file)
	fmt.Println("path:", path)
	splitstring := strings.Split(path, "\\")
	size := len(splitstring)
	splitstring = strings.Split(path, splitstring[size-1])
	ret := strings.Replace(splitstring[0], "\\", "/", size-1)
	return ret
}

猜你喜欢

转载自blog.csdn.net/qq_534019165/article/details/46619461