二维码生成 java

大家好:

今天研究了一下二维码,果断的,我写不出来,只能拷贝牛人的代码了!不过还是生成了二维码,供大家参考吧!

值得注意的是:乱码问题!未解决!

<span style="font-size:14px;">import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
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.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;


public class ZXingAction {
	 private static final int BLACK = 0xff000000;
	 private static final int WHITE = 0xFFFFFFFF;
	
	 
	 
	 public static void main(String [] args) {
		 ZXingAction test = new ZXingAction();
        File file = new File("F://test.png");
        test.encode("http://www.baidu.com", file, BarcodeFormat.QR_CODE, 200, 200, null);
        test.decode(file);
	 }
	 /**
      * 生成QRCode二维码<br> 
       * 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的<br>
       *  static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";<br>
       *  修改为UTF-8,否则中文编译后解析不了<br>
      */
     public void encode(String contents, File file, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
         try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
             writeToFile(bitMatrix, "png", file);
         } catch (Exception e) {
        e.printStackTrace();
         }
     }
     /**
      * 生成二维码图片<br>
      *            图片格式
      * @param file
      *            生成二维码图片位置
      * @throws IOException
      */
        public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
            BufferedImage image = toBufferedImage(matrix);
            ImageIO.write(image, format, file);
       }
	 /**
	  *生成二维码内容
      * @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;
	     }
	     
	 /**
	 * 解析QRCode二维码
	 */
	 public void decode(File file) {
	    try {
	         BufferedImage image;
	           try {
	                image = ImageIO.read(file);
	              if (image == null) {
	                   System.out.println("Could not decode image");
	           }
	           LuminanceSource source = new <strong><span style="color:#FF0000;">BufferedImageLuminanceSource</span></strong>(image);
	           BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
	           Result result;
	           @SuppressWarnings("rawtypes")
	            Hashtable hints = new Hashtable();
	          //解码设置编码方式为:utf-8
	                hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
	               result = new MultiFormatReader().decode(bitmap, hints);
	                String resultStr = result.getText();
	                System.out.println("解析后内容:" + resultStr);
	            } catch (IOException ioe) {
	            	System.out.println(ioe.toString());
	            } catch (ReaderException re) {
	            System.out.println(re.toString());
	         }
         } catch (Exception ex) {
            System.out.println(ex.toString());
        }
	 }
}</span>

上面的代码粘贴上报错,需要下面的方法

<span style="font-size:14px;">import java.awt.image.BufferedImage;

import com.google.zxing.LuminanceSource;
 
 public final class BufferedImageLuminanceSource extends LuminanceSource {
 
   private final BufferedImage image;
   private final int left;
   private final int top;
 
   public BufferedImageLuminanceSource(BufferedImage image) {
     this(image, 0, 0, image.getWidth(), image.getHeight());
   }
   public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
     super(width, height);
 
     int sourceWidth = image.getWidth();
     int sourceHeight = image.getHeight();
     if (left + width > sourceWidth || top + height > sourceHeight) {
       throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
     }
 
     for (int y = top; y < top + height; y++) {
       for (int x = left; x < left + width; x++) {
         if ((image.getRGB(x, y) & 0xFF000000) == 0) {
           image.setRGB(x, y, 0xFFFFFFFF); // = white
         }
       }
     }
 
     this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
     this.image.getGraphics().drawImage(image, 0, 0, null);
     this.left = left;
     this.top = top;
   }
 
   @Override
   public byte[] getRow(int y, byte[] row) {
     if (y < 0 || y >= getHeight()) {
       throw new IllegalArgumentException("Requested row is outside the image: " + y);
     }
     int width = getWidth();
     if (row == null || row.length < width) {
       row = new byte[width];
     }
     image.getRaster().getDataElements(left, top + y, width, 1, row);
     return row;
   }
 
   @Override
   public byte[] getMatrix() {
     int width = getWidth();
     int height = getHeight();
     int area = width * height;
     byte[] matrix = new byte[area];
     image.getRaster().getDataElements(left, top, width, height, matrix);
     return matrix;
   }
 
 }</span>

百度的二维码

后台输出

解析后内容:http://www.baidu.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值