/*******************************************************************************
 * Copyright 2019 2Fi Business Solutions Ltd.
 *
 * This code is part of the Core2 project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.core.utils;

import java.awt.Image;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.QRCodeWriter;

/**
 * ZXing Utils
 *
 * @author Patrick
 */
public abstract class ZXingUtils {

	private static final Writer CODE128_WRITER = new Code128Writer();

	private static final Writer QR_CODE_WRITER = new QRCodeWriter();

	/**
	 * Encode contents (String) to Code 128 image
	 *
	 * @throws WriterException
	 */
	public static Image encodeToImageCode128(String contents, int width, int height) throws WriterException {
		BitMatrix matrix = CODE128_WRITER.encode(contents, BarcodeFormat.CODE_128, width, height);
		return MatrixToImageWriter.toBufferedImage(matrix);
	}

	/**
	 * Encode contents (String) to QR Code image
	 *
	 * @throws WriterException
	 */
	public static Image encodeToImageQRCode(String contents, int width, int height) throws WriterException {
		BitMatrix matrix = QR_CODE_WRITER.encode(contents, BarcodeFormat.QR_CODE, width, height);
		return MatrixToImageWriter.toBufferedImage(matrix);
	}

}
