java-简单二维码制作

 

main测试方法  方法一

public static void main(String[] args) throws Exception {

    final int width = 300;

    final int height = 300;

    final String format = "png";

    final String content = "https://www.cnblogs.com/aston/p/7501895.html";

    //定义二维码的参数

    HashMap hints = new HashMap();

    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

    hints.put(EncodeHintType.MARGIN, 2);

    Path file =null;

    //生成二维码

    try {

        //OutputStream stream = new OutputStreamWriter();

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        System.out.println(bitMatrix + "   -----------------------------------------------------------------------------------------");

        file = new File("F:/img.png").toPath();

        MatrixToImageWriter.writeToPath(bitMatrix, format, file);

        //MatrixToImageWriter.writeToStream(bitMatrix, format, stream);

 

    } catch (Exception e) {

    }

    //return file;

}

实际案例  方法二

所需jar包maven地址

<dependency>

    <groupId>net.glxn.qrgen</groupId>

    <artifactId>javase</artifactId>

    <version>2.0</version>

</dependency>

 

//二维码制作方法

@Security(name = "二维码制作", asUrl = "downImage")

@RequestMapping("downImage/{id}")

public void play(ModelMap modelMap, @PathVariable Long id, HttpServletRequest request, HttpServletResponse response)throws Exception {

    Video video = videoService.findById(id);

    String domain = WebContext.getDomain();//获取域名

    String  path = domain+"/edu/manage/video/addPlay/"+id ;//视频播放地址

    InputStream inputStream = img(path);

    OutputStream outputStream= response.getOutputStream();

    response.setHeader("Content-Disposition",//此方法是在浏览器直接下在图片,给浏览器返回一个文件,如果浏览器发现是一个文件就会自动下载

            "attachment; filename=" + java.net.URLEncoder.encode(video.getName()+".png", "UTF-8"));

    byte[] bytes=new byte[1024];

    while ((inputStream.read(bytes))>0){

       outputStream.write(bytes);

    }

   outputStream.flush();

   outputStream.close();

}

 

 

public InputStream img(String content){

    final int width = 300;

    final int height = 300;

    final String format = "png";

    //final String content = "https://www.cnblogs.com/aston/p/7501895.html";

    //定义二维码的参数

    HashMap hints = new HashMap();

    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

    hints.put(EncodeHintType.MARGIN, 2);

    Path file =null;

    //生成二维码

    try {

        //OutputStream stream = new OutputStreamWriter();

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        file = new File("F:/img.png").toPath();

        MatrixToImageWriter.writeToPath(bitMatrix, format, file);

        return new FileInputStream(file.toFile());

    } catch (Exception e) {

    }

    return null;

}

下载图片例如下所示:

 

 

获取电脑磁盘盘符

File[] roots = File.listRoots();

File root = roots[0];  //获取磁盘的第一个盘符

System.out.println(root + "  root  ----------------------------------------");

for (int i =0; i < roots.length; i++) {

    System.out.println(roots[i] + "   ++++++++++++++++++++++++++++++");

}

 

手机微信扫描二维码提示跳转到浏览器里面

<!DOCTYPE html>

<html>

   <head>

      <meta charset="utf-8" />

      <meta name="renderer" content="webkit" />

      <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

      <meta name="format-detection" content="telephone=no,email=no,date=no,address=no" />

      <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

      <meta name="keywords" content="" />

      <meta name="description" content="" />

      <meta name="author" content="Mr Shen,sgq625@126.com" />

      <meta name="revised" content="Mr Shen,2019-03-15 10:20:30" />

      <meta name="generator" content="HBuilder 9.1.29" />

      <title>shiping</title>

      <link href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css" rel="stylesheet">

      <link href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css" rel="stylesheet">

        <link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/1.9.2/skins/default/index.css"/>

        <script type="text/javascript" src="https://g.alicdn.com/de/prismplayer/1.9.2/prism.js"></script>

        <script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>

        <script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>

 

      <style type="text/css">

         body{

            min-width: 320px;

            max-width: 640px;

            margin: 0 auto;

         }

         

         .weui-panel__hd{

            color: #333;

            font-size: 18px;

            padding-top: 0;

         }    

                                                                                                                                                                                                                                                                                                                                

         .weui-cells:after,

         .weui-cells:before,

         .weui-cell:after,

         .weui-cell:before {

            display: none;

         }

 

         .weui-panel:after,

         .weui-panel:before{

            display: none;

         }

      </style>

   </head>

   <body>

    <div class="" style="max-width: 640px;min-width: 320px;width: 100%;height: auto;margin-top: 20px">

 

        <div id="" style="line-height: 24px;text-align: center; margin-bottom: 15px; font-size: 20px">

         ${(video.name)!}

        </div>

 

        <div id="J_prismPlayer" style="max-width: 640px;min-width: 320px;width: 100%; height: auto;margin: 0 auto;" controls>

        </div>

    </div>

      <div class="weui-panel weui-panel_access">

         <div class="weui-panel__hd">相关视频推荐</div>

         <div class="weui-panel__bd"  id="J-call-app">

                <#list videoList as video>

                    <#--<a href="${contextPath}/video/play/${video.id};" class="weui-media-box weui-media-box_appmsg">-->

                    <a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg">

                        <div class="weui-media-box__hd">

                            <img style="width: 60px; height: 60px" src="${(video.imagePath)!}">

                        </div>

                        <div class="weui-media-box__bd">

                            <h4 class="weui-media-box__title">${(video.name)!}</h4>

                            <p class="weui-media-box__desc">${(video.description)!}</p>

                        </div>

                    </a>

                </#list>

         </div>

      </div>

 

    <#--下载app地址-->

    <input id="J-download-app" type="hidden" name="storeurl" value="http://m.chanyouji.cn/apk/chanyouji-2.2.0.apk">

 

    <script>

        var player = new prismplayer({//视频播放插件

            id: "J_prismPlayer",

            autoplay: true,

            width: "auto",

            //height: "auto",

            vid: "${(video.videoId)!}",

            playauth: "${(playAuth)!}",

        });

 

 

        $("#J_prismPlayer").find("video").each(function(i){

            $(this).attr({"controls":"controls","loop":"loop"});

        }); //视频添加全屏进度条声音及自动播放等控件

        $(".prism-controlbar").hide(); //去掉时间重复

        $("#J_prismPlayer").click(function () {

            $(".prism-controlbar").hide();

        })//避免点击视频时出现两个时间

 

        $("video").trigger("play");//for auto play

        $("video").addClass('pause');//for check pause or play add a class

        $('video').click(function() {

            if ($(this).hasClass('pause')) {

                $("video").trigger("play");

                $(this).removeClass('pause');

                $(this).addClass('play');

            } else {

                $("video").trigger("pause");

                $(this).removeClass('play');

                $(this).addClass('pause');

            }

        })//添加点击视频就能播放暂停

 

        $("#J-call-app").click(function () {

            adds();

        });

 

        function adds(){

            var ua = navigator.userAgent.toLowerCase(); //识别用户的客户端,可以判断是什么软件

            var t;

            var config = {

                /*scheme:必须*/

                scheme_IOS: 'cundong://',  //苹果

                scheme_Adr: 'cundong://splash',  //安卓

                download_url: document.getElementById('J-download-app').value, //下载地址

                timeout: 600

            };

            openclient();

            function openclient() {

                var startTime = Date.now();

                var ifr = document.createElement('iframe');

                ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS : config.scheme_Adr;

                ifr.style.display = 'none';

                document.body.appendChild(ifr);

 

                var t = setTimeout(function() {

                    var endTime = Date.now();

                    if ( endTime - startTime  < (config.timeout + 200)) {

                        if(ua.match(/MicroMessenger/i)=="micromessenger"){ //判断是否是微信中扫描的二维码

                            alert("请点击右上角 ... 按钮选择在浏览器打开");

                            return;

                        }

                        if(ua.indexOf('os') > 0){//大于0是苹果端

                            alert("苹果")

                        }

                        if(ua.indexOf('os') < 0){//小于0是安卓端 -1

                            alert("安卓");

                        }

                        window.location = config.download_url; //在浏览器中直接下载

                    }

                }, config.timeout);

            }

            window.addEventListener("DOMContentLoaded", function(){

                document.getElementById("J-call-app").addEventListener('click',openclient,false);

 

            }, false);

        }

    </script>

   </body>

</html>

posted @ 2020-12-06 10:39  My--style  阅读(65)  评论(0)    收藏  举报