我正在使用 zxing api 来创建条形码。但是在创建时,我无法将条形码内容写为条形码下方的标签。
输出 -
需要输出——
生成这些条形码的代码是这样的 -
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class BarcodeTesting {
private static void wrtieToStream(BitMatrix bitMatrix) {
try {
MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File("hello" + ".png")));
System.out.println( " Barcode Generated.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private BitMatrix generateBitMatrix(String content, BarcodeFormat format, int width, int height) {
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix bitMatrix = null;
try {
bitMatrix = writer.encode(content, format, width, height);
} catch (WriterException e) {
e.printStackTrace();
}
return bitMatrix;
}
public static void main(String[] args) {
BarcodeTesting obj = new BarcodeTesting();
BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
BitMatrix bitMatrix = obj.generateBitMatrix("MY QR Code 123", barcodeFormat, 24, 24);
// String formatString = format.toString();
wrtieToStream(bitMatrix);
}
}