



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
package test;import java.awt.image.BufferedImage;import java.awt.Graphics;import java.awt.Color;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import com.sun.image.codec.jpeg.JPEGImageEncoder;import com.sun.image.codec.jpeg.JPEGCodec;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import com.sun.image.codec.jpeg.ImageFormatException;import java.io.IOException;import java.util.Random;/* */* 符合BarCode 39规范的条码图像生成器* author ChenLiang & LiGuang*/public class BarCodeImage private static final int rate = 3; /条码宽条与窄条宽度之比 private int m_nNarrowWidth; /窄条的宽度像素数 private int m_nImageHeight; /条码的高度像素数 private boolean m_bRotato; /输出的图像是否需要先旋转 /* */* * 根据strCodes传入的字符串,生成符合BarCode 39规范的JPEG输出流; * param nNarrowWidth * param nImageHeight */* public BarCode39ImageBuilder(int nNarrowWidth, int nImageHeight) m_nNarrowWidth = nNarrowWidth; m_nImageHeight = nImageHeight; m_bRotato = false; */* */* * 构造函数,默认窄条宽为4像素,条码高度是100像素; */* public BarCode39ImageBuilder() this(1, 33); */ /* */* * 设置是否需要将结果图像在输出之前进行旋转;系统默认是不旋转 * param b true:旋转,false:不旋转 */ public void setRotato(boolean b) m_bRotato = b; /* */* * 生成相应的Bar Code图像,格式以jpeg格式的输出流; * param strCodes 要生成条码的字符串,注意该字符串需要包含首尾的两个星号 * param out 接结果的输出流 * throws IOException * throws ImageFormatException * throws IOException */ public void getImage(String strCodes, String path) throws ImageFormatException, IOException String fileBar= System.getProperty(file.separator); File myPNG = new File(path); OutputStream out = new FileOutputStream(myPNG); if (null=strCodes | null=out | 0=strCodes.length() return; int nImageWidth = (strCodes.length() * (3 * rate + 7) * m_nNarrowWidth); BufferedImage bi = new BufferedImage(nImageWidth, m_nImageHeight+13, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, nImageWidth, m_nImageHeight); g.setColor(Color.BLACK); int startx = 0; for (int i = 0; i strCodes.length(); i+) startx = drawOneChar(g, startx, strCodes.charAt(i); g.setColor(Color.WHITE); g.fillRect(0, 33, nImageWidth, 13); g.setColor(Color.BLACK); g.drawString(strCodes, nImageWidth/4, 45); if (m_bRotato) /旋转 bi = flipX2Y(bi); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); while(true) /图片生成完毕后,向下运行 if(myPNG.exists() /System.out.println(-over); break; /* */* * 辅助方法,绘制一个字符 * param g 画布 * param x 起始位置 * param ch 要绘制的字符 * return 下一个要绘制字符的位置 */ private int drawOneChar(Graphics g, int x, char ch) short sCode = getCharCode(ch); for (int i = 0; i i) & sCode) != 0) width *= rate; if (i & 0x1) = 0) g.fillRect(x, 0, width, m_nImageHeight); x += width; return x + m_nNarrowWidth; /* */* * 图像翻转,即X与Y方向互换 * param in 源图像 * return 翻转后的图像 */ private BufferedImage flipX2Y(BufferedImage in) BufferedImage out = new BufferedImage(in.getHeight(), in.getWidth(), in.getType(); /请查看JDK AffineTransform affineTransform = new AffineTransform(0, 1, 1, 0, 0, 0); AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); return affineTransformOp.filter(in, out); / 原来旋转的版本,仅供参考 / private BufferedImage rotate(BufferedImage in) / / int width = in.getWidth(); / int height = in.getHeight(); / BufferedImage out = new BufferedImage(height, width, in.getType(); / / AffineTransform affineTransform = AffineTransform.getRotateInstance(Math.toRadians(90); / affineTransform.translate(0, -height); / / AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, / AffineTransformOp.TYPE_NEAREST_NEIGHBOR); / affineTransformOp.filter(in, out); / return out; / /Code39符号表 private static final char m_chars = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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, -, ., , *, $, /, +, % ; /Code39符号所对应的码表,16进制编码 n为0 w为1 private static final short m_codes = 0x34, 0x121, 0x61, 0x160, 0x31, 0x130, 0x70, 0x25, 0x124, 0x64, 0x109, 0x49, 0x148, 0x19, 0x118, 0x58, 0xd, 0x10c, 0x4c, 0x1c, 0x103, 0x43, 0x142, 0x13, 0x112, 0x52, 0x7, 0x106, 0x46, 0x16, 0x181, 0xc1, 0x1c0, 0x91, 0x190, 0xd0, 0x85, 0x184, 0xc4, 0x94, 0xa8, 0xa2, 0x8a, 0x2a ; /* */* * 通过码表符号获得码表字符 * param ch 码表符号 * return 码表值 */ private static short getCharCode(char ch) for (int i = 0; i m_chars.length; i+) if (ch = m_charsi) return m_codesi; return 0; /生成条形码的数据-8位随机字符串 public String barcodeData() String dataFinal = null; /String dataTemp = longDateFormat.format(new java.util.Date(); Random r = new Random(); String strRandom = +r.nextInt(9)+r.nextInt(9)+r.nextInt(9)+r.nextInt(9)+; dataFinal = strRandom; /d
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 新零售模式线上线下融合方案
- 兼职工作协议的注意事项
- 企业市场分析中的数据挖掘技术
- 2025年世界顶级几何题目及答案
- 浸出生产车间考试试题及答案
- 分数通分试题及答案
- 2025年善意的谎言辩论材料
- 2025年山西省晋中市事业单位工勤技能考试题库及答案
- CN120246490A 仓储系统、货架机器人、换电机器人及换电方法 (杭州海康机器人股份有限公司)
- CN120127918B 一种伺服电机定子冲片自动化叠片机 (江苏联博精密科技股份有限公司)
- 《社会工作》课件
- 中国工程总承包行业市场深度调研及发展趋势与投资前景研究报告2025-2028版
- 老年髋部骨折围术期护理临床实践专家共识2024版解读
- 中国胎教行业市场调研分析及投资前景预测报告
- 储能电站施工方案新建项目
- 《GNSS测量技术与应用》 课件 4.10GNSS控制测量技术总结
- DB32-T 4987-2024 桥梁轻量化监测系统建设规范
- 电梯自动化与智能化技术的前沿探索
- 2025年万达商业地产租赁合同标准版
- 湖南造价咨询合同范本(2篇)
- DB21-T 4079-2024 畜禽养殖场污水贮存设施建设规范
评论
0/150
提交评论