免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java實(shí)現(xiàn)二維碼的生成和解析:QRCode、zxing 兩種方式

第一種:QRCode.jar,使用QRCode生成和解析二維碼

1.導(dǎo)入jar包

?

?2.代碼

(1)QRCodeUtil .java

  1 import com.swetake.util.Qrcode;  2 import jp.sourceforge.qrcode.QRCodeDecoder;  3   4 import javax.imageio.ImageIO;  5 import java.awt.*;  6 import java.awt.image.BufferedImage;  7 import java.io.File;  8   9 public class QRCodeUtil { 10     /** 11      *加密:  文字信息 ->二維碼.png 12      * @param content 文字信息 13      * @param imgPath 二維碼路徑 14      * @param imgType 二維碼類型:png 15      * @param size    二維碼尺寸 16      * @throws Exception 17      */ 18     public void encoderQRCode(String content,String imgPath,String imgType,int size)  throws Exception{ 19         //BufferedImage :內(nèi)存中的一張圖片 20         BufferedImage bufImg =   qRcodeCommon(content,imgType,size); 21  22         File file = new File(imgPath);// "src/二維碼.png" --> 二維碼.png 23  24         //生成圖片 25         ImageIO.write(bufImg, imgType, file); 26     } 27  28     /** 29      *產(chǎn)生一個二維碼的BufferedImage 30      * @param content 二維碼隱藏信息 31      * @param imgType 圖片格式 32      * @param size    圖片大小 33      * @return 34      */ 35     private BufferedImage qRcodeCommon(String content, String imgType, int size) throws Exception { 36         BufferedImage bufImg =null; 37         //Qrcode對象:字符串->boolean[][] 38         Qrcode qrCodeHandler = new Qrcode(); 39         //設(shè)置二維碼的排錯率:7% L<M<Q<H30%  :排錯率越高,可存儲的信息越少;但是對二維碼清晰對要求越小 40         qrCodeHandler.setQrcodeErrorCorrect('M'); 41         //可存放的信息類型:N:數(shù)字、  A:數(shù)字 A-Z  B:所有 42         qrCodeHandler.setQrcodeEncodeMode('B'); 43         //尺寸:取值范圍:1-40 44         qrCodeHandler.setQrcodeVersion(size); 45  46         //"Hello world" -> byte[]"Hello world" -->boolean[][] 47         byte[] contentBytes = content.getBytes("UTF-8"); 48         boolean[][] codeOut = qrCodeHandler.calQrcode(contentBytes); 49  50         int imgSize =  67   12*(size -1) ; 51  52         //BufferedImage:內(nèi)存中的圖片 53         bufImg = new BufferedImage(imgSize,imgSize,BufferedImage.TYPE_INT_RGB );//red green blue 54  55         //創(chuàng)建一個畫板 56         Graphics2D gs = bufImg.createGraphics(); 57         gs.setBackground(Color.WHITE);//將畫板的背景色設(shè)置為白色 58         gs.clearRect( 0,0, imgSize,imgSize); //初始化 59         gs.setColor(Color.BLACK);//設(shè)置 畫板上 圖像的顏色(二維碼的顏色) 60  61         int pixoff = 2 ; 62  63         for(int j=0;j<codeOut.length;j  ) { 64             for(int i=0;i<codeOut.length;i  ) { 65                 if(codeOut[j][i]) { 66                     gs.fillRect(j*3 pixoff , i*3 pixoff, 3, 3); 67                 } 68             } 69         } 70         //增加LOGO 71         //將硬盤中的src/logo.png  加載為一個Image對象 72         Image logo =  ImageIO.read(new File("/src/logo.png")  ) ; 73         int maxHeight = bufImg.getHeight(); 74         int maxWdith = bufImg.getWidth(); 75  76         //在已生成的二維碼上 畫logo 77         gs.drawImage(logo,imgSize/5*2,imgSize/5*2, maxWdith/5,maxHeight/5 ,null) ; 78  79         gs.dispose(); //釋放空間 80         bufImg.flush(); //清理 81         return bufImg ; 82     } 83  84     //解密:  二維碼(圖片路徑) -> 文字信息 85     public String decoderQRCode(String imgPath) throws Exception{ 86  87         //BufferedImage內(nèi)存中的圖片  :硬盤中的imgPath圖片 ->內(nèi)存BufferedImage 88         BufferedImage bufImg =  ImageIO.read( new File(imgPath) ) ; 89         //解密 90         QRCodeDecoder decoder = new QRCodeDecoder() ; 91  92         TwoDimensionCodeImage tdcImage = new TwoDimensionCodeImage(bufImg); 93         byte[] bs = decoder.decode(tdcImage) ;    //bufImg 94         //byte[] -->String 95         String content    = new String(bs,"UTF-8"); 96         return content; 97     } 98  99 100 }

?(2)TwoDimensionCodeImage .java

 1 import jp.sourceforge.qrcode.data.QRCodeImage; 2  3 import java.awt.image.BufferedImage; 4  5 public class TwoDimensionCodeImage implements QRCodeImage { 6     BufferedImage bufImg;  //將圖片加載到內(nèi)存中 7     public TwoDimensionCodeImage(BufferedImage bufImg) { 8         this.bufImg = bufImg; 9     }10     public int getWidth() {11         return bufImg.getWidth();12     }13 14     public int getHeight() {15         return bufImg.getHeight();16     }17 18     public int getPixel(int x, int y) {19         return bufImg.getRGB(x,y);20     }21 }

?(3)Test .java

 1 public class Test { 2     public static void main(String[] args)  throws Exception{ 3         //生成二維碼 4         /* 5          * 生成圖片的路徑        src/二維碼.png 6          * 文字信息、網(wǎng)址信息 :  "helloworld" 7          */ 8         String imgPath = "e:\\二維碼2.png"; 9         String content =  "http://baidu.com";  //掃描二維碼后,網(wǎng)頁跳轉(zhuǎn)10 11         //生成二維碼12         /*13          * 加密:  文字信息 ->二維碼14          * 解密:  二維碼 -> 文字信息15          */16         QRCodeUtil qrUtil = new QRCodeUtil();17         //加密:  文字信息 ->二維碼18         qrUtil.encoderQRCode(content, imgPath, "png", 17);19 20 //           TwoDimensionCode handler = new TwoDimensionCode();21 //           handler.encoderQRCode(content, imgPath, "png", 7);22 23 24 //        //解密:  二維碼 -> 文字信息25         String imgContent = qrUtil.decoderQRCode(imgPath) ;26         System.out.println(imgContent);27 28 29     }30 }

第二種:借助Google提供的ZXing Core工具包

1.maven依賴

 1  <dependency> 2             <groupId>com.google.zxing</groupId> 3             <artifactId>core</artifactId> 4             <version>3.4.0</version> 5         </dependency> 6         <dependency> 7             <groupId>com.google.zxing</groupId> 8             <artifactId>javase</artifactId> 9             <version>3.3.1</version>10         </dependency>

2.代碼

(1)ZXingUtil .java

 1 import com.google.zxing.*; 2 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 3 import com.google.zxing.common.BitMatrix; 4 import com.google.zxing.common.HybridBinarizer; 5 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 6 import jp.sourceforge.qrcode.util.Color; 7  8 import javax.imageio.ImageIO; 9 import java.awt.image.BufferedImage;10 import java.io.File;11 import java.util.HashMap;12 import java.util.Hashtable;13 import java.util.Map;14 15 public class ZXingUtil {16     public static void encodeImg(String imgPath,String format,String content,int width,int height,String logo) throws Exception {17         Hashtable<EncodeHintType,Object > hints = new Hashtable<EncodeHintType,Object>() ;18         //排錯率  L<M<Q<H19         hints.put( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H) ;20         //編碼21         hints.put( EncodeHintType.CHARACTER_SET, "utf-8") ;22         //外邊距:margin23         hints.put( EncodeHintType.MARGIN, 1) ;24         /*25          * content : 需要加密的 文字26          * BarcodeFormat.QR_CODE:要解析的類型(二維碼)27          * hints:加密涉及的一些參數(shù):編碼、排錯率28          */29         BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height, hints);30         //內(nèi)存中的一張圖片:此時需要的圖片 是二維碼-> 需要一個boolean[][] ->BitMatrix31         BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);32 33         for(int x=0;x<width;x  ) {34             for(int y=0;y<height;y  ) {35                 image.setRGB(x, y,     (bitMatrix.get(x,y)? Color.BLACK:Color.WHITE)  );36             }37         }38         //畫logo39         image = LogoUtil.logoMatrix(image, logo) ;40         //string->file41         File file = new File(imgPath);42         //生成圖片43         ImageIO.write(image, format, file);44     }45 46     //解密:二維碼->文字47     public static void decodeImg(File file) throws Exception {48         if(!file.exists()) return ;49         //file->內(nèi)存中的一張圖片50         BufferedImage imge = ImageIO.read(file)  ;51 52         MultiFormatReader formatReader = new MultiFormatReader() ;53         LuminanceSource source = new BufferedImageLuminanceSource(imge);54         Binarizer binarizer = new HybridBinarizer(source);55         BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);56         //圖片 ->result57         Map map = new HashMap();58         map.put(EncodeHintType.CHARACTER_SET, "utf-8") ;59         Result result = formatReader.decode(binaryBitmap  ,map ) ;60         System.out.println("解析結(jié)果:"  result.toString());61     }62 63 }

(2)LogoUtil .java

 1 import javax.imageio.ImageIO; 2 import java.awt.*; 3 import java.awt.geom.RoundRectangle2D; 4 import java.awt.image.BufferedImage; 5 import java.io.File; 6 import java.io.IOException; 7  8 public class LogoUtil { 9     //傳入logo、二維碼 ->帶logo的二維碼10     public  static BufferedImage  logoMatrix( BufferedImage matrixImage,String logo ) throws IOException {11         //在二維碼上畫logo:產(chǎn)生一個  二維碼畫板12         Graphics2D g2 = matrixImage.createGraphics();13 14         //畫logo: String->BufferedImage(內(nèi)存)15         BufferedImage logoImg = ImageIO.read(new File(logo));16         int height = matrixImage.getHeight();17         int width = matrixImage.getWidth();18         //純logo圖片19         g2.drawImage(logoImg, width * 2 / 5, height * 2 / 5, width * 1 / 5, height * 1 / 5, null);20 21         //產(chǎn)生一個 畫 白色圓角正方形的 畫筆22         BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);23         //將畫板-畫筆 關(guān)聯(lián)24         g2.setStroke(stroke);25         //創(chuàng)建一個正方形26         RoundRectangle2D.Float round = new RoundRectangle2D.Float(width * 2 / 5, height * 2 / 5, width * 1 / 5, height * 1 / 5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);27         g2.setColor(Color.WHITE);28         g2.draw(round);29 30         //灰色邊框31         BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);32         g2.setStroke(stroke2);33         //創(chuàng)建一個正方形34         RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width * 2 / 5   2, height * 2 / 5   2, width * 1 / 5 - 4, height * 1 / 5 - 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);35 //        Color color = new Color(128,128,128) ;36         g2.setColor(Color.GRAY);37         g2.draw(round2);38 39         g2.dispose();40         matrixImage.flush();41 42         return matrixImage;43     }44 }

(3)test.java

 1 import java.io.File; 2  3 public class test { 4     public static void main(String[] args) throws Exception { 5         String imgPath = "src/二維碼12.png"; 6         String content = "helloworld你好"; 7         String logo = "src/logo.png"; 8         //加密:文字信息->二維碼 9         ZXingUtil.encodeImg(imgPath, "png",content, 430, 430,logo);10         //解密:二維碼  ->文字信息11         ZXingUtil.decodeImg(new File(imgPath));12     }13 }

(效果圖)

?

來源:https://www.icode9.com/content-1-274601.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
二維碼Java開發(fā)(筆記)
java生成二維碼的三個工具
使用zxing生成QRCode二維碼,在Android的webview中調(diào)用條形碼掃描
關(guān)于二維碼實(shí)驗(yàn)的總結(jié)(前言+小試牛刀)
一步一步教你用java生成二維碼
Google開源項(xiàng)目ZXing(二維條碼編解碼)簡單使用(Java版)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服