java 验证码 源码_Java实现图片验证码源码

1 /*

2

3 * To change this template, choose Tools | Templates * and open the template in the editor.4

5 */

6

7 packagecom.wind.util;8

9 importjava.awt.BasicStroke;10

11 import java.awt.Color; importjava.awt.Font;12

13 import java.awt.Graphics; importjava.awt.Graphics2D;14

15 importjava.awt.geom.AffineTransform;16

17 importjava.awt.geom.Line2D;18

19 importjava.awt.image.BufferedImage;20

21 importjava.io.ByteArrayInputStream;22

23 importjava.io.ByteArrayOutputStream;24

25 importjava.io.IOException;26

27 importjava.util.Random;28

29 importjavax.imageio.ImageIO;30

31 importjavax.imageio.stream.ImageOutputStream;32

33 /**

34

35 *36

37 *@authorzyong38

39 *验证码40

41 */

42

43 public classCheckCode {44

45 /**

46

47 *创建一个随机数对象48

49 */

50

51 Random random = newRandom();52

53 /**

54

55 *生成的字符集56

57 */

58

59 private final String character = "0ABC1DEF2GHI3JKL4MNO5PQR6ST7UV8WX9YZ";60

61 /**

62

63 *返回生成后的图片字符64

65 */

66

67 privateString checkCode;68

69 /**

70

71 * 设置生成图片的宽度,默认为6572

73 */

74

75 private int width = 65;76

77 /**

78

79 * 设置生成图片的高度,默认为2580

81 */

82

83 private int height = 25;84

85 /**

86

87 * 设置图片的类型,默认为BufferedImage.TYPE_INT_RGB88

89 */

90

91 private int imageType =BufferedImage.TYPE_INT_RGB;92

93 /**

94

95 *96

97 * @return生成图片后的字符98

99 */

100

101 publicString getCheckCode() {102

103 returncheckCode;104

105 }106

107 /**

108

109 *110

111 * @return图片的高度112

113 */

114

115 public intgetHeight() {116

117 returnheight;118

119 }120

121 /**

122

123 *124

125 *@paramheight图片的高度126

127 */

128

129 public void setHeight(intheight) {130

131 this.height =height;132

133 }134

135 /**

136

137 *138

139 * @return图片的宽度140

141 */

142

143 public intgetWidth() {144

145 returnwidth;146

147 }148

149 /**

150

151 *152

153 *@paramwidth图片的宽度154

155 */

156

157 public void setWidth(intwidth) {158

159 this.width =width;160

161 }162

163 /**

164

165 *166

167 * @return图片类型168

169 */

170

171 public intgetImageType() {172

173 returnimageType;174

175 }176

177 /**

178

179 *180

181 *@paramimageType图片类型182

183 */

184

185 public void setImageType(intimageType) {186

187 this.imageType =imageType;188

189 }190

191 /**

192

193 *194

195 * @return生成一张图片196

197 */

198

199 publicByteArrayInputStream buildImage() {200

201 BufferedImage image = new BufferedImage(this.width, this.height, this.imageType);202

203 Graphics g =image.getGraphics();204

205 Graphics2D g2d =(Graphics2D) g;206

207 g.setColor(this.getColor(200, 250));208

209 g.fillRect(0, 0, this.width, this.height);210

211 g.setFont(new Font("Times New Roman", Font.BOLD, 17));212

213 g.setColor(this.getColor(180, 200));214

215 /*

216

217 *绘制100条干扰线218

219 */

220

221 for (int i = 0; i < 100; i++) {222

223 int x1 = random.nextInt(this.width);224

225 int y1 = random.nextInt(this.height);226

227 int x2 = random.nextInt(this.width - 3);228

229 int y2 = random.nextInt(this.height - 3);230

231 BasicStroke bs = newBasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);232

233 Line2D line = newLine2D.Double(x1, y1, x2, y2);234

235 g2d.setStroke(bs);236

237 g2d.draw(line);238

239 g.setColor(getColor(180, 222));240

241 }242

243 StringBuffer codeStr = newStringBuffer();244

245 for (int i = 0; i < 4; i++) {246

247 char c = character.charAt(random.nextInt(36));248

249 codeStr.append(c);250

251 Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110));252

253 g.setColor(color); /*随机文字,旋转文字到指定角度*/

254

255 AffineTransform trans = newAffineTransform();256

257 trans.rotate(random.nextInt(10) * i + 3, 5);258

259 float scaleSize = random.nextFloat() + 0.5f;260

261 if (scaleSize < 0.8 || scaleSize > 1.1f) {262

263 scaleSize =1f;264

265 }266

267 trans.scale(scaleSize, scaleSize); g2d.setTransform(trans);268

269 g.drawString(String.valueOf(c), 15 * i + 6, 9);270

271 }272

273 this.checkCode =codeStr.toString(); g.dispose();274

275 ByteArrayInputStream inputStream = null;276

277 ByteArrayOutputStream outputStream = newByteArrayOutputStream();278

279 try{280

281 ImageOutputStream imgOutput =ImageIO.createImageOutputStream(outputStream);282

283 ImageIO.write(image, "JPEG", imgOutput);284

285 imgOutput.close();286

287 inputStream = newByteArrayInputStream(outputStream.toByteArray());288

289 outputStream.close();290

291 } catch(IOException e) {292

293 e.printStackTrace();294

295 }296

297 returninputStream;298

299 }300

301 /**

302

303 * 利用随机数,随机生成一个Color颜色的对象304

305 *@paramfc306

307 *@parambc308

309 * @return颜色对象310

311 */

312

313 private Color getColor(int fc, intbc) {314

315 if (fc > 255) {316

317 fc = 255;318

319 }320

321 if (bc > 255) {322

323 bc = 255;324

325 }326

327 int r = fc + random.nextInt(bc -fc);328

329 int g = fc + random.nextInt(bc -fc);330

331 int b = fc + random.nextInt(bc -fc);332

333 return newColor(r, g, b);334

335 }336

337 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值