pom.xml#

<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.0</version>
</dependency>

Controller.java#

model.addAttribute("barcodeImage", barcodeService.getBarcodeImageString(barcodeText, 100, 45));

Service.java#

import com.google.zxing.BarcodeFormat;
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;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Service
public class BarcodeService {
    private static final String BARCODE_FORMAT = "PNG";
    private static final String CHARSET_NAME = "UTF-8";

    public String getBarcodeImageString(String text, int width, int height) {
        return convertByteArrayToBase64String(this.getBarCodeImageByteArray(text, width, height));
    }

    public String getQRcodeImageString(String text, int width, int height) {
        return convertByteArrayToBase64String(this.getQRCodeImageByteArray(text, width, height));
    }

    public byte[] getBarCodeImageByteArray(String text, int width, int height) {
        Code128Writer code128Writer = new Code128Writer();
        try {
            BitMatrix bitMatrix = code128Writer.encode(text, BarcodeFormat.CODE_128, width, height);
            ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(bitMatrix, BARCODE_FORMAT, pngOutputStream);
            return pngOutputStream.toByteArray();
        } catch (WriterException | IOException e) {
            log.error(e.getMessage(), e);
        }

        return ArrayUtils.EMPTY_BYTE_ARRAY;
    }

    public byte[] getQRCodeImageByteArray(String text, int width, int height) {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        try {
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
            ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();

            MatrixToImageWriter.writeToStream(bitMatrix, BARCODE_FORMAT, pngOutputStream);
            return pngOutputStream.toByteArray();
        } catch (WriterException | IOException e) {
            log.error(e.getMessage(), e);
        }

        return ArrayUtils.EMPTY_BYTE_ARRAY;
    }

    /**
     * jsp에서 img태그에 노출하기 위해 base64문자열로 변환
     *
     * <img src="data:image/png;base64,${barcode}" width="100%" height="100%" alt="바코드">
     *
     * @param imageByteArray
     * @return
     */
    private String convertByteArrayToBase64String(byte[] imageByteArray) {
        if (Objects.isNull(imageByteArray)) {
            return StringUtils.EMPTY;
        }

        byte[] encodeBase64 = Base64.encodeBase64(imageByteArray);
        try {
            return new String(encodeBase64, CHARSET_NAME);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
        }

        return StringUtils.EMPTY;
    }
}

jsp#

<img src="data:image/png;base64,${barcodeImage}" width="100%" alt="바코드">

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-2) was last changed on 05-Dec-2019 10:38 by DongGukLee  
G’day (anonymous guest) My Prefs

Referenced by
Java

JSPWiki v2.8.4