基於Java實現二維碼的生成和解析
最近因個人需求需要對根據內容生成二維碼和進行解析!記錄一下!二維碼其實就是一種編碼技術,隻是這種編碼技術是用在圖片上瞭,將給定的一些文字,數字轉換為一張經過特定編碼的圖片。這裡利用的是 google 公司的 zxing使用方便,可以操作條形碼或者二維碼等
導入相關jar包
<!-- 二維碼 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
二維碼工具類編
創建二維碼圖片
public static BufferedImage createImage(String charSet, String content, int qrWidth, int qrHeight) { Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, charSet); hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = null; try { bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, // 修改二維碼底部高度 hints); } catch (WriterException e) { e.printStackTrace(); } int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } return image; }
二維碼設置logo
public static void insertLogoImage(BufferedImage source, Image logo, int logoWidth, int logoHeight) { Graphics2D graph = source.createGraphics(); int qrWidth = source.getWidth(); int qrHeight = source.getHeight(); int x = (qrWidth - logoWidth) / 2; int y = (qrHeight - logoHeight) / 2; graph.drawImage(logo, x, y, logoWidth, logoHeight, null); Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6); graph.setStroke(new BasicStroke(3f)); graph.draw(shape); graph.dispose(); }
將文明說明增加到二維碼上
public static BufferedImage textToImage(String str, int width, int height, int fontSize) { BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) textImage.getGraphics(); //開啟文字抗鋸齒 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setBackground(Color.WHITE); g2.clearRect(0, 0, width, height); g2.setPaint(Color.BLACK); FontRenderContext context = g2.getFontRenderContext(); Font font = new Font("微軟雅黑", Font.PLAIN, fontSize); g2.setFont(font); LineMetrics lineMetrics = font.getLineMetrics(str, context); FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font); float offset = (width - fontMetrics.stringWidth(str)) / 2; float y = (height + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2; g2.drawString(str, (int) offset, (int) y); return textImage; }
解析二維碼
/* * 解析二維碼 */ public static String decode(File file, DecodeHintType cherSet) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, cherSet); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; }
main方法測試類
public static void main(String[] args) { String content="用戶攝影作品版權信息"; BufferedImage image = QRCodeUtil.createImage( "utf-8", content, 400, 400 ); QRCodeUtil.addUpFont( image,"用戶攝影作品版權信息" ); String formatName="png"; String imagePath="D:\temp\java二維碼.png"; File file=new File(imagePath); try { ImageIO.write(image, formatName, file); } catch (IOException e) { e.printStackTrace(); } String decode = null; try { decode = QRCodeUtil.decode(file, DecodeHintType.CHARACTER_SET); } catch (Exception e) { e.printStackTrace(); } System.out.println(decode); System.out.println("執行完成"); }
這樣就實現瞭 !二維碼裡面的內容根據實際數據來就行!用手機掃描後查看。
到此這篇關於基於Java實現二維碼的生成和解析的文章就介紹到這瞭,更多相關Java二維碼內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!