zoukankan      html  css  js  c++  java
  • Java--二维码生成&图片和流转化

     1 package test;
     2 
     3 import java.awt.image.BufferedImage;
     4 import java.io.ByteArrayInputStream;
     5 import java.io.ByteArrayOutputStream;
     6 import java.io.File;
     7 import java.util.Hashtable;
     8 
     9 import javax.imageio.ImageIO;
    10 
    11 import com.google.zxing.BarcodeFormat;
    12 import com.google.zxing.EncodeHintType;
    13 import com.google.zxing.MultiFormatWriter;
    14 import com.google.zxing.client.j2se.MatrixToImageWriter;
    15 import com.google.zxing.common.BitMatrix;
    16 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
    17 
    18 public class Test {
    19 
    20     public static void main(String[] args) throws Exception {
    21         String text = "你好";
    22 
    23         int width = 100;
    24         int height = 100;
    25         String format = "png";
    26         Hashtable hints = new Hashtable();
    27         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    28         BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
    29         File outputFile = new File("new.png");
    30         MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
    31 
    32         byte[] b = toByteArray(new File("new.png"));
    33         new Base64();
    34         String s = Base64.encode(b);
    35         // String s = new String(b, "utf-8");
    36         System.out.println(s);
    37         // new Base64();
    38         ByteArrayInputStream in = new ByteArrayInputStream(Base64.decode(s));
    39         BufferedImage image = ImageIO.read(in);
    40         File newFile = new File("new2.png");
    41         ImageIO.write(image, "png", newFile);
    42 
    43     }
    44 
    45     public static byte[] toByteArray(File imageFile) throws Exception {
    46         BufferedImage img = ImageIO.read(imageFile);
    47         ByteArrayOutputStream buf = new ByteArrayOutputStream((int) imageFile.length());
    48         try {
    49             ImageIO.write(img, "jpg", buf);
    50         } catch (Exception e) {
    51             e.printStackTrace();
    52             return null;
    53         }
    54         return buf.toByteArray();
    55     }
    56 
    57 }

  • 相关阅读:
    forEach 不能跳出循环;用some 或者every 代替
    echarts图表不重新渲染
    vue 的el-tree获取选中节点的集合执行多次问题
    vue 2.6版本 手动配置json文件显示隐藏
    echart category series 数据多个 长度不对应 对应的数据一定要用字符串 不要用数字
    nginx前端配置后端
    UCOS多任务下有效的喂狗的方式
    判断数据类型
    PDFJS插件带添加header以及携带授权
    vue中控制浏览器前进和后退
  • 原文地址:https://www.cnblogs.com/microcat/p/6398185.html
Copyright © 2011-2022 走看看