您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java怎么生成qrCode”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java怎么生成qrCode”吧!
參數(shù)類:
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * */ public class QRCodeParams { private String txt; // 二維碼內(nèi)容 private String qrCodeUrl; // 二維碼網(wǎng)絡(luò)路徑 private String logoPath; // logo圖片 private Integer width = 300; // 二維碼寬度 private Integer height = 300; // 二維碼高度 private Integer onColor = 0xFF000000; // 前景色 private Integer offColor = 0xFFFFFFFF; // 背景色 private Integer margin = 1; // 白邊大小,取值范圍0~4 private ErrorCorrectionLevel level = ErrorCorrectionLevel.L; // 二維碼容錯(cuò)率 public String getTxt() { return txt; } public void setTxt(String txt) { this.txt = txt; } public Integer getWidth() { return width; } public void setWidth(Integer width) { this.width = width; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } public String getQrCodeUrl() { return qrCodeUrl; } public void setQrCodeUrl(String qrCodeUrl) { this.qrCodeUrl = qrCodeUrl; } public String getLogoPath() { return logoPath; } public void setLogoPath(String logoPath) { this.logoPath = logoPath; } public Integer getOnColor() { return onColor; } public void setOnColor(Integer onColor) { this.onColor = onColor; } public Integer getOffColor() { return offColor; } public void setOffColor(Integer offColor) { this.offColor = offColor; } public ErrorCorrectionLevel getLevel() { return level; } public void setLevel(ErrorCorrectionLevel level) { this.level = level; } public Integer getMargin() { return margin; } public void setMargin(Integer margin) { this.margin = margin; } }
圖標(biāo)配置類:
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import javax.imageio.ImageIO; /** */ public class QRLogoConfig { /** * 設(shè)置 logo * * @param matrixImage * 源二維碼圖片 * @return 返回帶有l(wèi)ogo的二維碼圖片 * @throws IOException * @author Administrator sangwenhao */ public BufferedImage LogoMatrix(BufferedImage matrixImage, String logoPath) throws IOException { /** * 讀取二維碼圖片,并構(gòu)建繪圖對(duì)象 */ Graphics2D g2 = matrixImage.createGraphics(); int matrixWidth = matrixImage.getWidth(); int matrixHeigh = matrixImage.getHeight(); int num = logoPath.indexOf('/', 8); String u = logoPath.substring(0, num); URL url = new URL(logoPath); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("referer", u); /** * 讀取Logo圖片 */ BufferedImage logo = ImageIO.read(connection.getInputStream()); // 開始繪制圖片 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);// 設(shè)置筆畫對(duì)象 // 指定弧度的圓角矩形 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);// 繪制圓弧矩形 // 設(shè)置logo 有一道灰色邊框 BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 設(shè)置筆畫對(duì)象 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(); matrixImage.flush(); return matrixImage; } }
異常類:
/** * */ public class QRParamsException extends Exception { private static final long serialVersionUID = 8837582301762730656L; public QRParamsException() { } // 用來創(chuàng)建無參數(shù)對(duì)象 public QRParamsException(String message) { // 用來創(chuàng)建指定參數(shù)對(duì)象 super(message); // 調(diào)用超類構(gòu)造器 } }
工具處理類:
import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import org.apache.commons.lang3.StringUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * */ public class QRCodeUtil { private static int width = 500; // 二維碼寬度 private static int height = 500; // 二維碼高度 private static int onColor = 0xFF000000; // 前景色 private static int offColor = 0xFFFFFFFF; // 背景色 private static int margin = 1; // 白邊大小,取值范圍0~4 private static ErrorCorrectionLevel level = ErrorCorrectionLevel.L; // 二維碼容錯(cuò)率 /** * 生成二維碼 * * @param params * QRCodeParams屬性:txt、fileName、filePath不得為空; * @throws QRParamsException */ public static ByteArrayOutputStream generateQRImage(QRCodeParams params) throws QRParamsException { if (params == null || params.getTxt() == null) { throw new QRParamsException("參數(shù)錯(cuò)誤"); } try { initData(params); String txt = params.getTxt(); if (StringUtils.isNoneBlank(params.getLogoPath())) { return generateQRImage(txt, params.getLogoPath()); } else { return generateQRImage(txt); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 生成二維碼 * * @param txt * //二維碼內(nèi)容 */ public static ByteArrayOutputStream generateQRImage(String txt) { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); // 指定糾錯(cuò)等級(jí) hints.put(EncodeHintType.ERROR_CORRECTION, level); // 指定編碼格式 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, margin); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(txt, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = toBufferedImage(bitMatrix); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(image, "PNG", out); return out; } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 生成帶logo的二維碼圖片 * * @param txt * //二維碼內(nèi)容 * @param logoPath * //logo絕對(duì)物理路徑 * @throws Exception */ public static ByteArrayOutputStream generateQRImage(String txt, String logoPath) throws Exception { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); // 指定糾錯(cuò)等級(jí),糾錯(cuò)級(jí)別(L 7%、M 15%、Q 25%、H 30%) hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.MARGIN, margin); BitMatrix bitMatrix = new MultiFormatWriter().encode(txt, BarcodeFormat.QR_CODE, width, height, hints); return writeToFile(bitMatrix, logoPath); } /** * * @param matrix * 二維碼矩陣相關(guān) * @param format * 二維碼圖片格式 * @param file * 二維碼圖片文件 * @param logoPath * logo路徑 * @throws IOException */ public static void writeToFile(BitMatrix matrix, String format, File file, String logoPath) throws IOException { BufferedImage image = toBufferedImage(matrix); Graphics2D gs = image.createGraphics(); int ratioWidth = image.getWidth() * 2 / 10; int ratioHeight = image.getHeight() * 2 / 10; // 載入logo Image img = ImageIO.read(new File(logoPath)); int logoWidth = img.getWidth(null) > ratioWidth ? ratioWidth : img .getWidth(null); int logoHeight = img.getHeight(null) > ratioHeight ? ratioHeight : img .getHeight(null); int x = (image.getWidth() - logoWidth) / 2; int y = (image.getHeight() - logoHeight) / 2; gs.drawImage(img, x, y, logoWidth, logoHeight, null); gs.setColor(Color.black); gs.setBackground(Color.WHITE); gs.dispose(); img.flush(); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static ByteArrayOutputStream writeToFile(BitMatrix matrix, String logoPath) throws IOException { BufferedImage image = toBufferedImage(matrix); // 設(shè)置logo圖標(biāo) QRLogoConfig logoConfig = new QRLogoConfig(); image = logoConfig.LogoMatrix(image, logoPath); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(image, "PNG", out); return out; } catch (IOException e) { e.printStackTrace(); } return null; } public 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) ? onColor : offColor); } } return image; } public 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; } private static void initData(QRCodeParams params) { if (params.getWidth() != null) { width = params.getWidth(); } if (params.getHeight() != null) { height = params.getHeight(); } if (params.getOnColor() != null) { onColor = params.getOnColor(); } if (params.getOffColor() != null) { offColor = params.getOffColor(); } if (params.getLevel() != null) { level = params.getLevel(); } } }
調(diào)用:
QRCodeParams qrParams = new QRCodeParams(); // url = "http://192.168.1.2:8086/business"; qrParams.setTxt(url); qrParams.setWidth(300); qrParams.setHeight(300); ByteArrayOutputStream out = QRCodeUtil.generateQRImage(qrParams); qrCode = this.uploadService.upload(out,IdUtils.id()+".png");
到此,相信大家對(duì)“java怎么生成qrCode”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。