Java--生成Code128條形碼
阿新 • • 發佈:2021-01-18
生成條形碼並在條碼下面新增對應的編碼,通過Java我是用的是com.google.zxing來實現的
使用的jar
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing< /groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
雖然通過barcode4j也可以實現一維碼的生成-下面是依賴
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
< version>2.0</version>
</dependency>
先說下為啥不用barcode4j實現,按照道理barcode4j生成是比較方便的-他可以直接設定條碼下方的內容等資訊—當時在使用過程中發現條碼的掃描的靈敏度不是很高-需要調節條碼的解析度/設定條碼每一條的寬度才能讓條碼變得靈敏,但是這個條碼需要列印,列印的紙張不是很大-所以最後選擇了google.zxing來實現
具體實現的程式碼
/**
* 生成一維碼(128) 生成的一維碼兩邊不會留白
* 不設定一維碼的寬度 保證掃描靈敏
* @param str 一維碼
* @param barCodeHeight 一維碼的高度
* @param type 是否在圖片下新增字型 目前只做下面新增
* @param pictureWidth 圖片的寬度
* @param pictureHeight 圖片的高度
* @return
*/
public static String toBarCodeMatrix(String str,Integer barCodeHeight,
Boolean type,Integer pictureWidth,Integer pictureHeight) {
try {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(str, BarcodeFormat.CODE_128, 0, barCodeHeight, hints);
//一維碼轉圖片
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix,new MatrixToImageConfig());
if (type&&pictureHeight>image.getHeight()){
//寬度和條形碼的寬度一樣-----高度=圖片的高度-條形碼高度
BufferedImage target = new BufferedImage(image.getWidth(), pictureHeight-image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = target.createGraphics();
//設定成白色的底
g.setColor(Color.WHITE);
g.fillRect(0,0,image.getWidth(),pictureHeight-image.getHeight());
target.flush();
g.dispose();
image = mergeImage(image,target);
Graphics2D graphics = image.createGraphics();
Font font = new Font(null, Font.PLAIN, 10);
FontMetrics metrics = graphics.getFontMetrics(font);
int startX = (image.getWidth(null) - metrics.stringWidth(str)) / 2;
int endY = image.getHeight(null);
graphics.setColor(Color.BLACK);
graphics.setFont(font);
graphics.drawString(str, startX, endY);
graphics.dispose();
}
//進行圖片縮放
image = imageResize(image,pictureWidth,pictureHeight);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, FORMAT_NAME, bos);
return Base64.encodeBase64String(bos.toByteArray()).replaceAll("[\r\n]","");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 實現影象的等比縮放
* @param source 原來的圖片
* @param targetW 想要的寬
* @param targetH 想要的高
* @return 縮放後的圖片
*/
private static BufferedImage imageResize(BufferedImage source, int targetW,
int targetH) throws IOException {
return Thumbnails.of(source).forceSize(targetW,targetH)
.outputFormat(FORMAT_NAME).outputQuality(1).asBufferedImage();
}
/**
* 垂直方向合併圖片,寬度必須相等。
* @param imgOne 待合併的第一張圖
* @param imgTwo 待合併的第二張圖
* @return 返回合併後的BufferedImage物件
* @throws IOException
*/
private static BufferedImage mergeImage(BufferedImage imgOne,
BufferedImage imgTwo) throws IOException {
int w1 = imgOne.getWidth();
int h1 = imgOne.getHeight();
int w2 = imgTwo.getWidth();
int h2 = imgTwo.getHeight();
// 從圖片中讀取RGB
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = imgOne.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
int[] ImageArrayTwo = new int[w2 * h2];
ImageArrayTwo = imgTwo.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);
// 生成新圖片
BufferedImage destImage = new BufferedImage(w1, h1 + h2, BufferedImage.TYPE_INT_BGR);
destImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
destImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2);
return destImage;
}
雖然這個程式碼和barcode4j比較多好多但是這個符合我的需求,不管是大小還是靈敏度都可以 主要思路生成條碼然後拼接圖片-如果需要縮放還能根據大小進行縮放 當時進行縮放也會影響條碼的靈敏度
生成的效果
如果各位大佬, 還有其他的什麼好的思路來實現可以留言告訴下弟弟我!感激不盡