ag_tools/cmd/phash.go

49 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"archgrid.xyz/ag/tools/types"
"archgrid.xyz/ag/toolsbox/hash/phash"
"archgrid.xyz/ag/toolsbox/serialize/base64"
"archgrid.xyz/ag/toolsbox/serialize/hex"
"github.com/spf13/cobra"
)
var (
phashResult types.ResultEncoding = types.ResultInHex
)
var phashCmd = &cobra.Command{
Use: "phash",
Short: "使用pHash算法计算指定图像文件的感知哈希值。",
Long: `使用pHash算法计算指定图像文件的感知哈希值。`,
Args: cobra.MinimumNArgs(1),
Run: phashExecute,
}
func phashExecute(cmd *cobra.Command, args []string) {
var (
hashResult []byte
err error
)
hashResult, err = phash.HashFile(args[0])
if err != nil {
fmt.Printf("散列计算失败:%s\n", err)
return
}
switch phashResult {
case types.ResultInBase64:
fmt.Printf("感知计算结果:%s\n", base64.ToBase64(hashResult))
case types.ResultInHex:
fallthrough
default:
fmt.Printf("感知计算结果:%s\n", hex.ToHex(hashResult))
}
}
func init() {
phashCmd.Flags().VarP(&phashResult, "output", "o", "计算结果编码输出的格式可选Base64或者Hex编码默认使用Hex编码。")
rootCmd.AddCommand(phashCmd)
}