golang 防知乎 中文驗證碼 源碼

來源:互聯網
上載者:User

原創,轉載請註明出處!

最開始用圖形來模仿文字進行各種角度的倒立和排列,後來切換為文字後,有很多問題。總結如下:

1、程式在畫圖形和畫文字方面不一樣,圖形的是從原點開始(0,0),而文字則從文字的基準開始(0,baseline)

2、在增加角度位移時,文字或圖形的高寬會產生變化(偏∠45度時達到最大),這時候為了讓它們頂點對齊,需要計算位移量(用三角函數)

3、在繪圖時,會先旋轉“畫布”(描述可能不準確),再繪製文字。此時要往迴旋轉,否則下一個圖形會順著這個角度繼續畫。

4、為了讓圖形保持固定寬度,對於有偏角的文字,需要平均縮小左右間距(否則不同的角度,固定的文字個數,會讓圖形寬度不同)

 

源碼:(代碼還可以再整理和最佳化,但限於計劃時間,懶得弄了)

package mainimport ( "math""reflect""time""math/rand""github.com/golang/freetype/truetype""github.com/fogleman/gg" // 需要安裝這個包    "flag"     "fmt"     "io/ioutil"     "log" )var wORDSLIB = []interface{}{"趙","錢","孫","李","周","吳","鄭","馮","陳","褚","衛","蔣","沈","韓","楊","朱","秦","尤","許","何","呂","施","張","孔","曹","嚴","華","金","魏","陶","薑","戚","謝","鄒","喻","柏","水","竇","章","雲","蘇","潘","葛","奚","範","彭","郎","魯","韋","昌","馬","苗","鳳","花","方","俞","任","袁","柳","酆","鮑","史","唐","費","廉","岑","薛","雷","賀","倪","湯","滕","殷","羅","畢","郝","鄔","安","常","樂","於","時","傅","皮","卞","齊","康","伍","餘","元","蔔","顧","孟","平","黃","和","穆","蕭","尹","姚","邵","湛","汪","祁","毛","禹","狄","米","貝","明","臧","計","伏","成","戴","談","宋","茅","龐","熊","紀","舒","屈","項","祝","董","梁","杜","阮","藍","閔","席","季","麻","強","賈","路","婁","危","江","童","顏","郭","梅","盛","林","刁","鐘","徐","邱","駱","高","夏","蔡","樊","胡","淩","霍","虞","萬","支","柯","昝","管","盧","莫","經","房","裘","繆","幹","解","應","宗","丁","宣","賁","鄧","鬱","單","杭","洪","包","諸","左","石","崔","吉","鈕","龔","程","嵇","邢","滑","裴","陸","榮","翁","荀","羊","於","惠","甄","曲","家","封","芮","羿","儲","靳","汲","邴","糜","松","井","段","富","巫","烏","焦","巴","弓","牧","隗","山","穀","車","侯","宓","蓬","全","郗","班","仰","秋","仲","伊","宮","寧","仇","欒","暴","甘","鈄","厲","戎","祖","武","符","劉","景","詹","束","龍","葉","幸","司","韶","郜","黎","薊","薄","印","宿","白","懷","蒲","邰","從","鄂","索","鹹","籍","賴","卓","藺","屠","蒙","池","喬","陰","鬱","胥","能","蒼","雙","聞","莘","黨","翟","譚","貢","勞","逄","姬","扶","堵","冉","宰","酈","雍","卻","璩","桑","桂","濮","牛","壽","通","邊","扈","燕","冀","郟","浦","尚","農","溫","別","莊","晏","柴","瞿","閻","充","慕","連","茹","習","宦","艾","魚","容","向","古","易","慎","戈","廖","庾","終","暨","居","衡","步","都","耿","滿","弘","匡","國","文","寇","廣","祿","闕","東","歐","殳","沃","利","蔚","越","夔","隆","師","鞏","厙","聶","晁","勾","敖","融","冷","訾","辛","闞","那","簡","饒","空","曾","毋","沙","乜","養","鞠","須","豐","巢","關","蒯","相","查","後","荊","紅","遊","竺","權","逯","蓋","益","桓","公","俟","上","官","陽","人","赫","皇","甫","尉","遲","澹","台","冶","政","淳","太","叔","申","軒","轅","令","狐","離","宇","長","鮮","閭","丘","徒","仉","督","子","顓","端","木","西","漆","雕","正","壤","駟","良","拓","跋","夾","父","晉","楚","閆","法","汝","鄢","塗","欽","百","裡","南","門","呼","延","歸","海","舌","微","生","嶽","帥","緱","亢","況","郈","有","琴","商","牟","佘","佴","伯","賞","墨","哈","譙","笪","年","愛","佟","第","五","言","福","姓"}var (     dpi      = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")     fontfile = flag.String("fontfile", "./SIMYOU.TTF", "filename of the ttf font")     size     = flag.Float64("size", 40, "font size in points") )func main() { x := "abc"fmt.Println(x[0:3])    drawImageBygg()} func drawImageBygg(){dc := gg.NewContext(320, 56)  // 56 => w*sin(45) + h*sin(45)  45度時,字型達到最大高度dc.SetRGBA(1, 1, 1,0) // 設定背景色:末尾為透明度 1-0(1-不透明 0-透明)dc.Clear()dc.SetRGBA(0, 0, 0,1) // 設定字型色fontBytes, err := ioutil.ReadFile(*fontfile)     if err != nil {         log.Println(err)         return     } font, err := truetype.Parse(fontBytes)if err != nil {        log.Println(err)         return }face := truetype.NewFace(font, &truetype.Options{Size: *size,DPI:*dpi,}) dc.SetFontFace(face)// 初始化用於計算座標的變數fm := face.Metrics()ascent := float64(fm.Ascent.Round()) // 字型的基準到頂部距離decent := float64(fm.Descent.Round()) // 字型的基準到底部的距離w := float64(fm.Height.Round()) // 方塊字,大多數應為等寬字,即和高度一樣h := float64(fm.Height.Round())totalWidth := 0.0 // 目前已累積的圖片寬度(需要用來計算字型位置)// 隨機取漢字,定位倒立的字words := getRandomMembersFromMemberLibary(wORDSLIB,8) // 取8個字reverseWordsIndex := getRandomMembersFromMemberLibary([]interface{}{0,1,2,3,4,5,6,7},2) // 隨機2個倒立字for i,word := range words{degree := If(Contain(i,reverseWordsIndex),float64(RandInt64(150,210)),float64(RandInt64(-30,30))) // 隨機角度,正向角度 -30~30,倒立角度 150~210x,y,leftCutSize,rightCS := getCoordByQuadrantAndDegree(w,h,ascent,decent,degree,totalWidth)dc.RotateAbout(gg.Radians(degree),0,0)dc.DrawStringAnchored(word.(string), x,y, 0,0)dc.RotateAbout(-1*gg.Radians(degree),0,0)totalWidth = totalWidth + leftCutSize + rightCSfmt.Println("x:",x,"y:",y,"total:",totalWidth,"degree:",degree)}dc.Stroke()dc.SavePNG("out.png")    fmt.Println("Wrote out.png OK.") }func getCoordByQuadrantAndDegree(w,h,ascent,descent,degree,beforTotalWidth float64)(x,y,leftCutSize,rightCutSize float64){var totalWidth float64switch{case degree<=0 && degree >= -40:  // 第一象限:逆時針 -30度 ~ 0  <=>  330 ~ 360 (目前參數要傳入負數)  rd := -1 * degree // 轉為正整數,便於計算leftCutSize = w*getDegreeSin(90-rd)rightCutSize = h*getDegreeSin(rd)offset := (leftCutSize + rightCutSize - w) / 2 // 橫向位移量(角度傾斜越厲害,占寬越多,通過位移量分攤給它的左右邊距來收窄)leftCutSize,rightCutSize = leftCutSize - offset,rightCutSize - offsettotalWidth = beforTotalWidth + leftCutSize x = getDegreeSin(90 - rd)*totalWidth - w y = ascent + getDegreeSin(rd)*totalWidth  case degree >=0 && degree <= 40:  // 第四象限:順時針 0 ~ 30度leftCutSize = h*getDegreeSin(degree)rightCutSize = w*getDegreeSin(90-degree)offset := (leftCutSize + rightCutSize - w) / 2leftCutSize,rightCutSize = leftCutSize - offset,rightCutSize - offsettotalWidth = beforTotalWidth + leftCutSize // 現在totalwidth = 前面的寬 + 自己的左切邊x = getDegreeSin(90-degree)*totalWidthy = ascent - getDegreeSin(degree)*totalWidth case degree >= 180 && degree <= 220:  // 第二象限:順時針 180 ~ 210度rd := degree - 180leftCutSize = h*getDegreeSin(rd)rightCutSize = w*getDegreeSin(90-rd)offset := (leftCutSize + rightCutSize - w) / 2 leftCutSize,rightCutSize = leftCutSize - offset,rightCutSize - offsettotalWidth = beforTotalWidth + leftCutSize x = -1 * (getDegreeSin(90-rd)*totalWidth + w)y = getDegreeSin(rd)*totalWidth - descentcase degree >= 140 && degree <= 180:  // 第三象限:順時針 150 ~ 180度rd := 180-degreeleftCutSize = w*getDegreeSin(90-rd)rightCutSize = h*getDegreeSin(rd)offset := (leftCutSize + rightCutSize - w) / 2leftCutSize,rightCutSize = leftCutSize - offset,rightCutSize - offsettotalWidth = beforTotalWidth + leftCutSize x = -1 * (getDegreeSin(90-rd) * totalWidth)y = -1 * (getDegreeSin(rd) * totalWidth + descent)default: panic(fmt.Sprintf("非法的參數:%f",degree))}return}func getDegreeSin(degree float64) float64{return math.Sin(degree*math.Pi/180)}//RandInt64 ...func RandInt64(min, max int64) int64 {if min >= max || min == 0 || max == 0 {return max}rand.Seed(time.Now().UnixNano())return rand.Int63n(max-min) + min}func getRandomMembersFromMemberLibary(lib []interface{},size int)[]interface{}{source,result := make([]interface{},0),make([]interface{},0)if size <= 0 || len(lib) == 0{return result}for _,v := range lib{source = append(source,v)}if size >= len(lib){return source}for i:=0;i<size;i++{rand.Seed(time.Now().UnixNano())pos := rand.Intn(len(source))result = append(result,source[pos])source = append(source[:pos], source[pos+1:]...)}return result}//Contain ...func Contain(obj interface{}, target interface{}) bool {    targetValue := reflect.ValueOf(target)    switch reflect.TypeOf(target).Kind() {    case reflect.Slice, reflect.Array:        for i := 0; i < targetValue.Len(); i++ {            if targetValue.Index(i).Interface() == obj {                return true            }        }    case reflect.Map:        if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() {            return true        }    }    return false}//If ...func If(expr bool,trueVal float64,falseVal float64) float64{if expr {return trueVal }return falseVal}

  

 需要一個字型檔,這裡使用的幼圓(幼圓免費,其他免費字型懶得找)。

注意:字型不同,寬度和高度可能會需要調整。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.