基于Java實(shí)現(xiàn)二維碼的生成和解析
最近因個(gè)人需求需要對(duì)根據(jù)內(nèi)容生成二維碼和進(jìn)行解析!記錄一下!二維碼其實(shí)就是一種編碼技術(shù),只是這種編碼技術(shù)是用在圖片上了,將給定的一些文字,數(shù)字轉(zhuǎn)換為一張經(jīng)過(guò)特定編碼的圖片。這里利用的是 google 公司的 zxing使用方便,可以操作條形碼或者二維碼等
導(dǎo)入相關(guān)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>
二維碼工具類(lèi)編
創(chuàng)建二維碼圖片
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; }
二維碼設(shè)置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(); }
將文明說(shuō)明增加到二維碼上
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(); //開(kāi)啟文字抗鋸齒 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方法測(cè)試類(lèi)
public static void main(String[] args) { String content="用戶攝影作品版權(quán)信息"; BufferedImage image = QRCodeUtil.createImage( "utf-8", content, 400, 400 ); QRCodeUtil.addUpFont( image,"用戶攝影作品版權(quán)信息" ); 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("執(zhí)行完成"); }
這樣就實(shí)現(xiàn)了 !二維碼里面的內(nèi)容根據(jù)實(shí)際數(shù)據(jù)來(lái)就行!用手機(jī)掃描后查看。
到此這篇關(guān)于基于Java實(shí)現(xiàn)二維碼的生成和解析的文章就介紹到這了,更多相關(guān)Java二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java web.xml之contextConfigLocation作用案例詳解
這篇文章主要介紹了Java web.xml之contextConfigLocation作用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08springBoot集成redis的key,value序列化的相關(guān)問(wèn)題
這篇文章主要介紹了springBoot集成redis的key,value序列化的相關(guān)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08packages思維及使用Java添加Android平臺(tái)特定實(shí)現(xiàn)
這篇文章主要為大家介紹了packages思維及使用Java添加Android平臺(tái)特定實(shí)現(xiàn)在Flutter框架里的體現(xiàn)和運(yùn)用詳解,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12JAVA如何自動(dòng)下載SSL證書(shū)并導(dǎo)入到本地
這篇文章主要介紹了JAVA如何自動(dòng)下載SSL證書(shū)并導(dǎo)入到本地問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08解決idea找不到setting.xml文件的問(wèn)題
這篇文章主要介紹了解決idea找不到setting.xml文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02java從命令行獲取數(shù)據(jù)的三種方式代碼實(shí)例
這篇文章主要介紹了java從命令行獲取數(shù)據(jù)的三種方式代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Java輸入數(shù)據(jù)的知識(shí)點(diǎn)整理
在本篇文章里小編給大家整理的是關(guān)于Java如何輸入數(shù)據(jù)的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)參考下。2020-01-01