java 產生 二維碼 和jquery 產生二維碼

來源:互聯網
上載者:User

標籤:ref   try   buffer   exception   cep   mina   下載   top   iter   

產生二維碼

Java 產生二維碼: 思路為拿到jar 包知道裡面的方法使用Step one ; 在https://github.com/zxing 中下載(點擊網頁中名為 zxing 的a標籤,跳轉到源碼頁面,點擊release 查看所有發布的源碼,下載zip壓縮檔 Step two:  解壓檔案後開啟檔案夾,將core包和javase包 中的com包拷貝到一java項目src目錄下。右鍵匯出 jar file  得到一個二維碼開發的jar包Step three: 進行二維碼製作

 

 

 

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class CreateQRCode {

    //產生二維碼
    public static void main(String[]args) {


        int width=  300;
        int height = 300;
        String format = "png";
        String content = "姓名:馮嘉偉";


        //定義二維碼參數
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);






        try {
            BitMatrix  bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
            Path file = new File("D:/code/img.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);//產生二維碼
        } catch(Exception e) {
            // TODOAuto-generated catch block
           
e.printStackTrace();
        }





    }

}

 

二維碼解析器
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
//解析二維碼
public class ReadQRcode {

    public static void main(String[] args) {




        try {

            MultiFormatReader formatReader = new MultiFormatReader();

            File file = new File("D:/code/img.png");
            BufferedImage image = ImageIO.read(file);

            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

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

            Result result = formatReader.decode(binaryBitmap,hints);

            System.out.println("解析結果"+result.toString());
            System.out.println("二維碼格式類型"+result.getBarcodeFormat());
            System.out.println("二維碼常值內容"+result.getText());

        } catch (Exception e) {
            // TODO Auto-generated catch block
           
e.printStackTrace();
        }

    }


}
通過JQuery產生二維碼第一步:https://github.com/jeromeetienne/jquery-qrcode第二步:release 下載 jquery 包第三步: 在前端匯入js 包

注意:<script type="text/javascript" src="<%request.getContextPath()%>">/js/jquery.min.js</script>
<script type="text/javascript"src="<%request.getContextPath()%>">/js/jquery.qrcode.js</script>

   這兩個js包的順序不能顛倒,否則會失敗

詳細的使用在一步網址中有how to use it

<script type="text/javascript" src="<%request.getContextPath()%>">/js/jquery.min.js</script>
<script type="text/javascript" src="<%request.getContextPath()%>">/js/jquery.qrcode.js</script>
<body>
    "產生的二維碼如下:"<br>
    <div id="qrcode">
    </div>
    <script type="text/javascript">
        jQuery("#qrcode").qrcode("www.baidu.com");
    </script>
</body>

 

java 產生 二維碼 和jquery 產生二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.