温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用 zxing生成二维码图片

发布时间:2020-07-10 15:55:11 来源:网络 阅读:390 作者:阿山哥258 栏目:开发技术

package com.cyberway.Encoder.util;



import java.io.File;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Hashtable;


import javax.p_w_picpathio.ImageIO;

import javax.p_w_picpathio.stream.ImageOutputStream;


import java.awt.p_w_picpath.BufferedImage;


import com.google.zxing.BarcodeFormat;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.WriterException;

import com.google.zxing.common.BitMatrix;

import com.jcraft.jsch.jce.Random;



/**

 * 

 * @author lijinshan

 * @createTime 2015年10月26日  下午4:10:30

 */

public class EncoderHandler{


   

  private static final int BLACK = 0xFF000000; 

  private static final int WHITE = 0xFFFFFFFF; 

   

  private EncoderHandler() {} 

     

  public static BufferedImage toBufferedImage(BitMatrix matrix) { 

    int width = matrix.getWidth(); 

    int height = matrix.getHeight(); 

    BufferedImage p_w_picpath = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

    for (int x = 0; x < width; x++) { 

      for (int y = 0; y < height; y++) { 

        p_w_picpath.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); 

      } 

    } 

    return p_w_picpath; 

  } 

   

     

  public static void writeToFile(BitMatrix matrix, String format, File file) 

      throws IOException { 

    BufferedImage p_w_picpath = toBufferedImage(matrix); 

    if (!ImageIO.write(p_w_picpath, format, file)) { 

      throw new IOException("Could not write an p_w_picpath of format " + format + " to " + file); 

    } 

  } 

   

     

  public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) 

      throws IOException { 

    BufferedImage p_w_picpath = toBufferedImage(matrix); 

    if (!ImageIO.write(p_w_picpath, format, stream)) { 

      throw new IOException("Could not write an p_w_picpath of format " + format); 

    } 

  } 

public static void main(String[] args) throws WriterException, IOException {

String text = "单号:2030011150614143706938   收件人:小李";  

int width = 300; 

       int height = 300; 

       //二维码的图片格式 

       String format = "jpg"; 

       Hashtable hints = new Hashtable(); 

       //内容所使用编码 

       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 

       BitMatrix bitMatrix = new MultiFormatWriter().encode(text, 

               BarcodeFormat.QR_CODE, width, height, hints); 

       //生成二维码 

 

       File outputFile = new File("f:"+File.separator+"lijinshan.jpg"); 

       EncoderHandler.writeToFile(bitMatrix, format, outputFile); 

  

}

}


附件:http://down.51cto.com/data/2366260
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI