zoukankan      html  css  js  c++  java
  • zxing生成二维码

    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.3.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.3.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.33</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
     1 import java.awt.image.BufferedImage;
     2 import java.io.ByteArrayOutputStream;
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.nio.file.FileSystems;
     7 import java.nio.file.Path;
     8 import java.util.HashMap;
     9 import java.util.Map;
    10 import java.util.Random;
    11 import java.util.UUID;
    12 
    13 import javax.imageio.ImageIO;
    14 
    15 import org.apache.commons.io.IOUtils;
    16 import org.w3c.dom.UserDataHandler;
    17 
    18 import com.alibaba.fastjson.JSONObject;
    19 
    20 import com.google.zxing.BarcodeFormat;
    21 import com.google.zxing.EncodeHintType;
    22 import com.google.zxing.MultiFormatWriter;
    23 import com.google.zxing.WriterException;
    24 import com.google.zxing.client.j2se.MatrixToImageWriter;
    25 import com.google.zxing.common.BitMatrix;
    26 
    27 public class Hseid {
    28     
    29     
    30     
    31      public void testEncode() throws WriterException, IOException, InterruptedException {  
    32             String filePath = "D://";  
    33             String fileName =UUID.randomUUID()+".png";  
    34 //            JSONObject json = new JSONObject();  
    35 //            json.put("zxing", "https://github.com/zxing/zxing/tree/zxing-3.0.0/javase/src/main/java/com/google/zxing");  
    36 //            json.put("author", "shihy");  
    37 //            String content = json.toJSONString();// 内容  
    38             String content = "www.youku.com";
    39             int width = 200; // 图像宽度  
    40             int height = 200; // 图像高度  
    41             String format = "png";// 图像类型  
    42             Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
    43             hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
    44             
    45             
    46             BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵  
    47             
    48           //  ByteArrayOutputStream out = new ByteArrayOutputStream();  
    49             
    50             //轉換到字節數組方式一
    51            // BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
    52             //boolean flag = ImageIO.write(bufferedImage, "gif", out);  
    53             //byte[] b = out.toByteArray(); 
    54             
    55             //轉換到字節數組方式二
    56            // MatrixToImageWriter.writeToStream(bitMatrix, format, out);
    57           //  byte[] byteArray = out.toByteArray();
    58             
    59             
    60             //字節數組轉換到文件
    61           //  FileOutputStream fileOutputStream = new FileOutputStream("D://11111.png");
    62           //  IOUtils.write(byteArray,fileOutputStream );
    63        
    64             //方案二先上傳再刪除
    65             String usrHome = System.getProperty("user.home");
    66             System.out.println(usrHome);
    67             Path path = FileSystems.getDefault().getPath(usrHome, fileName);  
    68             MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像  
    69             //...這裡完成上傳
    70             Thread.sleep(10000);
    71             
    72             
    73             
    74             File file = path.toFile();  
    75             if(file.exists()&&file.isFile()) {
    76                 
    77                 //再刪除
    78                 file.delete();
    79             }
    80             
    81             
    82             System.out.println("输出成功.");  
    83         }  
    84      
    85      
    86      public static void main(String[] args) throws WriterException, IOException, InterruptedException {
    87         new Hseid().testEncode();
    88     }
    89 
    90 }
  • 相关阅读:
    LeetCode Merge Two Sorted Lists 归并排序
    LeetCode Add Binary 两个二进制数相加
    LeetCode Climbing Stairs 爬楼梯
    034 Search for a Range 搜索范围
    033 Search in Rotated Sorted Array 搜索旋转排序数组
    032 Longest Valid Parentheses 最长有效括号
    031 Next Permutation 下一个排列
    030 Substring with Concatenation of All Words 与所有单词相关联的字串
    029 Divide Two Integers 两数相除
    028 Implement strStr() 实现 strStr()
  • 原文地址:https://www.cnblogs.com/woms/p/7067469.html
Copyright © 2011-2022 走看看