java二维码生成代码
import com.google.zxing.common.BitMatrix; import java.awt.image.BufferedImage; import java.awt.Graphics2D; import java.awt.Image; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.Hashtable; import javax.imageio.ImageIO; import org.springframework.core.io.ClassPathResource; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; /** * 二维码生成工具 * @Auth */ public final class MatrixToImageWriter { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static final int MARGIN = 1; //边框 private static final String FORMAT = "png"; private MatrixToImageWriter() { } public static void createRqCode(String textOrUrl, int width, int height, OutputStream toStream) throws WriterException, IOException { Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码 hints.put(EncodeHintType.MARGIN, new Integer(MARGIN)); BitMatrix bitMatrix = new MultiFormatWriter().encode(textOrUrl, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = toBufferedImage(bitMatrix); //应用LOGO //applyLogo(image); writeToStream(image, FORMAT, toStream); } private static void applyLogo(BufferedImage image) throws IOException { Graphics2D gs = image.createGraphics(); ClassPathResource resource = new ClassPathResource("logo.png");//logo图片 // 载入logo Image img = ImageIO.read(resource.getFile()); int left = image.getWidth() / 2 - img.getWidth(null) / 2; int top = image.getHeight() / 2 - img.getHeight(null) / 2; gs.drawImage(img, left, top, null); gs.dispose(); img.flush(); } private static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } return image; } public static void writeToFile(BufferedImage image, String format, File file) throws IOException { if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static void writeToStream(BufferedImage image, String format, OutputStream stream) throws IOException { if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } }
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency>
相关推荐
-
zookeeper分布式锁 java
2019-1-13
-
Java版跳一跳 java
2019-1-13
-
java常用来验证的一个类,各种正则匹配 java
2019-1-7
-
java模拟表单请求 java
2019-1-8
-
properties转换yml格式 java
2019-1-13
-
删除 java代码中所有的注释 java
2019-1-7
-
Restful Api调用工具类 java
2019-1-8
-
QRCodeGenerator.java java
2019-1-8
-
二维码的生成和读取 java
2019-1-7
-
一个数组求倒数第二个大值 java
2019-1-7