Java后台生成条形码(linux)生成条形码BarcodePDF417

生成条形码BarcodePDF417

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.io.*;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import java.awt.Color;  
import java.awt.Font;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.font.FontRenderContext;  
import java.awt.font.TextAttribute;  
import java.awt.image.BufferedImage;  
import java.io.BufferedOutputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.OutputStream;  
 
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.itextpdf.text.BadElementException;  
import com.itextpdf.text.pdf.BarcodePDF417; 
public static void createPdf417()throws IOException{ BarcodePDF417 pdf = new BarcodePDF417(); /*String str = new String(codeString.getBytes(),"GBK");*/ /* pdf.setText(codeString.getBytes("GBK")); pdf.setYHeight(4);*/ pdf.setText(codeString.getBytes("GBK")); Image pdfImg = pdf.createAwtImage(Color.black, Color.white); BufferedImage img = new BufferedImage((int)pdfImg.getWidth(null), (int)pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.drawImage(pdfImg, 0, 0, Color.white, null); OutputStream os = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Lenovo\\Desktop\\111.png")); ImageIO.write(img, "PNG", os); }
  private static final String codeString =
              "XXXXXXXXX^123XXXXXXXXXX3XXX3^XX^自XXXXXXXXXXXXXXXX料^2020-12-08 17:56:34.0^XXXXXX单位^2020-12-08 17-56-44^XXXXXXXXXX机构^|";  
    public static void main(String[] args) throws IOException {
        
         createPdf417();
      
        
    }

完美生成pdf条形码,但是如果改变图片边大小来显示可以用下面的方法实现(下面方法可能会导致图片过小或者过大扫码枪不能识别

本人当时生成条形码的图片没改变大小,但是放到PDF文件上的时候通过方法控制了一下大小

(PS详情:https://www.cnblogs.com/sjzxs/p/14068392.html)

/**
     * 改变图片的大小
     * @return boolean
     */
    public static boolean readImage(  ) {
    //设定要生成的新图片的宽高和图片格式    
    BufferedImage newbi = new BufferedImage(300, 50, BufferedImage.TYPE_INT_BGR);
        boolean val = false;
        try {
            //从硬盘读取一张图片
            BufferedImage oldbi = ImageIO.read(new File("C:\\Users\\Lenovo\\Desktop\\111.png"));
            //把读过的来的图片画到设定好的新图片上
            newbi.getGraphics().drawImage(oldbi, 0, 0, 300, 50, null);
            //通过ImageIO写到硬盘上去
            val = ImageIO.write(newbi, "png", new File("C:\\Users\\Lenovo\\Desktop\\11133.png"));
            return val;
        }catch(IOException e) {
            e.printStackTrace();
        }
        return val;
    }

 

上一篇:[Java] Modules


下一篇:java-使用Graphics2d合成图片和文字