package imageenhancement
import (
"encoding/base64"
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
ocr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ocr/v20181119"
"io/ioutil"
"os"
"testing"
)
//MainImageEnhancement 主函数
func MainImageEnhancement(imagesPath string) {
//图片地址
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
// 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
credential := common.NewCredential(
//这里填入腾讯云账户密钥对
"SecretId",
"SecretKey",
)
// 实例化一个client选项,可选的,没有特殊需求可以跳过
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com"
// 实例化要请求产品的client对象,clientProfile是可选的
client, _ := ocr.NewClient(credential, "ap-guangzhou", cpf)
//读取图片base64
toBase64Str, err := imageToBase64(imagesPath)
respImages, err := imageType208(*client, toBase64Str)
if err != nil {
return
}
err = writeFile("test.jpg", *respImages)
if err != nil {
return
}
}
//imageType1 切边增强
func imageType1(client ocr.Client, toBase64Str string) (*string, error) {
// 实例化一个请求对象,每个接口都会对应一个request对象
request := ocr.NewImageEnhancementRequest()
request.ImageBase64 = common.StringPtr(toBase64Str)
request.ReturnImage = common.StringPtr("preprocess")
request.TaskType = common.Int64Ptr(1)
// 返回的resp是一个ImageEnhancementResponse的实例,与请求对象对应
response, err := client.ImageEnhancement(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return nil, err
}
if err != nil {
return nil, err
}
return response.Response.Image, nil
}
//imageType2 弯曲矫正
func imageType2(client ocr.Client, toBase64Str string) (*string, error) {
// 实例化一个请求对象,每个接口都会对应一个request对象
request := ocr.NewImageEnhancementRequest()
request.ImageBase64 = common.StringPtr(toBase64Str)
request.ReturnImage = common.StringPtr("preprocess")
request.TaskType = common.Int64Ptr(2)
// 返回的resp是一个ImageEnhancementResponse的实例,与请求对象对应
response, err := client.ImageEnhancement(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return nil, err
}
if err != nil {
return nil, err
}
return response.Response.Image, nil
}
//imageType202 黑白模式
func imageType202(client ocr.Client, toBase64Str string) (*string, error) {
// 实例化一个请求对象,每个接口都会对应一个request对象
request := ocr.NewImageEnhancementRequest()
request.ImageBase64 = common.StringPtr(toBase64Str)
request.ReturnImage = common.StringPtr("preprocess")
request.TaskType = common.Int64Ptr(202)
// 返回的resp是一个ImageEnhancementResponse的实例,与请求对象对应
response, err := client.ImageEnhancement(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return nil, err
}
if err != nil {
return nil, err
}
return response.Response.Image, nil
}
... //这里省略重复的部分,可以扩展其他模式或者任意模式组合
//writeFile base64转image
func writeFile(path string, s string) error {
//解析base64字符串
dist, _ := base64.StdEncoding.DecodeString(s)
//写入新文件
f, _ := os.OpenFile(path, os.O_RDWR|os.O_CREATE, os.ModePerm)
defer f.Close()
_, err := f.Write(dist)
return err
}
//imageToBase64 img转base64
func imageToBase64(filePath string) (string, error) {
srcByte, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
res := base64.StdEncoding.EncodeToString(srcByte)
return res, nil
}
评论