所有工具類
條形碼類型及常見條形碼介紹條形碼或條碼(barcode)是將寬度不等的多個黑條和空白,按照一定的編碼規則排列,用以表達一組信息的圖形標識符。常見的條形碼是由反射率相差很大的黑條(簡稱條)和白條(簡稱空)排成的平行線圖案。條形碼可以標出物品的生產國、制造廠家、商品名稱、生產日期、圖書分類號、郵件起止地點、類別、日期等許多信息,因而在商品流通、圖書管理、郵政管理、銀行系統等許多領域都得到了廣泛的應用。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package zj.bar.util;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code39Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;
/**
* 條形碼類
*
* @version 1.00 (2014.09.15)
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
*/
public class BarCodeUtil {
public static void main(String[] paramArrayOfString) {
exportBarCode("959009257120", "D:\\zhangjun\\test-file\\barCode\\EAN13.jpeg");
}
public static void exportBarCode(String str, String path) {
try {
JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance());
// 生成. 歐洲商品條碼(=European Article Number)
// 這里我們用作圖書條碼
// String str = "959009257120";
System.out.println(str.length());
// 默認
localJBarcode.setShowCheckDigit(true);
BufferedImage localBufferedImage = localJBarcode.createBarcode(str);
saveToFile(localBufferedImage, path);
// saveToPNG(localBufferedImage, "EAN13.png");
// saveToGIF(localBufferedImage, "EAN13.gif");
localJBarcode.setEncoder(Code39Encoder.getInstance());
localJBarcode.setPainter(WideRatioCodedPainter.getInstance());
localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());
localJBarcode.setShowCheckDigit(false);
// xx
str = "JBARCODE-39";
localBufferedImage = localJBarcode.createBarcode(str);
saveToFile(localBufferedImage, "D:\\zhangjun\\test-file\\barCode\\Code39.jpeg");
// saveToPNG(localBufferedImage, "Code39.png");
// saveToGIF(localBufferedImage, "Code39.gif");
} catch (Exception localException) {
localException.printStackTrace();
}
}
// static void saveToJPEG(BufferedImage paramBufferedImage, String
// paramString) {
// saveToFile(paramBufferedImage, paramString, "jpeg");
// }
//
// static void saveToPNG(BufferedImage paramBufferedImage, String
// paramString) {
// saveToFile(paramBufferedImage, paramString, "png");
// }
//
// static void saveToGIF(BufferedImage paramBufferedImage, String
// paramString) {
// saveToFile(paramBufferedImage, paramString, "gif");
// }
public static void saveToFile(BufferedImage paramBufferedImage, String path) {
String folder = path.substring(0, path.lastIndexOf(File.separator));
File t_fr = new File(folder);
if (!t_fr.exists()) {
t_fr.mkdirs();
}
String paramString = path.substring(path.lastIndexOf(".") + 1);
try {
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ImageUtil.encodeAndWrite(paramBufferedImage, paramString, bos, 96, 96);
bos.flush();
fos.flush();
bos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}package zj.image;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.imageio.ImageIO;
/**
*
* @author 張軍
* @date 2020年3月22日 下午4:34:15
* @version V1.0
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
*/
public class CodeUtil {
private static int width = 90;// 定義圖片的width
private static int height = 20;// 定義圖片的height
private static int codeCount = 4;// 定義圖片上顯示驗證碼的個數
private static int xx = 15;
private static int fontHeight = 18;
private static int codeY = 16;
private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
/**
* 生成一個map集合 code為生成的驗證碼 codePic為生成的驗證碼BufferedImage對象
*
* @return
*/
public static Map<String, Object> generateCodeAndPic() {
// 定義圖像buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Graphics2D gd = buffImg.createGraphics();
// Graphics2D gd = (Graphics2D) buffImg.getGraphics();
Graphics gd = buffImg.getGraphics();
// 創建一個隨機數生成器類
Random random = new Random();
// 將圖像填充為白色
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
// 創建字體,字體的大小應該根據圖片的高度來定。
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
// 設置字體。
gd.setFont(font);
// 畫邊框。
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
// 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。
gd.setColor(Color.BLACK);
for (int i = 0; i < 30; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}
// randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// 隨機產生codeCount數字的驗證碼。
for (int i = 0; i < codeCount; i++) {
// 得到隨機產生的驗證碼數字。
String code = String.valueOf(codeSequence[random.nextInt(36)]);
// 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用隨機產生的顏色將驗證碼繪制到圖像中。
gd.setColor(new Color(red, green, blue));
gd.drawString(code, (i + 1) * xx, codeY);
// 將產生的四個隨機數組合在一起。
randomCode.append(code);
}
Map<String, Object> map = new HashMap<String, Object>();
// 存放驗證碼
map.put("code", randomCode);
// 存放生成的驗證碼BufferedImage對象
map.put("codePic", buffImg);
return map;
}
public static void main(String[] args) throws Exception {
// 創建文件輸出流對象
OutputStream out = new FileOutputStream("D://img/" + System.currentTimeMillis() + ".jpg");
Map<String, Object> map = CodeUtil.generateCodeAndPic();
ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
System.out.println("驗證碼的值為:" + map.get("code"));
}
}
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://www.dlhighland.cn
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

