当前位置: 代码迷 >> Web前端 >> 依据内容生产条2维形码
  详细解决方案

依据内容生产条2维形码

热度:180   发布时间:2012-07-01 13:15:00.0
根据内容生产条2维形码
将内容转换为2维条码 ,com.google.zxing   为1.5版本jar包

参考:
[url]
http://kb.cnblogs.com/a/1309789/
http://easymorse.googlecode.com/svn/tags/barcode-demo-0.1/src/main/java/com/easymorse/BarcodeWriterDemo.java
http://marshal.easymorse.com/archives/2791
http://blog.csdn.net/a_b_a_b_a_b_a_b/article/details/6197636
[/url]


import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.ByteMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class BarcodeUtils {
	
	Hashtable<EncodeHintType, String> hints;
	{
		hints = new Hashtable<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
	}

	public Boolean generate(String content,int width,int height,OutputStream outputStream){
		QRCodeWriter writer = new QRCodeWriter();

		try {
			ByteMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE,
					width, height, hints);
			byte[][] matrixByte = matrix.getArray();

			BufferedImage bimg = new BufferedImage(width, height,
					BufferedImage.TYPE_BYTE_GRAY);
			byte[] linearbuffer = new byte[width * height];

			for (int y = 0,i=0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					linearbuffer[i++] = matrixByte[y][x];
				}
			}
			bimg.getRaster().setDataElements(0, 0, width, height, linearbuffer);
			boolean result = ImageIO.write(bimg, "png", outputStream);
			return result;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
		  
	
 Boolean  result=  generate("content",200,200,new FileOutputStream("D:/123.png"));		 
		 
		 
		 
	}


}



  相关解决方案