《百度云平台验证码实现》-----javaweb

时间:2021-04-28 08:56:29
package com.fenghuo.controller;


import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.baidu.bae.api.factory.BaeFactory;
import com.baidu.bae.api.image.Annotate;
import com.baidu.bae.api.image.BaeImageService;
import com.baidu.bae.api.image.Image;
import com.baidu.bae.api.image.VCode;


public class IdentifyingCode extends HttpServlet {


/**

*/
private static final long serialVersionUID = -6642061549903457669L;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//获取服务类对象
BaeImageService service = BaeFactory.getBaeImageService();
VCode vc = new VCode();
vc.setLen(4);
// 获取验证码信息
Map<String,String> data = service.generateVCode(vc);

HttpSession session = request.getSession();
session.setAttribute("imgurl", data.get("imgurl"));
session.setAttribute("secret", data.get("secret"));
         
          String url = data.get("imgurl");
//创建图片对象
Image image = new Image(url);
//创建文字水印功能对象
          String text = "wxfjava";
Annotate annotate = new Annotate(text);
annotate.setFont(0,12,"D4E7F3");
annotate.setOpacity(0.5f);


          annotate.setPos(55,15);
    //调用文字水印服务
byte[] bs = service.applyAnnotate(image, annotate);
BufferedOutputStream bos = new BufferedOutputStream(
response.getOutputStream());
bos.write(bs, 0, bs.length);

if (bos != null)
bos.close();
bos.flush();
} catch (Exception e) {
}

}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


doGet(request, response);
}


}


《百度云平台验证码实现》-----javaweb