zoukankan      html  css  js  c++  java
  • java生产条形码

    准备:jbarcode-0.2.8.jar,下载地址:jbarcode-0.2.8.zip

    jsp:

    <img src="<%=basePath%>barcode/showBarCode" alt="" style=" 200px; height: 150px;">

    controller

    import java.io.BufferedOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import util.OneBarcodeUtil;
    
    @Controller
    @RequestMapping("/barcode")
    public class BarcodeController {
        @RequestMapping("/")
        public String index(){
            return "barcode";
        }
        @RequestMapping("/showBarCode")
        public void showIMG(HttpServletRequest req,HttpServletResponse resp) throws Exception{
            OneBarcodeUtil obu = new OneBarcodeUtil();
            byte[] bs = obu.createBarcodeDefault("123123123");//这里是条码号
             InputStream in = new ByteArrayInputStream(bs);// 业务逻辑取得图片的byte[]
             BufferedOutputStream bout = new BufferedOutputStream(resp.getOutputStream()); 
             //从输入流到输出流  
            try {    
              //  byte b[] = new byte[1024];    
                int len = in.read(bs);    
                while (len > 0) {    
                    bout.write(bs, 0, len);    
                    len = in.read(bs);    
                }    
            } catch (Exception e) {    
                throw e;     
            } finally {    
                bout.close();    
                in.close();    
            }  
        }
    }
    View Code

    工具类

    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.lang.reflect.Constructor;
    
    import org.jbarcode.JBarcode;
    import org.jbarcode.encode.BarcodeEncoder;
    import org.jbarcode.encode.Code39Encoder;
    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;
    
    public class OneBarcodeUtil { 
    
        
           
        /** 
         * 生成一维码 
         * @param value 值 
         * @return 
         */  
        public static byte[] createBarcodeDefault(String value){  
            return createBarcode(Code39Encoder.class,value,false);  
        }  
        
        
      //产生一维码图片  
        public static byte[] createBarcode(Class<?> clazz,String value,boolean checkDigit){  
              try{  
                  JBarcode localJBarcode = new JBarcode(getInstance(clazz),WidthCodedPainter.getInstance(),EAN13TextPainter.getInstance());    
                  localJBarcode.setPainter(WideRatioCodedPainter.getInstance());    
                  localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());    
                  localJBarcode.setCheckDigit(checkDigit);  
                  localJBarcode.setShowCheckDigit(checkDigit);  
                  return getBytes(localJBarcode.createBarcode(value));    
              }catch (Exception e) {  
                e.printStackTrace();  
                return null;  
            }  
        }  
        
        
        //获取单例的对象  
        private static BarcodeEncoder getInstance(Class<?> clazz) throws Exception{  
            Constructor<?>[] constructors=clazz.getDeclaredConstructors();  
            Constructor<?> privateConstructor = constructors[0];  
            privateConstructor.setAccessible(true);  
            return (BarcodeEncoder)privateConstructor.newInstance();  
              
        }  
          
        //获取图片字节码数组  
        private static byte[] getBytes(BufferedImage paramBufferedImage) throws IOException{    
              return ImageUtil.encode(paramBufferedImage,"jpeg", 96, 96);  
        }    
        
        
    }
    View Code

     启动项目访问:http://localhost:8080/wxPublic/barcode/

    生产条形码:

  • 相关阅读:
    界面和效果
    第一篇博客
    Java作业(六)
    Java学习(五)
    Java学习(四)
    JAVA学习(三)
    Java学习(二)
    Java学习心得
    音乐播放器项目计划进度安排
    课程设计 高云鹏 郑帅康 张程 姬泽辉
  • 原文地址:https://www.cnblogs.com/yanan7890/p/8875300.html
Copyright © 2011-2022 走看看