如何在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

下面的代码片段使用JFreeSVGjava.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值