Java创建属于自己的二维码(完整版)
Posted 咯咯咯咯哒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java创建属于自己的二维码(完整版)相关的知识,希望对你有一定的参考价值。
1、准备需要的jar包
core-3.0.0.jar
zxing.jar
下载地址:关注微信公众号 “IT资源分享平台”,或者扫描右上角二维码。自行获取
2、把jar包放到项目工程jar目录下(没有的话自己创建一个jar文件夹),选中两个jar包右键选择Build Path下的Add to Build Path。最终结构如下图。
3、把下面两个Java文件放到src下
结构图如下:
BufferedImageLuminanceSource.java
package codeTest; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import com.google.zxing.LuminanceSource; public class BufferedImageLuminanceSource extends LuminanceSource{ private final BufferedImage image; private final int left; private final int top; public BufferedImageLuminanceSource(BufferedImage image) { this(image, 0, 0, image.getWidth(), image.getHeight()); } public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) { super(width, height); int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); if (left + width > sourceWidth || top + height > sourceHeight) { throw new IllegalArgumentException( "Crop rectangle does not fit within image data."); } for (int y = top; y < top + height; y++) { for (int x = left; x < left + width; x++) { if ((image.getRGB(x, y) & 0xFF000000) == 0) { image.setRGB(x, y, 0xFFFFFFFF); // = white } } } this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY); this.image.getGraphics().drawImage(image, 0, 0, null); this.left = left; this.top = top; } @Override public byte[] getMatrix() { int width = getWidth(); int height = getHeight(); int area = width * height; byte[] matrix = new byte[area]; image.getRaster().getDataElements(left, top, width, height, matrix); return matrix; } @Override public byte[] getRow(int y, byte[] row) { if (y < 0 || y >= getHeight()) { throw new IllegalArgumentException( "Requested row is outside the image: " + y); } int width = getWidth(); if (row == null || row.length < width) { row = new byte[width]; } image.getRaster().getDataElements(left, top + y, width, 1, row); return row; } public boolean isCropSupported() { return true; } public LuminanceSource crop(int left, int top, int width, int height) { return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height); } public boolean isRotateSupported() { return true; } public LuminanceSource rotateCounterClockwise() { int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g = rotatedImage.createGraphics(); g.drawImage(image, transform, null); g.dispose(); int width = getWidth(); return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width); } }
QRCodeUtil.java
1 package codeTest; 2 3 import java.awt.BasicStroke; 4 import java.awt.Graphics; 5 import java.awt.Graphics2D; 6 import java.awt.Image; 7 import java.awt.Shape; 8 import java.awt.geom.RoundRectangle2D; 9 import java.awt.image.BufferedImage; 10 import java.io.File; 11 import java.io.OutputStream; 12 import java.util.Hashtable; 13 import java.util.Random; 14 15 import javax.imageio.ImageIO; 16 17 import com.google.zxing.BarcodeFormat; 18 import com.google.zxing.BinaryBitmap; 19 import com.google.zxing.DecodeHintType; 20 import com.google.zxing.EncodeHintType; 21 import com.google.zxing.MultiFormatReader; 22 import com.google.zxing.MultiFormatWriter; 23 import com.google.zxing.Result; 24 import com.google.zxing.common.BitMatrix; 25 import com.google.zxing.common.HybridBinarizer; 26 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 27 28 public class QRCodeUtil { 29 private static final String CHARSET = "utf-8"; 30 private static final String FORMAT_NAME = "JPG"; 31 // 二维码尺寸 32 private static final int QRCODE_SIZE = 300; 33 // LOGO宽度 34 private static final int WIDTH = 60; 35 // LOGO高度 36 private static final int HEIGHT = 60; 37 38 private static BufferedImage createImage(String content, String imgPath, 39 boolean needCompress) throws Exception { 40 Hashtable hints = new Hashtable(); 41 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 42 hints.put(EncodeHintType.CHARACTER_SET, CHARSET); 43 hints.put(EncodeHintType.MARGIN, 1); 44 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, 45 BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); 46 int width = bitMatrix.getWidth(); 47 int height = bitMatrix.getHeight(); 48 BufferedImage image = new BufferedImage(width, height, 49 BufferedImage.TYPE_INT_RGB); 50 for (int x = 0; x < width; x++) { 51 for (int y = 0; y < height; y++) { 52 image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 53 : 0xFFFFFFFF); 54 } 55 } 56 if (imgPath == null || "".equals(imgPath)) { 57 return image; 58 } 59 // 插入图片 60 QRCodeUtil.insertImage(image, imgPath, needCompress); 61 return image; 62 } 63 private static void insertImage(BufferedImage source, String imgPath, 64 boolean needCompress) throws Exception { 65 File file = new File(imgPath); 66 if (!file.exists()) { 67 System.err.println(""+imgPath+" 该文件不存在!"); 68 return; 69 } 70 Image src = ImageIO.read(new File(imgPath)); 71 int width = src.getWidth(null); 72 int height = src.getHeight(null); 73 if (needCompress) { // 压缩LOGO 74 if (width > WIDTH) { 75 width = WIDTH; 76 } 77 if (height > HEIGHT) { 78 height = HEIGHT; 79 } 80 Image image = src.getScaledInstance(width, height, 81 Image.SCALE_SMOOTH); 82 BufferedImage tag = new BufferedImage(width, height, 83 BufferedImage.TYPE_INT_RGB); 84 Graphics g = tag.getGraphics(); 85 g.drawImage(image, 0, 0, null); // 绘制缩小后的图 86 g.dispose(); 87 src = image; 88 } 89 // 插入LOGO 90 Graphics2D graph = source.createGraphics(); 91 int x = (QRCODE_SIZE - width) / 2; 92 int y = (QRCODE_SIZE - height) / 2; 93 graph.drawImage(src, x, y, width, height, null); 94 Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6); 95 graph.setStroke(new BasicStroke(3f)); 96 graph.draw(shape); 97 graph.dispose(); 98 } 99 public static void encode(String content, String imgPath, String destPath, 100 boolean needCompress) throws Exception { 101 BufferedImage image = QRCodeUtil.createImage(content, imgPath, 102 needCompress); 103 mkdirs(destPath); 104 String file = new Random().nextInt(99999999)+".jpg"; 105 ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file)); 106 } 107 public static void mkdirs(String destPath) { 108 File file =new File(destPath); 109 //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) 110 if (!file.exists() && !file.isDirectory()) { 111 file.mkdirs(); 112 } 113 } 114 public static void encode(String content, String imgPath, String destPath) 115 throws Exception { 116 QRCodeUtil.encode(content, imgPath, destPath, false); 117 } 118 119 120 public static void encode(String content, String destPath, 121 boolean needCompress) throws Exception { 122 QRCodeUtil.encode(content, null, destPath, needCompress); 123 } 124 125 126 public static void encode(String content, String destPath) throws Exception { 127 QRCodeUtil.encode(content, null, destPath, false); 128 } 129 130 131 public static void encode(String content, String imgPath, 132 OutputStream output, boolean needCompress) throws Exception { 133 BufferedImage image = QRCodeUtil.createImage(content, imgPath, 134 needCompress); 135 ImageIO.write(image, FORMAT_NAME, output); 136 } 137 138 139 public static void encode(String content, OutputStream output) 140 throws Exception { 141 QRCodeUtil.encode(content, null, output, false); 142 } 143 144 145 public static String decode(File file) throws Exception { 146 BufferedImage image; 147 image = ImageIO.read(file); 148 if (image == null) { 149 return null; 150 } 151 BufferedImageLuminanceSource source = new BufferedImageLuminanceSource( 152 image); 153 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 154 Result result; 155 Hashtable hints = new Hashtable(); 156 hints.put(DecodeHintType.CHARACTER_SET, CHARSET); 157 result = new MultiFormatReader().decode(bitmap, hints); 158 String resultStr = result.getText(); 159 return resultStr; 160 } 161 162 163 public static String decode(String path) throws Exception { 164 return QRCodeUtil.decode(new File(path)); 165 } 166 public static void main(String[] args) throws Exception { 167 String text = "http://itcsdr.com/"; 168 169 // 在你盘符下放一个图片 路径:D:/Java-jar/erweima.jpg 生成二维码的路径:D:/Java-jar 170 QRCodeUtil.encode(text, "D:/Java-jar/logo.jpg", "D:/Java-jar", true); 171 } 172 }
最终效果:
以上是关于Java创建属于自己的二维码(完整版)的主要内容,如果未能解决你的问题,请参考以下文章
IT好课SpringBoot2零基础入门springboot全套完整版网盘分享