Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Barcode content as label below the Barcode using Java Zxing API

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 -- enter image description here

output required -- enter image description here

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);
	}
}
like image 765
joshi Avatar asked Nov 01 '22 04:11

joshi


1 Answers

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.

like image 157
tobltobs Avatar answered Nov 15 '22 04:11

tobltobs