import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* Created with IntelliJ IDEA.
* Date: 10/9/13
* Time: 11:31 AM
*/
public class QRCodeTest {
private static int DEFAULT_WIDTH;
private static int UNIT_WIDTH = 10;
public static void main(String args[]) throws Exception{
createImg();
}
public static void createImg(){
Qrcode qrcode=new Qrcode();
//错误修正容量
//L水平 7%的字码可被修正
//M水平 15%的字码可被修正
//Q水平 25%的字码可被修正
//H水平 30%的字码可被修正
//QR码有容错能力,QR码图形如果有破损,仍然可以被机器读取内容,最高可以到7%~30%面积破损仍可被读取。
//相对而言,容错率愈高,QR码图形面积愈大。所以一般折衷使用15%容错能力。
qrcode.setQrcodeErrorCorrect('M');/* L','M','Q','H' */
qrcode.setQrcodeEncodeMode('B');/* "N","A" or other */
qrcode.setQrcodeVersion(3);/* 0-20 */
String testString = "5677777ghjjjjj";
byte[] buff = null;
try {
buff = testString.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
boolean[][] bRect = qrcode.calQrcode(buff);
DEFAULT_WIDTH = bRect.length * UNIT_WIDTH;
BufferedImage bi = new BufferedImage(DEFAULT_WIDTH, DEFAULT_WIDTH, BufferedImage.TYPE_INT_RGB);
// int unitWidth = DEFAULT_WIDTH / bRect.length;
// createGraphics
Graphics2D g = bi.createGraphics();
// set background
g.setBackground(Color.WHITE);
g.clearRect(0, 0, DEFAULT_WIDTH, DEFAULT_WIDTH);
g.setColor(Color.BLACK);
if (buff.length>0 && buff.length <123){
for (int i=0;i<bRect.length;i++){
for (int j=0;j<bRect.length;j++){
if (bRect[j][i]) {
g.fillRect(j*UNIT_WIDTH, i*UNIT_WIDTH, UNIT_WIDTH-1, UNIT_WIDTH-1);
}
}
}
}
g.dispose();
bi.flush();
String FilePath="QRCode.png";
File f = new File(FilePath);
try {
ImageIO.write(bi, "png", f);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Create QRCode finished!");
}
}
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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class QRCodeEvents {
public static void main(String []args)throws Exception{
String text = "你好";
int width = 100;
int height = 100;
String format = "png";
Hashtable hints= new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);
File outputFile = new File("new.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有