日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java制作带有logo的二维码,解决zxing中文乱码

發(fā)布時(shí)間:2024/10/5 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java制作带有logo的二维码,解决zxing中文乱码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目標(biāo)

  • 使用谷歌zxing生成帶有l(wèi)ogo二維碼
  • 便捷地解決二維碼中文亂碼問題
  • 過程

  • 下載依賴:
    maven坐標(biāo):
  • <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version> </dependency>
  • 編寫java代碼生成二維碼,并繪制logo
  • /*** @description:* @param qrUrl 二維碼對(duì)應(yīng)url* @param logoUrl logo url* @param outputPath 輸出路徑* @param note 二維碼備注* @param width* @param height* @exception Exception*/public static void drawQrCode(String qrUrl, String logoUrl, String outputPath, String note, int width, int height) throws Exception{MultiFormatWriter multiFormatWriter = new MultiFormatWriter();BitMatrix bitMatrix = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, width, height, new HashMap<EncodeHintType, Object>() {private static final long serialVersionUID = 1L;{put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);put(EncodeHintType.CHARACTER_SET, "UTF-8");put(EncodeHintType.MARGIN, 0);}});BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 開始利用二維碼數(shù)據(jù)圖片,分別設(shè)為黑(0xFFFFFFFF)白(0xFF000000)兩色for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);}}if (logoUrl != null && !"".equals(logoUrl) ){//繪制二維碼圖Graphics2D graphics = image.createGraphics();//讀取logo圖BufferedImage logoImg = ImageIO.read( new URL(logoUrl) );//繪制logo圖片graphics.drawImage(logoImg, width * 2 / 6, height * 2 / 6, width * 3 / 10, height * 3 / 10, null);graphics.dispose();logoImg.flush();}//設(shè)置二維碼備注if (note != null && !"".equals(note)){BufferedImage outImage = new BufferedImage(width, height + 45, BufferedImage.TYPE_4BYTE_ABGR);Graphics2D outGraphics = outImage.createGraphics();// 畫二維碼到新的面板outGraphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);// 畫文字到新的面板outGraphics.setColor(Color.BLACK);outGraphics.setFont( new Font("Arial", Font.BOLD, 26) );int noteWidth = outGraphics.getFontMetrics().stringWidth(note);// 畫文字outGraphics.drawString(new QrCodeAttributedCharacter(note), (width - noteWidth) / 2, image.getHeight() + (outImage.getHeight() - image.getHeight()) / 2 + 12);outGraphics.dispose();outImage.flush();image = outImage;}image.flush();ImageIO.write(image, "png", Paths.get(outputPath).toFile());}
  • 編寫QrCodeAttributedCharacter解決中文亂碼問題
  • /*** 二維碼文本wrapper,用來(lái)防止中文亂碼*/ public class QrCodeAttributedCharacter implements AttributedCharacterIterator {private int index;private char[] textChars;public QrCodeAttributedCharacter(String text) {this.textChars = text.toCharArray();}@Overridepublic int getRunStart() {return 0;}@Overridepublic int getRunStart(Attribute attribute) {return 0;}@Overridepublic int getRunStart(Set<? extends Attribute> attributes) {return 0;}@Overridepublic int getRunLimit() {return this.textChars.length;}@Overridepublic int getRunLimit(Attribute attribute) {return this.textChars.length;}@Overridepublic int getRunLimit(Set<? extends Attribute> attributes) {return this.textChars.length;}@Overridepublic Map<Attribute, Object> getAttributes() {return Collections.EMPTY_MAP;}@Overridepublic Object getAttribute(Attribute attribute) {return Collections.EMPTY_MAP;}@Overridepublic Set<Attribute> getAllAttributeKeys() {return Collections.EMPTY_SET;}@Overridepublic char first() {this.index = 0;return this.textChars[0];}@Overridepublic char last() {return this.textChars[ this.textChars.length - 1 ];}@Overridepublic char current() {return this.textChars[this.index];}@Overridepublic char next() {if (this.index == this.textChars.length - 1){return CharacterIterator.DONE;}return this.textChars[ ++this.index ];}@Overridepublic char previous() {if (this.index == 0){return CharacterIterator.DONE;}return this.textChars[this.index - 1];}@Overridepublic char setIndex(int position) {this.index = position;return textChars[position];}@Overridepublic int getBeginIndex() {return 0;}@Overridepublic int getEndIndex() {return this.textChars.length;}@Overridepublic int getIndex() {return this.index;}@Overridepublic Object clone() {return new String(textChars);} }
  • 中文亂碼問題分析與解決依據(jù)
    • 亂碼原因:
      通過查看zxing的Encoder類可以發(fā)現(xiàn)默認(rèn)編碼方式是ISO-8859-1。
    • 通過新建QrCodeAttributedCharacter類修復(fù)亂碼問題原理.
      進(jìn)入Graphics2D的drawString(AttributedCharacterIterator iterator, int x, int y)方法找到其讀取字符串內(nèi)容邏輯如下:


      從圖中可以看出其是通過調(diào)用AttributedCharacterIterator的實(shí)現(xiàn)類的方法來(lái)獲取字符串內(nèi)容的,因此可以通過實(shí)現(xiàn)AttributedCharacterIterator接口來(lái)控制獲取的字符串內(nèi)容。
  • 測(cè)試

  • 測(cè)試代碼:
  • public static void main(String[] args) throws Exception {String qrUrl = "https://blog.csdn.net/qq_41633199";String logoUrl = "http://www.finac.site/logo.png";String outputPath = "B:\\logo.jpg";drawQrCode(qrUrl, logoUrl, outputPath, "博文地址", 400, 400);}
  • 測(cè)試結(jié)果:
  • 總結(jié)

    以上是生活随笔為你收集整理的java制作带有logo的二维码,解决zxing中文乱码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。