首页>代码>java二维码qrcode生成代码下载,进行加密,解密,相对于最代码xiaoxiaot的更为简便>/qrcode/src/test/com/linapex/code/TxmWrite.java
package test.com.linapex.code;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.util.ContentConverter;
import com.swetake.util.Qrcode;
//二维条形码
public class TxmWrite {
public static void main(String[] args) throws Exception {
TxmWrite test = new TxmWrite();
test.creatTxm("676317283718啊好的乖哈苏德");
test.readTxm("e:"+File.separator+"QRCode.png");
System.out.println(File.separator+"QRCode.png");
}
/**
* 创建二维条形码
* @param param 比如身份证号码
* @throws Exception
*/
public void creatTxm(String param) throws Exception {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);
byte[] bstr = param.getBytes("UTF-8");
BufferedImage bi = new BufferedImage(139, 139,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE); //背景颜色
g.clearRect(0, 0, 139, 139);
g.setColor(Color.BLACK); //条码颜色
if (bstr.length > 0 && bstr.length < 123) {
boolean[][] b = qrcode.calQrcode(bstr);
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 = "e:"+File.separator+"QRCode.png";
File f = new File(FilePath);
ImageIO.write(bi, "png", f);
}
/**
* 解析二维条形码
* @param path 条形码图片的路径
* @throws Exception
*/
public void readTxm(String path) throws Exception {
QRCodeDecoder decoder = new QRCodeDecoder();
BufferedImage image = null;
if ( path.startsWith("http://")) {
image = ImageIO.read(new URL(path));
} else {
image = ImageIO.read(new File(path));
}
String decodedString = new String(decoder.decode(new J2SEImage(image)),"UTF-8");
decodedString = ContentConverter.convert(decodedString);
System.out.println("条码内容:"+decodedString);
}
}
class J2SEImage implements QRCodeImage {
BufferedImage image;
public J2SEImage(BufferedImage source) {
this.image = source;
}
public int getWidth() {
return image.getWidth();
}
public int getHeight() {
return image.getHeight();
}
public int getPixel(int x, int y) {
return image.getRGB(x, y);
}
}
最近下载更多
binbin0915 LV1
2021年5月27日
17587079094 LV6
2021年4月27日
安东尼online LV11
2021年2月19日
abcd11112 LV6
2020年8月12日
暖颜_o LV10
2020年7月13日
hanweinan6 LV13
2020年6月29日
gshnlj LV15
2020年1月28日
15838634741 LV18
2019年12月1日
shunwei LV7
2019年5月18日
zhangjian01 LV7
2019年4月8日
最近浏览更多
719818732 LV3
2024年3月11日
微信网友_6499216006828032 LV1
2023年10月12日
dzlwindy LV8
2023年7月11日
skytop
2023年5月13日
暂无贡献等级
zzuljh LV9
2023年4月25日
mq13947193109 LV19
2023年3月1日
zw050256 LV7
2022年9月30日
crosa_Don LV18
2022年7月19日
yydsod LV1
2022年6月5日
微信网友_5855482984206336 LV1
2022年3月3日

