利用Java產生帶有文字的二維碼_java

來源:互聯網
上載者:User

介紹

主要使用了goole的zxing包,下面給出了範例程式碼,很方便大家的理解和學習,代碼都屬於初步架構,功能有了,需要根據實際使用方式完善最佳化。

第一步、maven匯入zxing

<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version></dependency>

第二步、開始產生二維碼:

private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;/** * 把產生的二維碼存入到圖片中 * @param matrix zxing包下的二維碼類 * @return */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) ? BLACK : WHITE);  } } return image;}/** * 產生二維碼並寫入檔案 * @param content 掃描二維碼的內容 * @param format 圖片格式 jpg * @param file  檔案 * @throws Exception */public static void writeToFile(String content, String format, File file)  throws Exception { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); @SuppressWarnings("rawtypes") Map hints = new HashMap(); //設定UTF-8, 防止中文亂碼 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //設定二維碼四周白色地區的大小 hints.put(EncodeHintType.MARGIN,1); //設定二維碼的容錯性 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //畫二維碼 BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints); BufferedImage image = toBufferedImage(bitMatrix); if (!ImageIO.write(image, format, file)) {  throw new IOException("Could not write an image of format " + format + " to " + file); }}

第三步、把文字寫入二維碼圖片中:

/** * 給二維碼圖片加上文字 * @param pressText 文字 * @param qrFile  二維碼檔案 * @param fontStyle * @param color * @param fontSize */public static void pressText(String pressText, File qrFile, int fontStyle, Color color, int fontSize) throws Exception { pressText = new String(pressText.getBytes(), "utf-8"); Image src = ImageIO.read(qrFile); int imageW = src.getWidth(null); int imageH = src.getHeight(null); BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); g.drawImage(src, 0, 0, imageW, imageH, null); //設定畫筆的顏色 g.setColor(color); //設定字型 Font font = new Font("宋體", fontStyle, fontSize); FontMetrics metrics = g.getFontMetrics(font); //文字在圖片中的座標 這裡設定在中間 int startX = (WIDTH - metrics.stringWidth(pressText)) / 2; int startY = HEIGHT/2; g.setFont(font); g.drawString(pressText, startX, startY); g.dispose(); FileOutputStream out = new FileOutputStream(qrFile); ImageIO.write(image, "JPEG", out); out.close(); System.out.println("image press success");}

第四步、在main方法中測試一下,一個中間帶文字的二維碼就產生了

public static void main(String[] args) throws Exception { File qrcFile = new File("/Users/deweixu/","google.jpg"); writeToFile("www.google.com.hk", "jpg", qrcFile); pressText("Google", qrcFile, 5, Color.RED, 32);}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.