所有工具类
条形码类型及常见条形码介绍条形码或条码(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.zhangjunbk.com">张军个人网站</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.zhangjunbk.com">张军个人网站</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.zhangjunbk.com

