一栅隐、說(shuō)幾句
機(jī)關(guān)單位的公文條形碼生成,統(tǒng)一根據(jù)《機(jī)關(guān)公文二維條碼使用規(guī)范細(xì)則》規(guī)范制作。
二租悄、引入工具包
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
三谨究、上代碼
1、測(cè)試demo
package com.barcode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lowagie.text.pdf.BarcodePDF417;
import jj2000.j2k.util.MathUtil;
import org.apache.commons.lang.StringUtils;
import org.json.JSONString;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class BarcodeDemo {
public static void main(String[] args) throws IOException {
createPdf417();
}
private static final String codeString = "GB0626-2005^GD000014001300000871^廣東省**單位^通知^粵**函〔2013〕1**號(hào)^**單位^***標(biāo)題^無(wú)^無(wú)^20131031^^打字室^20131031^^|";
public static void createPdf417() throws IOException {
BarcodePDF417 pdf = new BarcodePDF417();
pdf.setText(codeString.getBytes("GBK"));
Image pdfImg = pdf.createAwtImage(Color.black, Color.white);
BufferedImage img = new BufferedImage(pdfImg.getWidth(null), 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/admin/Desktop/pdf417.bmp"));
// ImageIO.write(img, "PNG", os);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "PNG", baos);
byte[] data = baos.toByteArray();
InputStream is = new ByteArrayInputStream(baos.toByteArray());
System.out.println("圖像輸出流:");
System.out.println(baos.toByteArray());
byte2image(data, "C:/Users/admin/Desktop/pdf.bmp" );
}
//byte數(shù)組到圖片
public static void byte2image(byte[] data, String path) {
if (data.length < 3 || path.equals("")) {return;}
try {
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
}
2泣棋、場(chǎng)景重現(xiàn)
/**
* 生成二維條碼
*
* @param id 文檔id
* @param signSecureTerm 密級(jí)后胶哲,是否需要標(biāo)注保密期限 格式例如:絕密30年
* @return
*/
@GetMapping("/getDisBarcode")
public void getDisBarcode(@RequestParam String id, boolean signSecureTerm, HttpServletResponse response) {
//必須聲明圖片格式,否則前端無(wú)法取到文件流
response.setContentType("image/png");
this.disMng.getDisBarcode(id, signSecureTerm, response);
}
3潭辈、功能實(shí)現(xiàn)
@Override
public void getDisBarcode(String id, boolean signSecureTerm, String unitName, HttpServletResponse response) {
String barcodeStr = "GB0626-2005^GD000014001300000871^廣東省**單位^通知^粵**函〔2013〕1**號(hào)^**單位^***標(biāo)題^無(wú)^無(wú)^20131031^^打字室^20131031^^|";
//2鸯屿、生成二維條碼
BarcodePDF417 pdf = new BarcodePDF417();
try {
pdf.setText(barcodeStr.getBytes("GBK"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new Exception("UnsupportedEncodingException");
}
Image pdfImg = pdf.createAwtImage(Color.black, Color.white);
BufferedImage img = new BufferedImage(pdfImg.getWidth(null), pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.drawImage(pdfImg, 0, 0, Color.white, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(img, "PNG", baos);
} catch (IOException e) {
e.printStackTrace();
throw new Exception("IOException");
}
byte[] fileByte = baos.toByteArray();
// 設(shè)置文件名, 圖片轉(zhuǎn)為流的形式返回
try {
response.addHeader("Content-Disposition",
String.format("attachment;filename*=utf-8'zh_cn'%s", URLEncoder.encode("barcode.png", "UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
InputStream is = new ByteArrayInputStream(fileByte);
try (
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = response.getOutputStream();
) {
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
四、參考文章
1萎胰、java生成PDF417條碼: https://huhongyu.iteye.com/blog/1969400
2碾盟、java用Itext生成條形碼和二維碼:https://wjrko.iteye.com/blog/2256508