二维码生成与扫描

/*需要导入以下jar包
*QRCode.jar、Qrcodeen.jar
*/ 
//生成二维码
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class QRCodeEncoderTest {
	public QRCodeEncoderTest() throws Exception{
		Qrcode qrcode = new Qrcode();
		qrcode.setQrcodeErrorCorrect('M');
		qrcode.setQrcodeEncodeMode('B');
		qrcode.setQrcodeVersion(7);
		String encodeddata = "{id:10022,name:wandern}";
		byte[] d = encodeddata.getBytes("utf-8");
		BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
		// createGraphics
		Graphics2D g = bi.createGraphics();
		// set background
		g.setBackground(Color.WHITE);
		g.clearRect(0, 0, 139, 139);
		g.setColor(Color.BLACK);
		if (d.length > 0 && d.length < 123) {
			boolean[][] b = qrcode.calQrcode(d);
			for (int i = 0; i < b.length; i++) {
				for (int j = 0; j < b.length; j++) {
					if (b[j][i]) {
						g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
					}
				}
			}
		}
		g.dispose();
		bi.flush();
		String FilePath = "F:\\QRCode.png";
		File f = new File(FilePath);
		ImageIO.write(bi, "png", f);
		System.out.println("Input Encoded data is:"+encodeddata);
	}
	//测试
	public static void main(String[] args) throws Exception {
		QRCodeEncoderTest qRCodeEncoderTest=new QRCodeEncoderTest();
	}
}
//二维码扫描
 import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
public class QRCodeDecoderTest {
	public QRCodeDecoderTest() {
		QRCodeDecoder decoder = new QRCodeDecoder();
		File imageFile = new File("F:\\QRCode.png");  //生成的二位码图片
		BufferedImage image = null;
		try {
			image = ImageIO.read(imageFile);
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
		}
		try {
			String decodedData = new String(decoder.decode(new J2SEImage(image)), "utf-8");
			System.out.println("Output Decoded Data is:"+decodedData);
		} catch (DecodingFailedException dfe) {
			System.out.println("Error: " + dfe.getMessage());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
        //测试 
	public static void main(String[] args) {
		QRCodeDecoderTest cRCodeDecoderTest=new QRCodeDecoderTest();
	}
}
class J2SEImage implements QRCodeImage {
	BufferedImage image;
	public J2SEImage(BufferedImage image) {
		this.image = image;
	}
	public int getWidth() {
		return image.getWidth();
	}
	public int getHeight() {
		return image.getHeight();
	}
	public int getPixel(int x, int y) {
		return image.getRGB(x, y);
	}
}



转载于:https://my.oschina.net/u/1413786/blog/180447

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值