zxing 生成二维码

/**
 * 
 */
package ceshi;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;


import javax.imageio.ImageIO;


import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;


/**
 * @Filename: test.java
 * @Version: 1.0
 * @Author: 王超
 * @Email: 
 */
public class test {
    
      //生成二维码
    public static void shengcheng () throws WriterException, IOException{
        String filePath= "D://";//生成图像的路径
        String fileName = "zxing.png";
        
        JSONObject json = new JSONObject();
        json.put("zxing", "nihao");
        json.put("author", "wangchao");
        //内容
        String content = json.toJSONString();
        int width = 200;
        int height=200;
        String format = "png"; //类型
        
        Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>(); 
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
        //生成矩阵
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
        Path path = FileSystems.getDefault().getPath(filePath, fileName);
        MatrixToImageWriter.writeToFile(bitMatrix, format, new File(""+path));
        System.out.println("输出成功");
    }
    
    public static void jiexi() throws IOException, NotFoundException{
        String filePath = "D://zxing.png";
        BufferedImage image;
        image = ImageIO.read(new File(filePath));
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap  = new BinaryBitmap(binarizer);
        Map<DecodeHintType,Object> hints = new HashMap<DecodeHintType,Object>();
        hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
        //对图像进行解析
        Result result  = new MultiFormatReader().decode(binaryBitmap,hints);
        JSONObject  context = JSONObject.parseObject(result.getText());
        System.out.println("图片的内容是:");
        System.out.println("author:" + context.getString("author"));
        System.out.println("zxing:" + context.getString("zxing"));
        System.out.println("图片的格式:");
        System.out.println("encode" + result.getBarcodeFormat());
        
    }
    public static void main(String[] args) throws WriterException, IOException, NotFoundException{
        
        jiexi();
    }

}


文章出处:http://kesun-shy.iteye.com/blog/2154169
















































































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值