您的位置:首页 > 编程语言 > Go语言

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

2015-06-24 11:03 417 查看
// 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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: