not much to say, directly on the code, the project used, did not optimize, there are many parameters are not set. [Java] View plaincopy1.ImportJava.util.Random; 2. 3.ImportAndroid.graphics.Bitmap; 4.ImportAndroid.graphics.Canvas; 5.ImportAndroid.graphics.Color; 6.ImportAndroid.graphics.Paint; 7.ImportAndroid.graphics.Bitmap.Config; 8. 9. Public classBputil {10. 11.Private Static Final Char[] CHARS = { 12. ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', . ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ' 17. }; 18. 19.Private StaticBputil Bputil; 20. 21st. Public Staticbputil getinstance () {22.if(Bputil = =NULL) Bputil =NewBputil (); 24.returnBputil; 25. } 26. 27.//width= "height= "28.//base_padding_left= "5"29.//range_padding_left= "Ten"30.//base_padding_top= " "31.//range_padding_top= "Ten"32.//codelength= "4"33.//line_number= "3"34.//font_size= "35. 36.//Default Settings37.Private Static Final intDefault_code_length = 4; 38.Private Static Final intDefault_font_size = 20; 39.Private Static Final intDefault_line_number = 3; 40.Private Static Final intBase_padding_left = 5, Range_padding_left = ten, Base_padding_top =, Range_padding_top = 10; 41.Private Static Final intDefault_width = Default_height = 30; 42. 43.//settings decided by the layout XML44.//Canvas width and height45.Private intwidth = default_width, height =Default_height; 46. 47.//random word space and pading_top48.Private intBase_padding_left = base_padding_left, Range_padding_left =Range_padding_left,Base_padding_top = base_padding_top, Range_padding_top =Range_padding_top; 50. 51.//Number of chars, lines; font size52.Private intCodelength = default_code_length, Line_number = default_line_number, font_size =default_font_size; 53. 54.//variables35hPrivateString Code; 56.Private intPadding_left, Padding_top; 57.PrivateRandom random =NewRandom (); 58. 59. PublicBitmap CreateBitmap () {Padding_left = 0; 61. Bitmap BP =bitmap.createbitmap (width, height, config.argb_8888); A. Canvas C =NewCanvas (BP); 64. . Code =Createcode (); 66. 67. C.drawcolor (Color.White); . Paint Paint =NewPaint (); 69. Paint.settextsize (font_size); 70. 71. for(inti = 0; I < code.length (); i++) { 72. Randomtextstyle (paint); 73. Randompadding (); C.drawtext (Code.charat (i) + "", Padding_left, padding_top, paint); 75. } 76. 77. for(inti = 0; i < Line_number; i++) { 78. DrawLine (c, paint); 79. } 80. Bayi. C.save (Canvas.all_save_flag);//SaveC.restore ();//83.returnBP; 84. } 85. 86. PublicString GetCode () {87.returnCode; 88. } 89. 90.PrivateString Createcode () {StringBuilder buffer =NewStringBuilder (); 92. for(inti = 0; i < codelength; i++) { 93. Buffer.append (Chars[random.nextint (chars.length)); 94. } 95.returnbuffer.tostring (); 96. } 97. 98.Private voiddrawLine (canvas canvas, paint paint) {99.intcolor =Randomcolor (); 100.intStartX =random.nextint (width); 101.intStarty =random.nextint (height); 102.intSTOPX =random.nextint (width); 103.intStopy =random.nextint (height); 104. Paint.setstrokewidth (1); 105. Paint.setcolor (color); 106. Canvas.drawline (StartX, Starty, stopx, stopy, paint); 107. } 108. 109.Private intRandomcolor () {110.returnRandomcolor (1); 111. } 112. 113.Private intRandomcolor (intRate ) { 114.intRed = Random.nextint (256)/Rate ; 115.intGreen = Random.nextint (256)/Rate ; 116.intBlue = Random.nextint (256)/Rate ; 117.returnColor.rgb (red, green, blue); 118. } 119. 120.Private voidRandomtextstyle (Paint paint) {121.intcolor =Randomcolor (); 122. Paint.setcolor (color); 123. Paint.setfakeboldtext (Random.nextboolean ());//true is bold, false is not bold124.floatSkewx = Random.nextint (11)/10; Skewx = Random.nextboolean ()? SKEWX:-skewx; 126. PAINT.SETTEXTSKEWX (SKEWX);//float type parameter, negative number for right oblique, integer left oblique127.//Paint.setunderlinetext (true);//true is underlined, false is non-underlined128.//Paint.setstrikethrutext (true);//true is strikethrough, false is non-strikethrough129. } 130. 131.Private voidrandompadding () {Padding_left + = Base_padding_left +Random.nextint (Range_padding_left); 133. Padding_top = Base_padding_top +Random.nextint (range_padding_top); 134. } 135.} How to use [Java] View plaincopy1.ImageView ImageView =(ImageView) Findviewbyid (R.id.imageview); 2. Imageview.setimagebitmap (Bputil.getinstance (). CreateBitmap ()); Excerpt from: http://blog.csdn.net/whumr1/article/details/7092013
Android Build Verification Code bitmap