zxing生成二维码(outputStream ,File)

可直接生成二维码文件也可以通过outputStream 在点三方对象存储服务器上生成
//生成二维码
public class CreateQRCode {
private static final int BLACK = 0XFF000000;//前景色
private static final int WHITE = 0XFFFFFFFF;//背景色

private static int WIDTH=300;
private static int HEIGHT=300;

/**
 * 
 *
 * @param content 二维码内容
 * @param fileName 图片名称  使用uuid生成
 */
public static Boolean createZxingqrCode(String content,String fileName){
    Map hints=new HashMap();   
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    //设置编码     
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    //设置容错等级      
    hints.put(EncodeHintType.MARGIN, 2);//设置边距默认是5
    Boolean bool = null;
    //生成二维码
    try{
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        BitMatrix bitMatrix = null;
        bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT,hints);
        BufferedImage image = toBufferedImage(bitMatrix);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //生成二维码图片及存放地址
        ImageIO.write(image, "png",new File("D:/ceshi.jpg"));
        
        
        //如果需要通过输出流在七牛平台上生成图片可使用
        //ImageIO.write(image, "png", outputStream);  
        //qiniu(outputStream.toByteArray(), fileName);
    }catch(Exception e){
        return bool;
    }
    return bool;
}
/**
    * 生成二维码内容<br>
     * 
     * @param matrix
     * @return
     */
     public static BufferedImage toBufferedImage(BitMatrix matrix) {
         int width = matrix.getWidth();
        int height = matrix.getHeight();
       BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
         for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
            }
       }
        return image;
     }

}
项目下载地址:http://www.zrscsoft.com/sitepic/12036.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值