`
happyqing
  • 浏览: 3152115 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java生成二维码QRCode Zxing SwetakeQRCode barcode4j

阅读更多

 

最常见的就是QRcode二维码

一、Zxing

1.推荐使用Zxing Google出的,大厂商,文档齐全。也可生成条形码,Android上一般也采用Zxing。

2.可生成、读取二维码,可嵌入图片(logo),支持中文

3.编写代码量很少,就可生成二维码

4.生成的二维码的位点少,好识别

5.生成的二维码,占用存储空间小,2K

所需jar包:core-2.2.jar,jdk 1.6。自2.2以后需要jdk1.7,否则会报错

 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/google/zxing/EncodeHintType : Unsupported major.minor version 51.0

所需jar包:core-2.2.jar,附件提供

 

二、SwetakeQRCode

1.下载jar包都费劲,不推荐

2.可生成、读取二维码,可嵌入图片(logo),支持中文

3.编写代码量较多

4.生成的二维码的位点多,

5.生成的二维码,占用存储空间少,1K

所需jar包:QRCode.jar

代码参考:

http://www.oschina.net/code/snippet_1762525_49027

 

三、 barcode4j

datamatrix

 

PDF417

 

采用datamatrix算法,生成的二维码是长方形的,不是我想要的

貌似不支持中文

存储容量较大,8K-24K,因为他的黑点旁边有灰色格线

所需jar包:avalon-framework-4.2.0.jar,barcode4j.jar

代码参考:

http://liuwei1981.iteye.com/blog/368812

有一句有问题

// ret = FileUtil.getInputStreamFromBytes(baos.toByteArray());
   ret = new ByteArrayInputStream(baos.toByteArray());

补充方法

public static void saveFile(InputStream is, String fileName) throws Exception {
	FileOutputStream fos = new FileOutputStream(new File(fileName));
	byte[] b = new byte[1024];
	while((is.read(b)) != -1){
		fos.write(b);
	}
	is.close();
	fos.close();
}

public static void main(String[] args) throws Exception {
	InputStream is = CodeService.getInstance().getCodeImage("Tiantian,I love you!", CodeService.BARCODE_TYPE );
	saveFile(is, "D:/barcode4j.png");
}

 

二维码里嵌图片logo,就是利用二维码容错机制实现的,其实就是在二维码上画的图片,

 

Zxing样例:

参考:http://tec.5lulu.com/detail/110d4n2ehcg9a857d.html#tec_dir1

import java.io.File;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class Main {
	public static void main(String[] args) throws Exception {
		String text = "甜甜,我爱你!"; // 二维码内容
		int width = 300; // 二维码图片宽度
		int height = 300; // 二维码图片高度
		String format = "png";// 二维码的图片格式

		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码

		BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
		// 生成二维码
		File outputFile = new File("d:" + File.separator + "new.png");
		MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
	}
}

 

import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import java.io.OutputStream;  
  
import javax.imageio.ImageIO;  
  
import com.google.zxing.common.BitMatrix;  
  
/** 
 * 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类直接拷贝到源码中使用 
 */  
public class MatrixToImageWriter {  
    private static final int BLACK = 0xFF000000;  
    private static final int WHITE = 0xFFFFFFFF;  
  
    private MatrixToImageWriter() {  
    }  
  
    public static BufferedImage toBufferedImage(BitMatrix matrix) {  
        int width = matrix.getWidth();  
        int height = matrix.getHeight();  
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        for (int x = 0; x < width; x++) {  
            for (int y = 0; y < height; y++) {  
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);  
            }  
        }  
        return image;  
    }  
  
    public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {  
        BufferedImage image = toBufferedImage(matrix);  
        if (!ImageIO.write(image, format, file)) {  
            throw new IOException("Could not write an image of format " + format + " to " + file);  
        }  
    }  
  
    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {  
        BufferedImage image = toBufferedImage(matrix);  
        if (!ImageIO.write(image, format, stream)) {  
            throw new IOException("Could not write an image of format " + format);  
        }  
    }  
}

  

js jquery.qrcode生成二维码 带logo 支持中文

http://happyqing.iteye.com/blog/2294628

 

ZXing生成二维码,以及给二维码添加Logo

http://my.oschina.net/Rayn/blog/215055

Zxing和QR CODE 生成与解析二维码实例(带logo篇)

http://blog.csdn.net/gao36951/article/details/41149049

ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

http://blog.csdn.net/sanfye/article/details/45749139

带圆角LOGO的QrCode二维码实时生成 (这个格式太乱了)

http://www.th7.cn/Program/java/201412/336803.shtml

 

  • 大小: 991 Bytes
  • 大小: 8 KB
  • 大小: 24 KB
  • 大小: 1.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics