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庫