二维码

 <label>配件编号</label>  
                   <input class="easyui-validatebox" type="text" id="pjxxPjbh" name="pjxxPjbh"  data-options="required:true"  >

 

<div class="fitem" style="float: left;">
                              <div class="fitem" >
                            <label>二维码 </label>
                               <input type="checkbox" value="" id="tp">图片
                               <input type="checkbox" value="" id="bh">编号
                               <input type="checkbox" value="" id="mc">名称
                        </div>
                        <div class="fitem">
                            <label></label>
                            <input type="button" value="生成二维码" id="getCode" onclick="getCodeFunction()">
                        </div>
                        </div>
                        
                        <div class="fitem" >
                            <div class="fitem" >
                                <img id="2code" src="" style="width:100px;height:80px;padding-left:100px">
                            </div>
                            <div class="fitem" style="float: right;margin-right: 140px" >
                                <a>下载</a>
                            </div>
                        </div>           

 

function getCodeFunction(){
            var pjxxPjbh;
            if($('#bh').prop("checked")){
                pjxxPjbh = $("#pjxxPjbh").val();
                
            alert(pjxxPjbh);
            $.ajax({
                type:'POST',
                url:'${msUrl}/pjPjxx/getCode.do?pjxxPjbh='+pjxxPjbh+'',
                cache: false,
                success: function(data){
                    
                    $("#2code").attr('src','http://127.0.0.1:8080/INSIGMA_WEB/WebRoot/images/'+$("#pjxxPjbh").val()+'.gif');
                }
            })
            }else{
                alert('请选择二维码生成方式!');
            }

        };

 

    
    @RequestMapping("/getCode")
    public void getCode(String pjxxPjbh, HttpServletResponse response, HttpServletRequest request)
            throws Exception {
        File destImageFile = create2CodeImage(pjxxPjbh,request);
        pjPjxxService.get2CodeImage(pjxxPjbh, destImageFile);
        //HtmlUtil.writerJson(response, destImageFile.toString());
        System.out.println(destImageFile);
    
    }

 

//service
    public File create2CodeImage(String pjbh,HttpServletRequest request){

        String filePath = request.getSession().getServletContext()
                .getRealPath("")
                + "/WebRoot/images" + "/" + pjbh + ".gif";

        File destImageFile = new File(filePath);
        File destImageParentFile = destImageFile.getParentFile();
        if (!destImageParentFile.isDirectory()) {
            destImageParentFile.mkdirs();
        }
        
        return destImageFile;
    }

 

/**
     * 最终调用该方法生成二维码图片
     * 
     * @param url
     *            要生成二维码的url
     * @param imgPath
     *            二维码生成的绝对路径
     * @param logoPath
     *            二维码中间logo绝对地址
     * @param codeWidth
     *            二维码宽度
     * @param codeHeight
     *            二维码长度
     * @param logoLeftMargin
     *            logo图左边距离
     * @param logoTopMargin
     *            logo图顶部距离
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public void get2CodeImage(String pjCode, File imgPath) throws Exception {
        int codeWidth = 300;
        int codeHeight = 300;
        String format = "png";
        @SuppressWarnings("rawtypes")
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 矫错级别
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

        BitMatrix bitMatrix = new MultiFormatWriter().encode(pjCode,
                BarcodeFormat.QR_CODE, codeWidth, codeHeight, hints);
        
         writeToFile(bitMatrix, format, imgPath);  

    }

    /**
     * 
     * @param matrix
     *            二维码矩阵相关
     * @param format
     *            二维码图片格式
     * @param file
     *            二维码图片文件
     * @throws IOException
     */
    public static void writeToFile(BitMatrix matrix, String format, File file
            )
            throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        
        ImageIO.write(image, "gif", new File(file.getPath()));  

    }

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }

 

 

转载于:https://my.oschina.net/u/2535621/blog/686433

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值