博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

几种验证码的生成

Posted on 2013-01-30 17:58  无、为  阅读(2191)  评论(0编辑  收藏  举报
通过barcode4j生成
Java代码 复制代码 收藏代码
  1. @Controller  
  2. @RequestMapping("/bar/{code}")   
  3. public class BarCodeController {   
  4.     @RequestMapping(method = RequestMethod.GET)   
  5.     public void show(@PathVariable("code") String code,   
  6.             HttpServletRequest request, HttpServletResponse response)   
  7.             throws ConfigurationException, BarcodeException, IOException {   
  8.         DefaultConfiguration cfg = new DefaultConfiguration("barcode");   
  9.         DefaultConfiguration child = new DefaultConfiguration("datamatrix"); //code128   
  10.         DefaultConfiguration attr;   
  11.         //attr= new DefaultConfiguration("height");   
  12.         //attr.setValue("10");   
  13.         cfg.addChild(child);   
  14.         //child.addChild(attr);   
  15.         attr = new DefaultConfiguration("module-width");   
  16.         attr.setValue("0.6");   
  17.         child.addChild(attr);   
  18.         int orientation = 0;   
  19.          int resolution = 300;    
  20.             
  21.         BarcodeUtil util = BarcodeUtil.getInstance();   
  22.         BarcodeGenerator gen = util.createBarcodeGenerator(cfg);   
  23.   
  24.         ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);   
  25.   
  26.            
  27.         BitmapCanvasProvider bitmap = new BitmapCanvasProvider(bout,   
  28.                 MimeTypes.MIME_JPEG, resolution, BufferedImage.TYPE_BYTE_BINARY,   
  29.                 false, orientation);   
  30.         gen.generateBarcode(bitmap,code);   
  31.         try {   
  32.             bitmap.finish();   
  33.         } catch (IOException e) {   
  34.         } finally {   
  35.             try {   
  36.                 bout.close();   
  37.             } catch (IOException e) {   
  38.             }   
  39.         }   
  40.         response.setContentType(MimeTypes.MIME_JPEG);   
  41.         response.setContentLength(bout.size());   
  42.         response.getOutputStream().write(bout.toByteArray());   
  43.         response.getOutputStream().flush();   
  44.     }   
  45. }  
@Controller
@RequestMapping("/bar/{code}")
public class BarCodeController {
	@RequestMapping(method = RequestMethod.GET)
	public void show(@PathVariable("code") String code,
			HttpServletRequest request, HttpServletResponse response)
			throws ConfigurationException, BarcodeException, IOException {
		DefaultConfiguration cfg = new DefaultConfiguration("barcode");
		DefaultConfiguration child = new DefaultConfiguration("datamatrix"); //code128
		DefaultConfiguration attr;
		//attr= new DefaultConfiguration("height");
		//attr.setValue("10");
		cfg.addChild(child);
		//child.addChild(attr);
		attr = new DefaultConfiguration("module-width");
		attr.setValue("0.6");
		child.addChild(attr);
		int orientation = 0;
		 int resolution = 300; 
		 
		BarcodeUtil util = BarcodeUtil.getInstance();
		BarcodeGenerator gen = util.createBarcodeGenerator(cfg);

		ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);

		
		BitmapCanvasProvider bitmap = new BitmapCanvasProvider(bout,
				MimeTypes.MIME_JPEG, resolution, BufferedImage.TYPE_BYTE_BINARY,
				false, orientation);
		gen.generateBarcode(bitmap,code);
		try {
			bitmap.finish();
		} catch (IOException e) {
		} finally {
			try {
				bout.close();
			} catch (IOException e) {
			}
		}
		response.setContentType(MimeTypes.MIME_JPEG);
		response.setContentLength(bout.size());
		response.getOutputStream().write(bout.toByteArray());
		response.getOutputStream().flush();
	}
}

通过zxing生成
Java代码 复制代码 收藏代码
  1. package gov.rsj.controller;    
  2.   
  3. import java.io.IOException;   
  4.   
  5. import javax.servlet.http.HttpServletRequest;   
  6. import javax.servlet.http.HttpServletResponse;   
  7.   
  8. import org.springframework.stereotype.Controller;   
  9. import org.springframework.web.bind.annotation.PathVariable;   
  10. import org.springframework.web.bind.annotation.RequestMapping;   
  11. import org.springframework.web.bind.annotation.RequestMethod;   
  12.   
  13. import com.google.zxing.BarcodeFormat;   
  14. import com.google.zxing.WriterException;   
  15. import com.google.zxing.client.j2se.MatrixToImageWriter;   
  16. import com.google.zxing.common.BitMatrix;   
  17. import com.google.zxing.qrcode.QRCodeWriter;   
  18.   
  19. /**   
  20.  * @author fox   
  21.  * @date 2012-3-22 下午3:12:33   
  22.  * @version 1.0  
  23.  * @description QRCODE 条形码  需要javase.jar和core.jar两个包  
  24.  */  
  25. @Controller  
  26. @RequestMapping("/qrbar/{code}")   
  27. public class QrBarCodeController {   
  28.     @RequestMapping(method = RequestMethod.GET)   
  29.     public void show(@PathVariable("code") String code,   
  30.             HttpServletRequest request, HttpServletResponse response){   
  31.         QRCodeWriter writer = new QRCodeWriter();   
  32.         response.setHeader("Pragma""No-cache");   
  33.         response.setHeader("Cache-Control""no-cache");   
  34.         response.setDateHeader("Expires"0);   
  35.         response.setContentType("image/jpeg");   
  36.         BitMatrix bitMatrix = null;   
  37.         try {   
  38.             bitMatrix = writer.encode(code, BarcodeFormat.QR_CODE, 300300);   
  39.             MatrixToImageWriter.writeToStream(bitMatrix, "jpeg", response.getOutputStream());   
  40.             response.getOutputStream().flush();   
  41.             response.getOutputStream().close();   
  42.         } catch (WriterException e) {   
  43.             e.printStackTrace();   
  44.         } catch (IOException e) {   
  45.             e.printStackTrace();   
  46.         }   
  47.            
  48.            
  49.     }   
  50. }  
package gov.rsj.controller; 

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

/** 
 * @author fox 
 * @date 2012-3-22 下午3:12:33 
 * @version 1.0
 * @description QRCODE 条形码  需要javase.jar和core.jar两个包
 */
@Controller
@RequestMapping("/qrbar/{code}")
public class QrBarCodeController {
	@RequestMapping(method = RequestMethod.GET)
	public void show(@PathVariable("code") String code,
			HttpServletRequest request, HttpServletResponse response){
		QRCodeWriter writer = new QRCodeWriter();
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = writer.encode(code, BarcodeFormat.QR_CODE, 300, 300);
            MatrixToImageWriter.writeToStream(bitMatrix, "jpeg", response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
		
	}
}