当前位置: 首页 > 工具软件 > JFreeSVG > 使用案例 >

zxing qr区域判断_如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?

卢文博
2023-12-01

zxing qr区域判断

在本文中,我们将研究如何使用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

zxing qr区域判断

 类似资料: