java根據條碼字串產生條碼圖

來源:互聯網
上載者:User
 
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);//}    }


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.