package com.zte.evs.ebill.common;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
/**
* 类头编号:
* 类 名 称:BarCodeImage
* 内容摘要:根据条码字符串生成条码图
*/
public class BarCodeImage {
private int barWidth;//设置条码宽度
private int barHeight;//设置条码高度
private int left;//设置左边距
private int top;//设置条码距顶部距离
/**
* @param logicalBarCode 逻辑码
* @param g 画笔
* */
private void buildImg(String logicalBarCode, Graphics g)
{
for(int i = 0; i < logicalBarCode.length(); i++)
{
char cc = logicalBarCode.charAt(i);
if(cc == '1')
paint(g, Color.black, left);
else
paint(g, Color.white, left);
left += barWidth;
}
}
/**
* @param g 画笔
* @param color 条码条颜色
* @param left 条码条左边距
* */
private void paint(Graphics g, Color color, int left)
{
g.setColor(color);
g.fillRect(left, top, barWidth, barHeight);
}
/**
* 生成条码图入口方法
* @param codeStr: 条码字符串
* @param barWidth: 条码宽度
* @param barHeight: 条码高度
* @param left: 条码左边距或右边距
* @param top: 条码顶边距或底边距
* @return 返回BufferedImage对象
* */
public BufferedImage buildCodeBarImg(String codeStr,int barWidth, int barHeight, int left, int top)
{
BufferedImage image=null;
Graphics g=null;
try
{
boolean isRigth=checkParameter(codeStr,barWidth,barHeight,left,top);
if(isRigth)
{
BarCode128 code128=new BarCode128();
String logicalBarCode= code128.getLogicalBarCode(codeStr);
int imageWidth=logicalBarCode.length()*barWidth+(2*left);
int imageHieght=barHeight+(2*top);
image = new BufferedImage(imageWidth, imageHieght, BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, imageWidth, imageHieght);
buildImg(logicalBarCode,g);//构建条码图
}
else
{
image = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.white);
g.drawString("创建条码图失败!", 20, 80);
}
}
catch (Exception e)
{
image = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.white);
g.drawString("创建条码图失败!", 20, 80);
e.printStackTrace();
}
return image;
}
public BufferedImage buildDataMaxImg(String codeStr,int barWidth, int barHeight, int left, int top)
{
BufferedImage image=null;
Graphics g=null;
try
{
boolean isRigth=checkParameter(codeStr,barWidth,barHeight,left,top);
if(isRigth)
{
image = new BufferedImage(barWidth, barHeight,
BufferedImage.TYPE_INT_RGB);
BitMatrix bitMatrix = new QRCodeWriter().encode(codeStr, BarcodeFormat.QR_CODE,
barWidth, barHeight);
for (int x = 0; x < barWidth; x++) {
for (int y = 0; y < barHeight; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) == true ?BarCodeServlet.BLACK
: BarCodeServlet.WHITE);
}
}
}
else
{
image = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.white);
g.drawString("创建二维码图失败!", 20, 80);
}
}
catch (Exception e)
{
image = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.white);
g.drawString("创建二维码图失败!", 20, 80);
e.printStackTrace();
}
return image;
}
/**
* 检查参数的合法性
* @param codeStr: 条码字符串
* @param barWidth: 条码宽度
* @param barHeight: 条码高度
* @param left: 条码左边距或右边距
* @param top: 条码顶边距或底边距
* @return boolean
* */
private boolean checkParameter(String codeStr,int barWidth, int barHeight, int left, int top)
{
boolean isRigth=true;
try
{
this.barWidth = barWidth;
this.barHeight = barHeight;
this.left = left;
this.top = top;
if(codeStr==null||codeStr.length()<=0)
{
isRigth=false;
throw new Exception("条码字符串不能为空!");
}
else if(barWidth<=0)
{
isRigth=false;
throw new Exception("条码线宽度不能小于等于0!");
}
else if(barHeight<=0)
{
isRigth=false;
throw new Exception("条码线高度不能小于等于0!");
}
else if(left<0)
{
isRigth=false;
throw new Exception("条码线左边距不能小于0!");
}
else if(top<0)
{
isRigth=false;
throw new Exception("条码线顶边距不能小于0!");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return isRigth;
}
//
// public static void main(String[] args)throws Exception {
//
//
// BarCodeImage barCodeImage=new BarCodeImage();
// BufferedImage image=barCodeImage.buildCodeBarImg("fdsafsd",2,100,0,0);
// JPEGCodec.createJPEGEncoder(new FileOutputStream("C:/jamie7.jpg")).encode(image);
// }
}