From 4358672226259c6a505f8e6641748b0c70850358 Mon Sep 17 00:00:00 2001 From: lvxin Date: Sun, 30 Sep 2018 11:27:51 +0800 Subject: [PATCH] golang-api demo --- golang/README.md | 7 ++++ golang/test.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 golang/README.md create mode 100644 golang/test.go diff --git a/golang/README.md b/golang/README.md new file mode 100644 index 0000000..5464cbb --- /dev/null +++ b/golang/README.md @@ -0,0 +1,7 @@ +# Usage: + +Do: +``` +cd api-examples +go run golang/test.go images/algebra.jpg +``` diff --git a/golang/test.go b/golang/test.go new file mode 100644 index 0000000..5958d73 --- /dev/null +++ b/golang/test.go @@ -0,0 +1,102 @@ +package main + +import ( + "net/http" + "encoding/json" + "strings" + "io/ioutil" + "fmt" + "os" + "encoding/base64" + "bytes" +) + +type MathpixV3LatexParam struct { + Src string `json:"src"` + Formats []string `json:"formats"` + Url string `json:"url"` +} + +type DetectionMap struct { + ContainsChart int `json:"contains_chart"` + ContainsDiagram float32 `json:"contains_diagram"` + ContainsGraph int `json:"contains_graph"` + ContainsTable int `json:"contains_table"` + IsBlank float32 `json:"is_blank"` + IsInverted int `json:"is_inverted"` + IsNotMath float32 `json:"is_not_math"` + IsPrinted float32 `json:"is_printed"` +} + +type Position struct { + Height int `json:"height"` + TopLeftX int `json:"top_left_x"` + TopLeftY int `json:"top_left_y"` + Width int `json:"width"` +} + +type MathpixV3LatexResult struct { + DetectionList []string `json:"detection_list"` + DetectionMap DetectionMap `json:"detection_map"` + Error string `json:"error"` + LatexConfidence float64 `json:"latex_confidence"` + LatexConfidenceRate float64 `json:"latex_confidence_rate"` + LatexList []string `json:"latex_list"` + LatexNormal string `json:"latex_normal"` + LatexStyled string `json:"latex_styled"` + Position Position `json:"position"` +} + +func (mathpixV3LatexResult *MathpixV3LatexResult) String() string { + b, err := json.Marshal(*mathpixV3LatexResult) + if err != nil { + return fmt.Sprintf("%+v", *mathpixV3LatexResult) + } + var out bytes.Buffer + err = json.Indent(&out, b, "", " ") + if err != nil { + return fmt.Sprintf("%+v", *mathpixV3LatexResult) + } + return out.String() +} + +func main() { + filePath := os.Args[1] + imageByte, err := ioutil.ReadFile(filePath) + if err != nil{ + fmt.Println(err.Error()) + return + } + + mathpixV3LatexParam := new(MathpixV3LatexParam) + mathpixV3LatexParam.Src = "data:image/jpg;base64," + base64.URLEncoding.EncodeToString(imageByte) + mathpixV3LatexParam.Formats = []string{"latex_normal", "latex_styled"} + + client := &http.Client{} + data, err := json.Marshal(mathpixV3LatexParam) + req, err := http.NewRequest("POST", "https://api.mathpix.com/v3/latex", strings.NewReader(string(data))) + if err != nil { + fmt.Println(err.Error()) + return + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("app_id", "trial") + req.Header.Set("app_key", "34f1a4cea0eaca8540c95908b4dc84ab") + resp, err := client.Do(req) + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + + //fmt.Println(string(body)) + + if err != nil { + fmt.Println(err.Error()) + return + } + mathpixV3LatexResult := new(MathpixV3LatexResult) + err = json.Unmarshal(body,&mathpixV3LatexResult) + if err != nil { + fmt.Println(err.Error()) + return + } + fmt.Println(mathpixV3LatexResult.String()) +}