QRCode二维码与PDF417码生成与读取JAVA+HTML
生活随笔
收集整理的這篇文章主要介紹了
QRCode二维码与PDF417码生成与读取JAVA+HTML
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
二維碼
import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageConfig; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map;public class QRCodeTest {public static void main(String[] args) {try {String s = "202101051020295,http://www.abc.com";String[] datas = s.split(",");for (String data: datas ) {QREncode(data,"D:\\新建文件夾\\",data,"gif");QRReader(new File("D:\\新建文件夾\\"+data+".gif"));}} catch (IOException e) {e.printStackTrace();} catch (NotFoundException e) {e.printStackTrace();}}/*** 生成二維碼* @param content 二維碼內容* @param path 生成的路徑* @param filename 生成的圖像文件名* @param format 生成的圖像文件格式* @throws WriterException* @throws IOException*/public static void QREncode(String content,String path,String filename,String format) throws WriterException, IOException { // String content = "個人博客:https://www.cnblogs.com/huanzi-qch/";//二維碼內容int width = 300; // 圖像寬度int height = 300; // 圖像高度 // String format = "gif";// 圖像類型Map<EncodeHintType, Object> hints = new HashMap<>();//內容編碼格式hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 指定糾錯等級hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//設置二維碼邊的空度,非負數hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);String fullFileName = path.concat(filename).concat(".").concat(format);MatrixToImageWriter.writeToPath(bitMatrix, format, new File(fullFileName).toPath());// 輸出原圖片MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);/*問題:生成二維碼正常,生成帶logo的二維碼logo變成黑白原因:MatrixToImageConfig默認黑白,需要設置BLACK、WHITE解決:https://ququjioulai.iteye.com/blog/2254382*/BufferedImage bufferedImage = LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix,matrixToImageConfig), new File("D:\\logo.png"));// BufferedImage bufferedImage = LogoMatrix(toBufferedImage(bitMatrix), new File("D:\\logo.png"));String fullFileName1 = path.concat(filename).concat("1").concat(".").concat(format);ImageIO.write(bufferedImage, format, new File(fullFileName1));//輸出帶logo圖片System.out.println("輸出成功.");}/*** 識別二維碼*/public static void QRReader(File file) throws IOException, NotFoundException {MultiFormatReader formatReader = new MultiFormatReader();//讀取指定的二維碼文件BufferedImage bufferedImage =ImageIO.read(file);BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));//定義二維碼參數Map hints= new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");com.google.zxing.Result result = formatReader.decode(binaryBitmap, hints);//輸出相關的二維碼信息System.out.println("解析結果:"+result.toString());System.out.println("二維碼格式類型:"+result.getBarcodeFormat());System.out.println("二維碼文本內容:"+result.getText());bufferedImage.flush();}/*** 二維碼添加logo* @param matrixImage 源二維碼圖片* @param logoFile logo圖片* @return 返回帶有logo的二維碼圖片* 參考:https://blog.csdn.net/weixin_39494923/article/details/79058799*/public static BufferedImage LogoMatrix(BufferedImage matrixImage, File logoFile) throws IOException {/*** 讀取二維碼圖片,并構建繪圖對象*/Graphics2D g2 = matrixImage.createGraphics();int matrixWidth = matrixImage.getWidth();int matrixHeigh = matrixImage.getHeight();/*** 讀取Logo圖片*/BufferedImage logo = ImageIO.read(logoFile);//開始繪制圖片g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//繪制BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);g2.setStroke(stroke);// 設置筆畫對象//指定弧度的圓角矩形RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);g2.setColor(Color.white);g2.draw(round);// 繪制圓弧矩形//設置logo 有一道灰色邊框BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);g2.setStroke(stroke2);// 設置筆畫對象RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);g2.setColor(new Color(128,128,128));g2.draw(round2);// 繪制圓弧矩形g2.dispose();matrixImage.flush() ;return matrixImage ;} } <!--二維碼--><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.2.1</version></dependency><!-- ZXing --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version><!--3.2.1--></dependency> BarPdf417AsBase64<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.14</version></dependency><dependency><groupId>com.lowagie</groupId><artifactId>iText</artifactId><version>2.1.5</version></dependency> package com.doing.utils;import org.apache.commons.codec.binary.Base64;import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.nio.charset.StandardCharsets;public class BarcodePDF417Utils {/*** @Author Mr伍* @Description //TODO * @Date 2021/3/4* @Param [codeString] 字符串* @return java.lang.String 返回base64字符串**/public static String createPdf417AsBase64(String codeString){com.lowagie.text.pdf.BarcodePDF417 pdf = new com.lowagie.text.pdf.BarcodePDF417();pdf.setText(codeString.getBytes(StandardCharsets.UTF_8));Image pdfImg = pdf.createAwtImage(Color.black, Color.white);BufferedImage img = new BufferedImage((int)pdfImg.getWidth(null), (int)pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB);Graphics g = img.getGraphics();g.drawImage(pdfImg, 0, 0, Color.white, null);ByteArrayOutputStream bos = new ByteArrayOutputStream(); // ImageIO.write(img, "PNG", bos);try {ImageIO.write(img, "BMP", bos);} catch (IOException e) {e.printStackTrace();}Base64 base64 = new Base64();String s = base64.encodeToString(bos.toByteArray());return s;} } 二維碼 PDF417碼前端HTML生成二維碼帶log(兼容所有游覽器)
<!DOCTYPE html> <html><head><meta charset="utf-8" /><title></title></head><body><script type="text/javascript" src="js/jquery-1.11.1.js" ></script><script type="text/javascript" src="js/jquery.qrcode.js" ></script><script type="text/javascript" src="js/qrcode.js" ></script> <script type="text/javascript" src="js/utf.js" ></script><p>Render in table</p><div id="qrcodeTable"></div><p>Render in canvas</p><div id="qrcodeCanvas"></div><script> jQuery('#qrcodeTable').qrcode({render : "table", <!--二維碼生成方式 -->text : "http://www.baidu.com" , <!-- 二維碼內容 -->width : "200", //二維碼的寬度height : "200",}); jQuery('#qrcodeCanvas').qrcode({render : "canvas",text : "https://u.wechat.com/MInSjZ0QLtR0PTfqq3D5iO0",width : "200", //二維碼的寬度height : "200", //二維碼的高度background : "#ffffff", //二維碼的后景色foreground : "#000000", //二維碼的前景色src: 'img/1.jpg' //二維碼中間的圖片}); </script></body> </html>jquery.qrcode.js
鏈接:https://pan.baidu.com/s/1rqDyDC1ttJjdun2_6ckWWw 提取碼:j45ujquery-1.11.1.js
鏈接: https://pan.baidu.com/s/1I2Xa7TTNDl6l5NRdVqwVNA 提取碼: itbq 復制這段內容后打開百度網盤手機App,操作更方便哦qrcode.js
鏈接:https://pan.baidu.com/s/1kgIgsTG1Duz0LGfWtb1Rcw 提取碼:9c7uutf.js
鏈接:https://pan.baidu.com/s/1rBxd8FnH0uF354OMFW0EtA 提取碼:q8iv總結
以上是生活随笔為你收集整理的QRCode二维码与PDF417码生成与读取JAVA+HTML的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php执行npm命令_npm系列之命令执
- 下一篇: JAVA读取2g数据的速度_Java 读