I am using zxing api for creating barcode. But while creating i am not able to write barcode content as label below the barcode.
output --
output required --
The code to generate these barcode are as such -
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);
}
}
As the QRCode specifiaction does not provide an option to include the content into the image I do not think zxing or any other barcode generator does offer this functionality. You will have to add the text to the image on your own. Be aware that you have to leave enough white space around the QRCode. Your above image doesn't have enough whitespace at the bottom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With