java二维码代码 java实现二维码

Java如何用代码生成二维码引用spire.barcode.jar包
//创建BarcodeSettings对象
BarcodeSettings settings = new BarcodeSettings();
//设置条码类型为
QR二维码settings.setType(BarCodeType.QR_Code);
//设置二维码数据
settings.setData("Hello 123456789");
//设置二维码显示数据
settings.setData2D("Hello 123456789");
//设置数据类型
settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);
//设置二维码模型宽度
settings.setX(1.0f);
//设置二维码纠错级别settings.setQRCodeECL(QRCodeECL.H);
//创建BarCodeGenerator实例
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
//根据settings生成图像数据,保存至BufferedImage
BufferedImage bufferedImage = barCodeGenerator.generateImage();
//将图片数据保存为PNG格式
ImageIO.write(bufferedImage, "png", new File("QRCode.png"));
怎么使用java生成DataMatrix格式的二维码?参考:
import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class CreateDataMatrix {
public static void main(String[] args) throws Exception {
//生成BarcodeSettings实例
BarcodeSettings settings = new BarcodeSettings();
//设置条形码类型为DataMatrix
settings.setType(BarCodeType.Data_Matrix);
//设置条形码模型宽度
settings.setX(1.5f);
//设置数据和显示文本
settings.setData("ABC 123456789ABC 123456789ABC 123456789");
settings.setData2D("ABC 123456789ABC 123456789ABC 123456789");
//创建BarCodeGenerator实例
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
//根据settings生成图像数据,保存至BufferedImage实例
BufferedImage bufferedImage = barCodeGenerator.generateImage();
//保存为PNG图片
ImageIO.write(bufferedImage, "png", new File("DataMatrix.png"));
System.out.println("Complete!");
}
}
用到了spire.barcode for java库
java中怎样用代码生成二维码?参考代码
import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class QRCodeEncoderTest
{
/** Creates a new instance of QRCodeEncoderTest */
public QRCodeEncoderTest()
{
}
public static void create_image(String sms_info)throws Exception{
try{
qrcode testQrcode =new qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
String testString = sms_info;
byte[] d = testString.getBytes("gbk");
System.out.println(d.length);
//BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_INT_RGB);
BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 98, 98);
g.setColor(Color.BLACK);
// 限制最大字节数为120
if (d.length0d.length 120){
boolean[][] s = testQrcode.calQrcode(d);
for (int i=0;is.length;i++){
for (int j=0;js.length;j++){
if (s[j][i]) {
g.fillRect(j*2+3,i*2+3,2,2);
}
}
}
}
g.dispose();
bi.flush();
File f = new File("D:\\QRCodeTest\\"+sms_info+".jpg");
if(!f.exists()){
f.createNewFile();
}
//创建图片
ImageIO.write(bi, "jpg", f);
} // end try
catch (Exception e) {
e.printStackTrace();
} // end catch
}
public static void main(String[] args) throws Exception {
System.out.println(new Date());

推荐阅读