Tuesday 8 June 2021

Java: Generate UPC-A 1D format code

Below snippet generate UPC-A 1D format code.

Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(message.getBytes(encodingFormat)),BarcodeFormat.UPC_A, imageWidth, imageHeight);

String fileFormat = filePath.substring(filePath.lastIndexOf('.') + 1);
MatrixToImageWriter.writeToPath(bitMatrix, fileFormat, Paths.get(filePath));

 

Find the below working application.

 

UpcA1DCodeUtil.java

 

package com.sample.app.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class UpcA1DCodeUtil {
	public static boolean generateCode(String message, String filePath, Charset encodingFormat, int imageWidth,
			int imageHeight) {

		try {
			Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
			hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

			BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(message.getBytes(encodingFormat)),
					BarcodeFormat.UPC_A, imageWidth, imageHeight);

			String fileFormat = filePath.substring(filePath.lastIndexOf('.') + 1);
			MatrixToImageWriter.writeToPath(bitMatrix, fileFormat, Paths.get(filePath));
			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static String getInfoFromCode(String filePath, Charset encodingFormat)
			throws FileNotFoundException, IOException, NotFoundException {
		BinaryBitmap binaryBitmap = new BinaryBitmap(
				new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath)))));
		Result result = new MultiFormatReader().decode(binaryBitmap);
		return result.getText();
	}
}

 

UpcA1DDemo.java

package com.sample.app;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.google.zxing.NotFoundException;
import com.sample.app.util.Aztec2DBarcodeUtil;
import com.sample.app.util.UpcA1DCodeUtil;

public class UpcA1DDemo {
	public static void main(String args[]) throws FileNotFoundException, NotFoundException, IOException {
		String filePath = "/Users/Shared/secret.png";

		boolean isQRCodeGenerated = UpcA1DCodeUtil.generateCode("111111001111", filePath, StandardCharsets.UTF_8, 400,
				100);

		if (isQRCodeGenerated) {
			System.out.println("code generated successfully");
		} else {
			System.out.println("code generation failed");
		}

		String information = Aztec2DBarcodeUtil.getInfoFromCode(filePath, StandardCharsets.UTF_8);
		System.out.println("Retrieved message is : " + information);

	}
}

 

Output

code generated successfully

Retrieved message is : 111111001111

 

Generated secret.png file

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment