如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                在本文中,我們將研究如何使用Zxing QR代碼生成庫和JFreeSVG庫在Java中創建QR Code SVG圖像。
QR碼生成
下面的代碼使用Zxing庫創建一個表示QR Code的java.awt.image.BufferedImage對象:
public static BufferedImage getQRCode(String targetUrl, int width, int height) { try { Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = Hashtable<>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = Hashtable<>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(targetUrl, BarcodeFormat.QR_CODE, width, height, hintMap); QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(targetUrl, BarcodeFormat.QR_CODE, width, height, hintMap); int CrunchifyWidth = byteMatrix.getWidth(); BufferedImage image = CrunchifyWidth = byteMatrix.getWidth(); BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect( BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect( BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect( 0 , 0 , CrunchifyWidth, CrunchifyWidth); graphics.setColor(Color.BLACK); , CrunchifyWidth, CrunchifyWidth); graphics.setColor(Color.BLACK); for ( int i = 0 ; i < CrunchifyWidth; i++) { for ( int j = 0 ; j < CrunchifyWidth; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i, j, 1 , 1 ); } } } ); } } } return image; } image; } catch (WriterException e) { e.printStackTrace(); throw new RuntimeException( "Error getting QR Code" ); } } ); } }轉換為SVG
下面的代碼片段使用JFreeSVG將java.awt.image.BufferedImage對象轉換為SVG:
public static String getQRCodeSvg(String targetUrl, int width, int height, boolean withViewBox){ SVGGraphics2D g2 = new SVGGraphics2D(width, height); BufferedImage qrCodeImage = getQRCode(targetUrl, width, height); g2.drawImage(qrCodeImage, SVGGraphics2D(width, height); BufferedImage qrCodeImage = getQRCode(targetUrl, width, height); g2.drawImage(qrCodeImage, SVGGraphics2D(width, height); BufferedImage qrCodeImage = getQRCode(targetUrl, width, height); g2.drawImage(qrCodeImage, 0 , 0 , width, height, null ); ViewBox viewBox = ); ViewBox viewBox = null ; if ( withViewBox ){ viewBox = new ViewBox( 0 , 0 ,width,height); } ,width,height); } return g2.getSVGElement( null , true , viewBox, null , null ); } ); }完整的代碼可以在這里找到。
翻譯自: https://www.javacodegeeks.com/2019/04/create-code-svg-using-zxing-jfreesvg-java.html
總結
以上是生活随笔為你收集整理的如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 功能Java示例 第7部分–将失败也视为
- 下一篇: 莒县备案制教师(莒县备案制)
