java生成二维码
1.文件分布
2.工具类代码
1 import java.awt.BasicStroke; 2 import java.awt.Graphics; 3 import java.awt.Graphics2D; 4 import java.awt.Image; 5 import java.awt.Shape; 6 import java.awt.geom.RoundRectangle2D; 7 import java.awt.image.BufferedImage; 8 import java.io.File; 9 import java.text.SimpleDateFormat; 10 import java.util.Date; 11 import java.util.Hashtable; 12 13 import javax.imageio.ImageIO; 14 15 import com.google.zxing.BarcodeFormat; 16 import com.google.zxing.EncodeHintType; 17 import com.google.zxing.MultiFormatWriter; 18 import com.google.zxing.common.BitMatrix; 19 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 20 21 public class QRCodeUtil { 22 private static final String CHARSET = "utf-8"; 23 private static final String FORMAT_NAME = "JPG"; 24 private static final int QRCODE_SIZE = 300; 25 private static final int WIDTH = 60; 26 private static final int HEIGHT = 60; 27 28 @SuppressWarnings({ "rawtypes", "unchecked" }) 29 private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception { 30 Hashtable hints = new Hashtable(); 31 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 32 hints.put(EncodeHintType.CHARACTER_SET, CHARSET); 33 hints.put(EncodeHintType.MARGIN, 1); 34 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); 35 int width = bitMatrix.getWidth(); 36 int height = bitMatrix.getHeight(); 37 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 38 for (int x = 0; x < width; x++) { 39 for (int y = 0; y < height; y++) { 40 image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); 41 } 42 } 43 if (imgPath == null || "".equals(imgPath)) { 44 return image; 45 } 46 QRCodeUtil.insertImage(image, imgPath, needCompress); 47 return image; 48 } 49 50 private static void insertImage(BufferedImage source, String imgPath,boolean needCompress) throws Exception { 51 File file = new File(imgPath); 52 if (!file.exists()) { 53 System.err.println(""+imgPath+" The file does not exist"); 54 return; 55 } 56 Image src = ImageIO.read(new File(imgPath)); 57 int width = src.getWidth(null); 58 int height = src.getHeight(null); 59 if (needCompress) { 60 if (width > WIDTH) { 61 width = WIDTH; 62 } 63 if (height > HEIGHT) { 64 height = HEIGHT; 65 } 66 Image image = src.getScaledInstance(width, height,Image.SCALE_SMOOTH); 67 BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 68 Graphics g = tag.getGraphics(); 69 g.drawImage(image, 0, 0, null); 70 g.dispose(); 71 src = image; 72 } 73 Graphics2D graph = source.createGraphics(); 74 int x = (QRCODE_SIZE - width) / 2; 75 int y = (QRCODE_SIZE - height) / 2; 76 graph.drawImage(src, x, y, width, height, null); 77 Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6); 78 graph.setStroke(new BasicStroke(3f)); 79 graph.draw(shape); 80 graph.dispose(); 81 } 82 83 public static void encode(String content, String imgPath, String destPath, 84 boolean needCompress) throws Exception { 85 BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress); 86 mkdirs(destPath); 87 Date date = new Date(); 88 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); 89 String str = format.format(date); 90 String file = str +".jpg"; 91 ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file)); 92 System.out.println("二维码已生成,在"+destPath+"文件夹下");//The two-dimensional code has been generated , is located in the destPath folder 93 } 94 95 public static void mkdirs(String destPath) { 96 File file =new File(destPath); 97 if (!file.exists() && !file.isDirectory()) { 98 file.mkdirs(); 99 } 100 } 101 102 }
3.测试类代码
1 import java.util.Scanner; 2 3 public class Test { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 try { 10 Scanner scanner = new Scanner(System.in); 11 System.out.println("请输入内容:"); 12 String content = scanner.nextLine(); 13 if(content.isEmpty())Test.main(args); 14 QRCodeUtil.encode(content,"","D:/QRcode",true); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 } 18 Test.main(args); 19 } 20 }
4.jar包下载路径
http://pan.baidu.com/s/1pLAvw75
注:该程序的编码均为GBK编码,否则会出现奇数中文最后一个字乱码
成功不是终点,失败也并非末日,重要的是前行的勇气!