A. java 生成二维码后如何给该二维码添加信息
java可使用zxing生成二维码并为其添加信息。
以下是详细步骤:
1、创建MatrixToImageWriter类
importcom.google.zxing.common.BitMatrix;
importjavax.imageio.ImageIO;
importjava.io.File;
importjava.io.OutputStream;
importjava.io.IOException;
importjava.awt.image.BufferedImage;
{
privatestaticfinalintBLACK=0xFF000000;
privatestaticfinalintWHITE=0xFFFFFFFF;
privateMatrixToImageWriter(){}
(BitMatrixmatrix){
intwidth=matrix.getWidth();
intheight=matrix.getHeight();
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(intx=0;x<width;x++){
for(inty=0;y<height;y++){
image.setRGB(x,y,matrix.get(x,y)?BLACK:WHITE);
}
}
returnimage;
}
publicstaticvoidwriteToFile(BitMatrixmatrix,Stringformat,Filefile)
throwsIOException{
BufferedImageimage=toBufferedImage(matrix);
if(!ImageIO.write(image,format,file)){
thrownewIOException("Couldnotwriteanimageofformat"+format+"to"+file);
}
}
publicstaticvoidwriteToStream(BitMatrixmatrix,Stringformat,OutputStreamstream)
throwsIOException{
BufferedImageimage=toBufferedImage(matrix);
if(!ImageIO.write(image,format,stream)){
thrownewIOException("Couldnotwriteanimageofformat"+format);
}
}
}
2、生成二维码并添加信息
importjava.io.File;
importjava.util.Hashtable;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.MultiFormatWriter;
importcom.google.zxing.WriterException;
importcom.google.zxing.common.BitMatrix;
publicclassTest{
/**
*@paramargs
*@throwsException
*/
publicstaticvoidmain(String[]args)throwsException{
Stringtext="http://www..com";
intwidth=300;
intheight=300;
//二维码的图片格式
Stringformat="gif";
Hashtablehints=newHashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
BitMatrixbitMatrix=newMultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE,width,height,hints);
//生成二维码
FileoutputFile=newFile("d:"+File.separator+"new.gif");
MatrixToImageWriter.writeToFile(bitMatrix,format,outputFile);
}
}
B. java 如何完成二维码的制作
参考以下代码:
//创建BarcodeSettings实例
BarcodeSettingssettings=newBarcodeSettings();
//设置条码类型为QR二维码
settings.setType(BarCodeType.QR_Code);
//设置二维码数据
settings.setData("Hello123456789");
//设置二维码显示数据
settings.setData2D("Hello123456789");
//设置数据类型
settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);
//设置二维码模型宽度
settings.setX(1.0f);
//设置二维码纠错级别
settings.setQRCodeECL(QRCodeECL.H);
//创建BarCodeGenerator实例
=newBarCodeGenerator(settings);
//根据settings生成图像数据,保存至BufferedImage实例
BufferedImagebufferedImage=barCodeGenerator.generateImage();
//保存为PNG图片
ImageIO.write(bufferedImage,"png",newFile("QRCode.png"));
System.out.println("Complete!");
需要引用Spire.Barcode for java
原文:Java 生成二维码
C. 怎么使用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库