✨ add face recognition
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// SupportedHashFuncs 定义支持的哈希函数类型
|
||||
var SupportedHashFuncs = map[string]func() hash.Hash{
|
||||
"md5": crypto.MD5.New,
|
||||
"sha1": crypto.SHA1.New,
|
||||
"sha256": crypto.SHA256.New,
|
||||
"sha512": crypto.SHA512.New,
|
||||
}
|
||||
|
||||
// CalculateFileHash 根据指定的哈希算法计算文件的哈希值
|
||||
func CalculateFileHash(filePath string, algorithm string) (string, error) {
|
||||
// 获取对应的哈希函数
|
||||
hashFunc, exists := SupportedHashFuncs[algorithm]
|
||||
if !exists {
|
||||
return "", fmt.Errorf("unsupported hash algorithm: %s", algorithm)
|
||||
}
|
||||
|
||||
// 打开文件
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 创建哈希对象
|
||||
h := hashFunc()
|
||||
|
||||
// 计算哈希值
|
||||
if _, err := io.Copy(h, file); err != nil {
|
||||
return "", fmt.Errorf("failed to calculate hash: %w", err)
|
||||
}
|
||||
|
||||
// 返回哈希值的十六进制字符串
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// CalculateStreamHash 计算输入流的哈希值
|
||||
func CalculateStreamHash(reader io.Reader, algorithm string) (string, error) {
|
||||
// 获取对应的哈希函数
|
||||
hashFunc, exists := SupportedHashFuncs[algorithm]
|
||||
if !exists {
|
||||
return "", fmt.Errorf("unsupported hash algorithm: %s", algorithm)
|
||||
}
|
||||
|
||||
// 创建哈希对象
|
||||
h := hashFunc()
|
||||
|
||||
// 从输入流计算哈希值
|
||||
if _, err := io.Copy(h, reader); err != nil {
|
||||
return "", fmt.Errorf("failed to calculate hash: %w", err)
|
||||
}
|
||||
|
||||
// 返回哈希值的十六进制字符串
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
24
common/utils/generate_image_hash.go
Normal file
24
common/utils/generate_image_hash.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/corona10/goimagehash"
|
||||
"image"
|
||||
)
|
||||
|
||||
// CalculatePerceptualHash 计算感知哈希
|
||||
func CalculatePerceptualHash(img image.Image) (string, error) {
|
||||
hash, err := goimagehash.PerceptionHash(img)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hash.ToString(), nil
|
||||
}
|
||||
|
||||
// CalculateHash 计算平均哈希
|
||||
func CalculateHash(img image.Image) (uint64, error) {
|
||||
hash, err := goimagehash.AverageHash(img)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return hash.GetHash(), nil
|
||||
}
|
@@ -1,8 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg" // 引入 jpeg 解码器
|
||||
_ "image/png" // 引入 png 解码器
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -88,3 +93,25 @@ func Base64ToBytes(base64Str string) ([]byte, error) {
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Base64ToImage 将 Base64 字符串转换为 image.Image 格式
|
||||
func Base64ToImage(base64Str string) (image.Image, error) {
|
||||
// 使用正则表达式去除前缀
|
||||
re := regexp.MustCompile(`^data:image/([a-zA-Z]*);base64,`)
|
||||
// 去除前缀部分
|
||||
base64Str = re.ReplaceAllString(base64Str, "")
|
||||
|
||||
// 解码 Base64 字符串
|
||||
data, err := base64.StdEncoding.DecodeString(base64Str)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode base64 string: %v", err)
|
||||
}
|
||||
|
||||
// 使用 image.Decode 解码字节数据
|
||||
img, _, err := image.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode image: %v", err)
|
||||
}
|
||||
|
||||
return img, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user