国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java基于zxing生成二維碼矩陣過程解析

瀏覽:4日期:2022-08-23 15:53:55

這個例子需要使用google的開源項目zxing的核心jar包

core-3.2.0.jar

可以百度搜索下載jar文件,也可使用maven添加依賴

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

下面是將生成的二維碼矩陣寫入到jpg文件中。

* 生成二維碼圖片 * @param dir 存放的目錄 * @param fileName 文件名要以.jpg結尾 * @param content 這個內容可以是文字或鏈接 */ public static void generateQRCode(String dir, String fileName, String content) { //生成二維碼的寬高 int size = 400; Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); // 指定糾錯等級 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定編碼格式 hints.put(EncodeHintType.CHARACTER_SET, 'UTF-8'); // 指定二維碼的邊距,設置后無效,,設置糾錯等級ErrorCorrectionLevel.H為高等級時,無效 //hints.put(EncodeHintType.MARGIN, 1); try { //encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints); //bitMatrix = updateBit(bitMatrix, 20); File file1 = new File(dir); if (!file1.exists()) {file1.mkdirs(); } //將生成的矩陣像素寫入到指定文件中,這里是以jpg結尾 MatrixToImageWriter.writeToStream(bitMatrix, 'jpg', new FileOutputStream(dir + '/' + fileName)); System.out.println('創建成功'); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (WriterException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

上面指定了糾錯等級設置有四個值

/** L = ~7% correction */ L(0x01), /** M = ~15% correction */ M(0x00), /** Q = ~25% correction */ Q(0x03), /** H = ~30% correction */ H(0x02);

指定為L,M這兩個等級時,二維碼大小會根據其存儲的數據量變化,即邊距肯能會很大,看下圖,

Q,H高等級時,會按照標準格式顯示二維碼圖片。建議使用H等級。

這里生成的二維碼留的白色邊距有點多,想要適當減小邊距,看下圖

Java基于zxing生成二維碼矩陣過程解析

如果不想邊距太大,我們可以將生成的二維碼圖片進行剪切。新建立一個空的BitMatrix對象來放這個二維碼

margin為白色邊距的大小

private static BitMatrix updateBit(BitMatrix matrix, int margin) { int tempM = margin * 2; //left,top,width,height // 0 1 2 3 對應的數組下標 //這里的width和height是指去除白色邊框后的真實的二維碼長寬,而不是圖片長寬。 int[] rec = matrix.getEnclosingRectangle(); // 獲取二維碼圖案的屬性 int resWidth = rec[2] + tempM;//真實寬度加左右邊距 int resHeight = rec[3] + tempM; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定義邊框生成新的BitMatrix resMatrix.clear(); //從上->下按列進行值得復制,即一列一列的掃描到新的二維矩陣中 for (int i = margin; i < resWidth - margin; i++) { // 循環,將二維碼圖案繪制到新的bitMatrix中 for (int j = margin; j < resHeight - margin; j++) {//margin + rec[0]if (matrix.get(i - margin + rec[0], j - margin + rec[1])) { resMatrix.set(i, j);} } } return resMatrix; }

生成二維碼

Java基于zxing生成二維碼矩陣過程解析

這樣白色邊距就不會太大了,好看多了

后面還有將二維碼嵌入到海報,或者其他活動圖片上的方法,直接上代碼

將二維碼放置在圖片右下角的位置

public void insertQRCode(BufferedImage zxingImage, String backgroundPath) { InputStream dest = null; try { dest = new FileInputStream(backgroundPath); BufferedImage image = ImageIO.read(dest); Graphics g = image.getGraphics(); int leftMargin = image.getWidth() - zxingImage.getWidth() - 10; int topMargin = image.getHeight() - zxingImage.getHeight() - 10; g.drawImage(zxingImage, leftMargin, topMargin, zxingImage.getWidth(), zxingImage.getHeight(), null); ImageIO.write(image, 'jpg', new FileOutputStream('D:QRCodezengmei.jpg')); System.out.println('創建成功'); } catch (IOException e) { e.printStackTrace(); } }

生成后的結果,圖片是本地隨便找了一張圖片

Java基于zxing生成二維碼矩陣過程解析

修改二維碼線條顏色,在二維碼中插入logo圖標等方法

發現修改二維碼顏色之后,用微信,qq掃描二維碼很難被識別。這個很難受。這里說下怎么改。

修改原理就是,將內容通過new MultiFormatWriter().encode()方法生成二維矩陣后,,

用一個新的BufferedImage對象作為容器給矩陣的兩個不同的值設置顏色,有值得為true,沒值false,即設置黑白兩種顏色

/** * * @param onColor 二維碼的顏色,即黑白二維碼的黑色 :0xFF000000 藍色 0xFF000055 * @param offColor 二維碼的背景色 如白色:0xFFFFFFFF */ public static void generateOtherQRCode(int onColor, int offColor) { String content = '小姐姐最棒啦^_^'; int size = 200; Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); // 指定糾錯等級 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); // 指定編碼格式 hints.put(EncodeHintType.CHARACTER_SET, 'UTF-8'); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig(onColor, offColor)); ImageIO.write(image, 'png', new FileOutputStream('D:/QRCode/beautiful.png')); System.out.println('操作成功'); } catch (IOException e) { e.printStackTrace(); } catch (WriterException e) { e.printStackTrace(); } }

重要方法是:MatrixToImageWriter.toBufferedImage

也就是設置顏色,然后返回BufferImage對象

public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel()); int onColor = config.getPixelOnColor(); int offColor = config.getPixelOffColor(); 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; }

//imageType , zxing支持的圖像類型有三種,黑白顏色的默認為BufferedImage.TYPE_BYTE_BINARY = 12,圖像不帶透明度alpha 最多是4bit的的圖像TYPE_INT_RGB 這個是不帶alpha的8bit圖像TYPE_INT_ARGB 這個帶alpha的8bit圖像java.awt.image.BufferedImage.BufferedImage(int width, int height, int imageType)

開源項目地址

https://github.com/zxing/zxing

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲天堂男人在线 | 麻豆理论片 | 久久久久久综合七次郎 | 在线观看的毛片 | 中国a毛片 | 三级免费毛片 | 手机看片1024久久精品你懂的 | 男人女人真曰批视频播放 | 杨幂精品国产专区91在线 | 免费一级美国片在线观看 | 欧美成人全部视频 | 国产日产久久高清欧美一区 | 精品国产乱码久久久久久一区二区 | 性高湖久久久久久久久 | www夜色| 色视频在线免费 | 国产色a在线观看 | 伊人久久国产免费观看视频 | 亚洲美女在线观看亚洲美女 | 美女张开腿黄网站免费国产 | av大片| 三级黄色免费看 | 综合色久七七综合七七蜜芽 | 国产精品欧美一区二区在线看 | 免费看一级欧美毛片 | 欧美成人精品大片免费流量 | 久久爱91| 真人毛片 | 国产欧美日韩精品第三区 | 欧美激情中文字幕 | 又黄又刺激下面流水的视频 | 日韩一区二区三区在线免费观看 | 91精品国产高清久久久久久io | 日本午夜小视频 | 日韩中文字幕免费观看 | 男人和女人的做刺激性视频 | 成人a视频片在线观看免费 成人a视频在线观看 | 国产美女视频网站 | 青草欧美 | 国产在线视频一区二区三区 | 欧美在线视频精品 |