Java QR-Code generation with zxing processing library

ZXING

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
 
/**
 * @author Aaron Kreis (www.aaron.de)
 **/
public class QRCodeGenerator {	
	public static void createQRCode(String content, int width, String imageType, String fileName) throws WriterException, IOException {
		QRCodeWriter codeWriter = new QRCodeWriter();
		
		BitMatrix bitMatrix =  codeWriter.encode(content, BarcodeFormat.QR_CODE, width, width);		
		int matrixWidth = bitMatrix.getWidth();
		
		BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
		image.createGraphics();
		
		Graphics2D graphics = (Graphics2D) image.getGraphics();
		graphics.fillRect(0, 0, matrixWidth, matrixWidth);
		graphics.setColor(Color.BLACK);
		
		for (int i=0; i<matrixWidth; i++) {
			for (int j=0; j<matrixWidth; j++) {
				if (bitMatrix.get(i, j)) {
					graphics.fillRect(i, j, 1, 1);
				}
			}			
		}
		ImageIO.write(image, imageType, new File(fileName));
	}

	public static void main(String[] args) throws WriterException, IOException {		
		String content = "Aaron Kreis\n+49 (0)179 9749322\nmail@aaron.de\nwww.aaron.de\n";		
		new QRCodeGenerator().createQRCode(content, 200, "jpg", "test.jpg");		
	}
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.