本代码为本人开发过程中,二维码生成初级测试过程中的程序,有参考其他前辈代码
jar包依赖
<!-- 条形码、二维码生成 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>2.2</version> </dependency>
controller
@RequestMapping(value="ewm") public ModelAndView ewm(Site site,HttpServletRequest request, HttpServletResponse response, Model model) { String uploadPath = "/images" ; HttpSession session = request.getSession(); String realUploadPath = session.getServletContext().getRealPath(uploadPath) ; String imageName = "12345"+".png" ; // 模拟订单详情 //String contents = "订单编号:20160512082345"+"\n"+"订单金额:¥ 2050.00"+"\n"+"支付方式:预存款"+"\n"+"配送方式:京东快递" ; String contents = "7986464S64D6464SD46464" ; int width = 200; int height = 200; String zxingImage = coderService.encode(contents, width, height, uploadPath, realUploadPath, imageName); File image = new File(realUploadPath+"/"+imageName); String logoImageUrl = LogoConfig.LogoMatrix(image, uploadPath, realUploadPath, imageName) ; ModelAndView ret = new ModelAndView() ; //ret.addObject("imageUrl", zxingImage) ; ret.addObject("imageName", imageName) ; ret.addObject("logoImageUrl", logoImageUrl) ; ret.addObject("ceshi", uploadPath+"/"+imageName) ; ret.setViewName("zxingcoder"); return ret ; }service
public class CoderService { public String encode(String contents, int width, int height, String uploadPath, String realUploadPath, String imageName) { //生成条形码时的一些配置 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%) hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 内容所使用字符集编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); OutputStream out=null; try { if(realUploadPath!=null&&realUploadPath!=""){ out = new FileOutputStream(realUploadPath+"/"+imageName); } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } BitMatrix bitMatrix; try { // 生成二维码 bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); //bitMatrix = deleteWhite(bitMatrix);//删除白边 MatrixToImageWriter.writeToStream(bitMatrix, "png", out); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return uploadPath+"/"+imageName ; }
//去除百变所需 private static BitMatrix deleteWhite(BitMatrix matrix) { int[] rec = matrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j); } } return resMatrix; }
添加二维码logo
public class LogoConfig { public static String LogoMatrix(File image, String uploadPath, String realUploadPath, String imgPath) { /** * 读取二维码图片,并构建绘图对象 */ OutputStream os = null ; String logoFileName = "logo_"+imgPath ; try { Image image2 = ImageIO.read(image) ; int width = image2.getWidth(null) ; int height = image2.getHeight(null) ; BufferedImage bufferImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) ; //BufferedImage bufferImage =ImageIO.read(image) ; Graphics2D g2 = bufferImage.createGraphics(); g2.drawImage(image2, 0, 0, width, height, null) ; int matrixWidth = bufferImage.getWidth(); int matrixHeigh = bufferImage.getHeight(); //读取Logo图片 BufferedImage logo= ImageIO.read(new File(realUploadPath+"/"+"logo.jpg")); //开始绘制图片 g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制 BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke);// 设置笔画对象 //指定弧度的圆角矩形 RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20); g2.setColor(Color.white); g2.draw(round);// 绘制圆弧矩形 //设置logo 有一道灰色边框 BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 设置笔画对象 RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20); g2.setColor(new Color(128,128,128)); g2.draw(round2);// 绘制圆弧矩形 g2.dispose(); bufferImage.flush() ; os = new FileOutputStream(realUploadPath+"/"+logoFileName) ; JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os) ; en.encode(bufferImage) ; } catch (Exception e) { e.printStackTrace(); } finally { if(os!=null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return uploadPath+"/"+logoFileName ; }