java生产二维码和条形码

引入依赖

<!-- 生产二维码-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>

        <!-- 生产条形码-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.1</version>
        </dependency>

条形码工具类

package com.deka.web.controller.createImage;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
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.pdf417.PDF417Writer;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wyt
 * @Date 2023/6/21 下午 3:25
 * @Version 1.0
 * 条形码工具类
 */
public class BarcodeUtil {
    /**
     * 默认图片宽度
     */
    private static final int DEFAULT_PICTURE_WIDTH = 230;

    /**
     * 默认图片高度
     */
    private static final int DEFAULT_PICTURE_HEIGHT = 100;

    /**
     * 默认条形码宽度
     */
    private static final int DEFAULT_BAR_CODE_WIDTH = 100;

    /**
     * 默认条形码高度
     */
    private static final int DEFAULT_BAR_CODE_HEIGHT = 40;

    /**
     * 默认字体大小
     */
    private static final int DEFAULT_FONT_SIZE = 24;

    /**
     * 设置 条形码参数
     */
    private static final Map<EncodeHintType, Object> hints = new HashMap<>();

    static {
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    }

    /**
     * 获取条形码图片
     *
     * @param codeValue 条形码内容
     * @return 条形码图片
     */
    public static BufferedImage getBarCodeImage(String codeValue) {
        return getBarCodeImage(codeValue, DEFAULT_BAR_CODE_WIDTH, DEFAULT_BAR_CODE_HEIGHT);
    }

    /**
     * 获取条形码图片
     *
     * @param codeValue 条形码内容
     * @param width     宽度
     * @param height    高度
     * @return 条形码图片
     */
    public static BufferedImage getBarCodeImage(String codeValue, int width, int height) {
        // CODE_128是最常用的条形码格式
        return getBarCodeImage(codeValue, width, height, BarcodeFormat.CODE_128);
    }

    /**
     * 获取条形码图片
     *
     * @param codeValue     条形码内容
     * @param width         宽度
     * @param height        高度
     * @param barcodeFormat 条形码编码格式
     * @return 条形码图片
     */
    public static BufferedImage getBarCodeImage(String codeValue, int width, int height, BarcodeFormat barcodeFormat) {
        Writer writer;
        switch (barcodeFormat) {
            case CODE_128:
                // 最常见的条形码,但是不支持中文
                writer = new Code128Writer();
                break;
            case PDF_417:
                // 支持中文的条形码格式
                writer = new PDF417Writer();
                break;
            // 如果使用到其他格式,可以在这里添加
            default:
                writer = new Code128Writer();
        }

        // 编码内容, 编码类型, 宽度, 高度, 设置参数
        BitMatrix bitMatrix;
        try {
            bitMatrix = writer.encode(codeValue, barcodeFormat, width, height, hints);
        } catch (WriterException e) {
            throw new RuntimeException("条形码内容写入失败");
        }
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }

    /**
     * 获取条形码
     *
     * @param codeValue 条形码内容
     * @param bottomStr 底部文字
     * @return
     */
    public static BufferedImage getBarCodeWithWords(String codeValue, String bottomStr) {
        return getBarCodeWithWords(codeValue, bottomStr, "", "");
    }

    /**
     * 获取条形码
     *
     * @param codeValue   条形码内容
     * @param bottomStr   底部文字
     * @param topLeftStr  左上角文字
     * @param topRightStr 右上角文字
     * @return
     */
    public static BufferedImage getBarCodeWithWords(String codeValue,
                                                    String bottomStr,
                                                    String topLeftStr,
                                                    String topRightStr) {
        return getCodeWithWords(getBarCodeImage(codeValue),
                bottomStr,
                topLeftStr,
                topRightStr,
                DEFAULT_PICTURE_WIDTH,
                DEFAULT_PICTURE_HEIGHT,
                0,
                0,
                0,
                0,
                0,
                0,
                DEFAULT_FONT_SIZE);
    }

    /**
     * 获取条形码
     *
     * @param codeImage       条形码图片
     * @param bottomStr       底部文字
     * @param topLeftStr      左上角文字
     * @param topRightStr     右上角文字
     * @param pictureWidth    图片宽度
     * @param pictureHeight   图片高度
     * @param codeOffsetX     条形码宽度
     * @param codeOffsetY     条形码高度
     * @param topLeftOffsetX  左上角文字X轴偏移量
     * @param topLeftOffsetY  左上角文字Y轴偏移量
     * @param topRightOffsetX 右上角文字X轴偏移量
     * @param topRightOffsetY 右上角文字Y轴偏移量
     * @param fontSize        字体大小
     * @return 条形码图片
     */
    public static BufferedImage getCodeWithWords(BufferedImage codeImage,
                                                 String bottomStr,
                                                 String topLeftStr,
                                                 String topRightStr,
                                                 int pictureWidth,
                                                 int pictureHeight,
                                                 int codeOffsetX,
                                                 int codeOffsetY,
                                                 int topLeftOffsetX,
                                                 int topLeftOffsetY,
                                                 int topRightOffsetX,
                                                 int topRightOffsetY,
                                                 int fontSize) {
        BufferedImage picImage = new BufferedImage(pictureWidth, pictureHeight, BufferedImage.TYPE_INT_RGB);

        Graphics2D g2d = picImage.createGraphics();
        // 抗锯齿
        setGraphics2D(g2d);
        // 设置白色
        setColorWhite(g2d, picImage.getWidth(), picImage.getHeight());


        // 条形码默认居中显示
//        int codeStartX = (pictureWidth - codeImage.getWidth()) / 2 + codeOffsetX;
//        int codeStartY = (pictureHeight - codeImage.getHeight()) / 2 + codeOffsetY;
        int codeStartX = 10;
        int codeStartY = 20;
        // 画条形码到新的面板
        g2d.drawImage(codeImage, codeStartX, codeStartY, codeImage.getWidth(), codeImage.getHeight(), null);

        // 画文字到新的面板
        g2d.setColor(Color.BLACK);
        // 字体、字型、字号
        g2d.setFont(new Font("宋体", Font.PLAIN, fontSize));
        // 文字与条形码之间的间隔
        int wordAndCodeSpacing = 3;

        //尝试设置边框------------------
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.drawRect(1,1,228,97);
//        g2d.drawLine(0,100,180,120);



        //-----------------

        if (StringUtils.isNotEmpty(bottomStr)) {
            // 文字长度
            int strWidth = g2d.getFontMetrics().stringWidth(bottomStr);
            // 文字X轴开始坐标,这里是居中
            int strStartX = codeStartX + (codeImage.getWidth() - strWidth) / 2;
            // 文字Y轴开始坐标
            int strStartY = codeStartY + codeImage.getHeight() + fontSize + wordAndCodeSpacing;
            // 画文字
            g2d.drawString(bottomStr, strStartX, strStartY);
        }

        if (StringUtils.isNotEmpty(topLeftStr)) {
            // 文字长度
            int strWidth = g2d.getFontMetrics().stringWidth(topLeftStr);
            // 文字X轴开始坐标
            int strStartX = codeStartX + topLeftOffsetX;
            // 文字Y轴开始坐标
            int strStartY = codeStartY + topLeftOffsetY - wordAndCodeSpacing;
            // 画文字
            g2d.drawString(topLeftStr, strStartX, strStartY);
        }

        if (StringUtils.isNotEmpty(topRightStr)) {
            // 文字长度
            int strWidth = g2d.getFontMetrics().stringWidth(topRightStr);
            // 文字X轴开始坐标,这里是居中
            int strStartX = codeStartX + codeImage.getWidth() - strWidth + topRightOffsetX;
            // 文字Y轴开始坐标
            int strStartY = codeStartY + topRightOffsetY - wordAndCodeSpacing;
            // 画文字
            g2d.drawString(topRightStr, strStartX, strStartY);
        }

        g2d.dispose();
        picImage.flush();

        return picImage;
    }

    /**
     * 设置 Graphics2D 属性  (抗锯齿)
     *
     * @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
     */
    private static void setGraphics2D(Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
        g2d.setStroke(s);
    }

    /**
     * 设置背景为白色
     *
     * @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
     */
    private static void setColorWhite(Graphics2D g2d, int width, int height) {
        g2d.setColor(Color.WHITE);
        //填充整个屏幕
        g2d.fillRect(0, 0, width, height);
        //设置笔刷
        g2d.setColor(Color.BLACK);
    }
}

二维码工具类

package com.deka.web.controller.createImage;

import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import sun.font.FontDesignMetrics;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

/**
 * @Author wyt
 * @Date 2023/6/21 上午 11:55
 * @Version 1.0
 */
@Slf4j
@Component
public class QRCodeUtil {
    /**
     * 创建二维码
     * @param charSet 编码方式
     * @param content 二维码内容
     * @param qrWidth 二维码长度
     * @param qrHeight 二维码高度
     * @return
     */
    public static BufferedImage createImage(String charSet, String content, int qrWidth, int qrHeight){
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, charSet);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,qrWidth , qrHeight, // 修改二维码底部高度
                    hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    /**
     * 对已经生成好的二维码设置logo
     * @param source 二维码
     * @param logo logo图片
     * @param logoWidth logo宽度
     * @param logoHeight logo高度
     */
    public static void insertLogoImage(BufferedImage source,Image logo,int logoWidth,int logoHeight){
        Graphics2D graph = source.createGraphics();
        int qrWidth = source.getWidth();
        int qrHeight = source.getHeight();
        int x = (qrWidth - logoWidth) / 2;
        int y = (qrHeight - logoHeight) / 2;
        graph.drawImage(logo, x, y, logoWidth, logoHeight, null);
        Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

    /**
     * 缩小logo图片
     * @param logoPath
     * @param logoWidth
     * @param logoHeight
     * @return
     */
    public static Image compressLogo(String logoPath, int logoWidth, int logoHeight){
        File file = new File(logoPath);
        if (!file.exists()) {
            System.err.println("" + logoPath + "   该文件不存在!");
            return null;
        }
        Image original = null;
        try {
            original = ImageIO.read(new File(logoPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        int width = original.getWidth(null);
        int height = original.getHeight(null);
        if (width > logoWidth) {
            width = logoWidth;
        }
        if (height > logoHeight) {
            height = logoHeight;
        }
        Image image = original.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // 绘制缩小后的图
        g.dispose();
        return image;
    }

    /**
     * 增加底部的说明文字
     * @param source 二维码
     * @param text 说明内容
     * @param step
     */
    public static BufferedImage addBottomFont(BufferedImage source, String text,int step) {

        int qrWidth = source.getWidth();
        log.debug("二维码的宽度{}",qrWidth);
        int qrHeight = source.getHeight();
        log.debug("二维码的高度{}",qrHeight);
        BufferedImage textImage = textToImage(text, qrWidth, 20,16);
        Graphics2D graph = source.createGraphics();
        //开启文字抗锯齿
        graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        int width = textImage.getWidth(null);
        int height = textImage.getHeight(null);

        Image src = textImage;
        graph.drawImage(src, 0, qrHeight - (20 * step) - 10, width, height, null);
        graph.dispose();
        return  source;
    }

    /**
     * 将文明说明增加到二维码上
     * @param str
     * @param width
     * @param height
     * @param fontSize 字体大小
     * @return
     */
    public static BufferedImage textToImage(String str, int width, int height,int fontSize) {
        BufferedImage textImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D)textImage.getGraphics();
        //开启文字抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, width, height);
        g2.setPaint(Color.BLACK);
        FontRenderContext context = g2.getFontRenderContext();
        Font font = new Font("微软雅黑", Font.BOLD, fontSize);
        g2.setFont(font);
        LineMetrics lineMetrics = font.getLineMetrics(str, context);
        FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
        float offset = (width - fontMetrics.stringWidth(str)) / 2;
        float y = (height + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
        g2.drawString(str, (int)offset, (int)y);
        return textImage;
    }

    /**
     * 顶部增加说明文字
     * @param source
     * @param text
     */
    public static void addUpFont(BufferedImage source, String text) {
        int qrWidth = source.getWidth();
        int qrHeight = source.getHeight();

        BufferedImage textImage = textToImage(text, qrWidth, 30,24);
        Graphics2D graph = source.createGraphics();
        //开启文字抗锯齿
        graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        int width = textImage.getWidth(null);
        int height = textImage.getHeight(null);

        Image src = textImage;
        graph.drawImage(src, 0, 30, width, height, null);
        graph.dispose();
    }

    /**
     * 生成二维码图片
     * @param charSet 二维码编码方式
     * @param content 内容
     * @param qrWidth 宽度
     * @param qrHeight 长度
     * @param formatName jpg等图片格式
     * @param imgPath 二维码存放路径
     */
    public static void encode(String charSet,String content,int qrWidth,int qrHeight,String formatName,String imgPath){
        BufferedImage image = QRCodeUtil.createImage(charSet,content,qrWidth,qrHeight);
        try {
            ImageIO.write(image, formatName, new File(imgPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成二维码图片流
     * @param charSet 二维码编码方式
     * @param content 内容
     * @param qrWidth 宽度
     * @param qrHeight 长度
     * @return
     */
    public static BufferedImage encode(String charSet,String content,int qrWidth,int qrHeight) {
        BufferedImage image = QRCodeUtil.createImage(charSet,content,qrWidth,qrHeight);
        return image;
    }

    public static void encode( BufferedImage image,String formatName,String imgPath){
        try {

            ImageIO.write(image, formatName, new File(imgPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void mkdirs(String destPath) {
        File file = new File(destPath);
        // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    /*
     * 解析二维码
     */
    public static String decode(File file,String cherSet) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        Hashtable hints = new Hashtable();
        hints.put(DecodeHintType.CHARACTER_SET, cherSet);
        result = new MultiFormatReader().decode(bitmap, hints);
        String resultStr = result.getText();
        return resultStr;
    }
}
使用
package com.deka.web.controller.createImage;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;


/**
 * @Author wyt
 * @Date 2023/6/21 上午 10:32
 * @Version 1.0
 */
public class ProductImage {
    private BufferedImage image;
    private int imageWidth = 968;  //图片的宽度
    private int imageHeight = 1357; //图片的高度

    //生成图片文件
    @SuppressWarnings("restriction")
    public void createImage(String fileLocation) {
        BufferedOutputStream bos = null;
        if (image != null) {
            try {
                FileOutputStream fos = new FileOutputStream(fileLocation);
                bos = new BufferedOutputStream(fos);

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
                encoder.encode(image);
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {//关闭输出流
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    //批量处理重复数据  头部内容
    public void handleData(BufferedImage image, String title, String content, int height, int width) {
        Graphics tempTitle = image.createGraphics();
        //设置区域颜色
//        title.setColor(new Color(143, 0, 0));
        //填充区域并确定区域大小位置
        tempTitle.fillRect(0, 0, width, height);
        //设置字体颜色,先设置颜色,再填充内容
        tempTitle.setColor(Color.black);
        //设置字体
        Font titleFont = new Font("宋体", Font.BOLD, 24);
        tempTitle.setFont(titleFont);
        tempTitle.drawString(title + ":" + content, 5, height);
    }

    public void graphicsGeneration(String name, String id, String classname) throws IOException {
        int baseHeight = 30; //基础高度
        int gongyingshangmingcheng = baseHeight;     //供应商名称高度
        int jijumingcheng = gongyingshangmingcheng + baseHeight; //机具名称
        int shuliang = jijumingcheng + baseHeight;//数量
        int chuchangriqi = shuliang + baseHeight; //出厂日期
        int xianghao = chuchangriqi + baseHeight; //箱号
        int beizhu = xianghao + baseHeight;  //备注


        int H_mainPic = 200;  //轮播广告高度
        int H_tip = 60;  //上网提示框高度
        int H_btn = 25;  //按钮栏的高度
        int tip_2_top = (gongyingshangmingcheng + H_mainPic);
        int btns_2_top = tip_2_top + H_tip + 20;
        int btn1_2_top = btns_2_top + 10;
        int btn2_2_top = btn1_2_top + H_btn;
        int shops_2_top = btn2_2_top + H_btn + 20;
        int W_btn = 280;  //按钮栏的宽度

        image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        //设置图片的背景色
        Graphics2D main = image.createGraphics();
        main.setColor(Color.white);
        main.fillRect(0, 0, imageWidth, imageHeight);

        //***********************供应商名称
        Graphics title = image.createGraphics();
        //设置区域颜色
//        title.setColor(new Color(143, 0, 0));
        //填充区域并确定区域大小位置
        title.fillRect(0, 0, imageWidth, gongyingshangmingcheng);
        //设置字体颜色,先设置颜色,再填充内容
        title.setColor(Color.black);
        //设置字体
        Font titleFont = new Font("宋体", Font.BOLD, 24);
        title.setFont(titleFont);
        title.drawString("供应商名称:深圳市德卡科技技术股份有限公司", 5, gongyingshangmingcheng - 5);
        //机具名称
        handleData(image, "机具名称", "医保业务综合服务终端", jijumingcheng, gongyingshangmingcheng - 200);
        //机具名称
        handleData(image, "数量", "4 台/箱", shuliang, gongyingshangmingcheng - 200);
        //机具名称
        handleData(image, "出厂日期", "2023年4月", chuchangriqi, gongyingshangmingcheng - 200);
        //机具名称
        handleData(image, "箱号", "7/8(SZPT2023040280)", xianghao, gongyingshangmingcheng - 200);
        //机具名称
        handleData(image, "备注", "毛重 13.6Kg 净重 9.9Kg", beizhu, gongyingshangmingcheng - 200);

        //添加二维码
        Graphics mainPic = image.getGraphics();
        BufferedImage bimg = null;
        String profile = "D:/tempUpload";
        String tempPath=profile;
//        log.info("获取系统存储路径为 {}",profile);
        profile = profile + "/tempQrCodeImage.png";
//        log.info("实际存储图片路径为 {}",profile);
        System.out.println("实际存储图片路径为 {}" + profile);
        try {
            QRCodeUtil.encode("utf-8", "1234567890", 200, 200, "png", profile);
            bimg = javax.imageio.ImageIO.read(new java.io.File(profile));
        } catch (Exception e) {
            System.out.println(e.getMessage());
//            log.info(e.getMessage());
        } finally {
            File file = new File(profile);
            if (file.exists()) {
                file.delete();
            }
        }
        if (bimg != null) {
            mainPic.drawImage(bimg, 450, 30, 180, 180, null);
            mainPic.dispose();
        }
        int tempHeight = 120;


        // 生成条形码 sn
        for (int i = 1; i <= 4; i++) {
            Graphics mainPici = image.getGraphics();
            mainPici.setColor(Color.black);
            BufferedImage bimgi = null;
            String msg = i + "";
            String path = tempPath + "/barCode"+i+".png";
            System.out.println("条形码存储路径为 :" + path);

            BufferedImage image = BarcodeUtil.getBarCodeWithWords(i + "_3C002F112293000058", "医保SN:" + i);

            ImageIO.write(image, "png", new File(path));
            File tempfile = new File(path);
            try {
                bimgi = javax.imageio.ImageIO.read(tempfile);
            } catch (Exception e) {
                e.getMessage();
            } finally {
                if (tempfile.exists()) {
                    tempfile.delete();
                }
            }
            if (bimgi != null) {
                tempHeight = tempHeight + 138;
                mainPici.drawImage(bimgi, 0, tempHeight, 450, 150, null);
                mainPici.dispose();
            }
        }

        //禾苗SN
        // 生成条形码 sn
        tempHeight=120;
        for (int i = 1; i <= 4; i++) {
            Graphics mainPici = image.getGraphics();
            mainPici.setColor(Color.black);
            BufferedImage bimgi = null;
            String msg = i + "";
            String path = tempPath + "/HMbarCode"+i+".png";
            System.out.println("条形码存储路径为 :" + path);

            BufferedImage image = BarcodeUtil.getBarCodeWithWords(i + "_HNDF4AS220628001747", "SN:" + i);

            ImageIO.write(image, "png", new File(path));
            File tempfile = new File(path);
            try {
                bimgi = javax.imageio.ImageIO.read(tempfile);
            } catch (Exception e) {
                e.getMessage();
            } finally {
                if (tempfile.exists()) {
                    tempfile.delete();
                }
            }
            if (bimgi != null) {
                tempHeight = tempHeight + 138;
                mainPici.drawImage(bimgi, 447, tempHeight, 450, 150, null);
                mainPici.dispose();
            }
        }
        createImage("D:\\product_image\\hehe.jpg");
    }

    public static void main(String[] args) {
        ProductImage cg = new ProductImage();
        try {
            cg.graphicsGeneration("ewew", "1", "12");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值