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);//} }