java实现二维码生成

  1. package com.test;  
  2. import com.google.zxing.BarcodeFormat;  
  3. import com.google.zxing.EncodeHintType;  
  4. import com.google.zxing.MultiFormatWriter;  
  5. import com.google.zxing.common.BitMatrix;  
  6.   
  7. import javax.imageio.ImageIO;  
  8. import java.io.File;  
  9. import java.io.OutputStream;  
  10. import java.io.IOException;  
  11. import java.util.HashMap;  
  12. import java.util.Map;  
  13. import java.awt.image.BufferedImage;  
  14. /** 
  15.  * 需要Google  zxing的core包支持   
  16.  * @author lcx 
  17.  * 
  18.  */  
  19.   
  20. public final class MatrixToImageWriter {  
  21.   
  22.   private static final int BLACK = 0xFF000000;  
  23.   private static final int WHITE = 0xFFFFFFFF;  
  24.   
  25.   private MatrixToImageWriter() {}  
  26.   
  27.     
  28.   public static BufferedImage toBufferedImage(BitMatrix matrix) {  
  29.     int width = matrix.getWidth();  
  30.     int height = matrix.getHeight();  
  31.     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  32.     for (int x = 0; x < width; x++) {  
  33.       for (int y = 0; y < height; y++) {  
  34.         image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);  
  35.       }  
  36.     }  
  37.     return image;  
  38.   }  
  39.   
  40.     
  41.   public static void writeToFile(BitMatrix matrix, String format, File file)  
  42.       throws IOException {  
  43.     BufferedImage image = toBufferedImage(matrix);  
  44.     if (!ImageIO.write(image, format, file)) {  
  45.       throw new IOException("Could not write an image of format " + format + " to " + file);  
  46.     }  
  47.   }  
  48.   
  49.     
  50.   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)  
  51.       throws IOException {  
  52.     BufferedImage image = toBufferedImage(matrix);  
  53.     if (!ImageIO.write(image, format, stream)) {  
  54.       throw new IOException("Could not write an image of format " + format);  
  55.     }  
  56.   }  
  57.   public static void main(String[] args) {  
  58.       try {          
  59.              String content = "http://www.baidu.com/"; //二维码存储数据  
  60.              String path = "f:\\";//               
  61.              MultiFormatWriter multiFormatWriter = new MultiFormatWriter();  
  62.              Map hints = new HashMap();  
  63.              hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
  64.              BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400400,hints);  
  65.              File file1 = new File(path,"2013.jpg");//图片输出路径  
  66.              MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);  
  67.                
  68.          } catch (Exception e) {  
  69.              e.printStackTrace();  
  70.          }  
  71. }  
  72. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值