java生成二維碼,java生成二維碼的工具

本文目錄一覽:

java哪種方式生成二維碼比較好

最簡單的方式使用jQuery的qrCode插件

[html] view plain copy

!DOCTYPE html

html

head

meta charset=”UTF-8″

title使用JQueryQrCode生成二維碼/title

script type=”text/javascript” src=”./js/jquery-1.9.1.min.js”/script

script type=”text/javascript” src=”./js/qrcode.js”/script

/head

body

div id=”qrcode”/div

script type=”text/javascript”

//參數1表示圖像大小,取值範圍1-10;參數2表示質量,取值範圍’L’,’M’,’Q’,’H’

var content = “使用JQueryQrCode生成二維碼”

var qr = qrcode(8, ‘M’);

qr.addData(content);

qr.make();

/* var dom=document.createElement(‘DIV’);

dom.innerHTML = qr.createImgTag();

var element=document.getElementById(“qrcode”);

element.appendChild(dom); */

$(“#qrcode”).html(qr.createImgTag());

/script

/body

/html

如何用java生成二維碼

package common;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;

import jp.sourceforge.qrcode.exception.DecodingFailedException;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.Binarizer;

import com.google.zxing.BinaryBitmap;

import com.google.zxing.EncodeHintType;

import com.google.zxing.LuminanceSource;

import com.google.zxing.MultiFormatReader;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.NotFoundException;

import com.google.zxing.WriterException;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.common.HybridBinarizer;

import com.swetake.util.Qrcode;

/**

 * 二維碼生成工具類

 * @author Cloud

 * @data   2016-12-15

 * QRCode

 */

public class QRCodeUtil {

    

    //二維碼顏色

    private static final int BLACK = 0xFF000000;

    //二維碼顏色

    private static final int WHITE = 0xFFFFFFFF;

    /**

     * span style=”font-size:18px;font-weight:blod;”ZXing 方式生成二維碼/span

     * @param text    a href=”javascript:void();”二維碼內容/a

     * @param width    二維碼寬

     * @param height    二維碼高

     * @param outPutPath    二維碼生成保存路徑

     * @param imageType        二維碼生成格式

     */

    public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType){

        MapEncodeHintType, String his = new HashMapEncodeHintType, String();

        //設置編碼字符集

        his.put(EncodeHintType.CHARACTER_SET, “utf-8”);

        try {

            //1、生成二維碼

            BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);

            

            //2、獲取二維碼寬高

            int codeWidth = encode.getWidth();

            int codeHeight = encode.getHeight();

            

            //3、將二維碼放入緩衝流

            BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);

            for (int i = 0; i  codeWidth; i++) {

                for (int j = 0; j  codeHeight; j++) {

                    //4、循環將二維碼內容定入圖片

                    image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);

                }

            }

            File outPutImage = new File(outPutPath);

            //如果圖片不存在創建圖片

            if(!outPutImage.exists())

                outPutImage.createNewFile();

            //5、將二維碼寫入圖片

            ImageIO.write(image, imageType, outPutImage);

        } catch (WriterException e) {

            e.printStackTrace();

            System.out.println(“二維碼生成失敗”);

        } catch (IOException e) {

            e.printStackTrace();

            System.out.println(“生成二維碼圖片失敗”);

        }

    }

    

    /**

     * span style=”font-size:18px;font-weight:blod;”二維碼解析/span

     * @param analyzePath    二維碼路徑

     * @return

     * @throws IOException

     */

    @SuppressWarnings({ “rawtypes”, “unchecked” })

    public static Object zxingCodeAnalyze(String analyzePath) throws Exception{

        MultiFormatReader formatReader = new MultiFormatReader();

        Object result = null;

        try {

            File file = new File(analyzePath);

            if (!file.exists())

            {

                return “二維碼不存在”;

            }

            BufferedImage image = ImageIO.read(file);

            LuminanceSource source = new LuminanceSourceUtil(image);

            Binarizer binarizer = new HybridBinarizer(source);  

            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

            Map hints = new HashMap();

            hints.put(EncodeHintType.CHARACTER_SET, “UTF-8”);

            result = formatReader.decode(binaryBitmap, hints);

        } catch (NotFoundException e) {

            e.printStackTrace();

        }  

        return result;

    }

    

    /**

     * span style=”font-size:18px;font-weight:blod;”QRCode 方式生成二維碼/span

     * @param content    二維碼內容

     * @param imgPath    二維碼生成路徑

     * @param version    二維碼版本

     * @param isFlag    是否生成Logo圖片    為NULL不生成

     */

    public static void QRCodeCreate(String content, String imgPath, int version, String logoPath){

         try {  

            Qrcode qrcodeHandler = new Qrcode();  

            //設置二維碼排錯率,可選L(7%) M(15%) Q(25%) H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小    

            qrcodeHandler.setQrcodeErrorCorrect(‘M’);  

            //N代表數字,A代表字符a-Z,B代表其他字符  

            qrcodeHandler.setQrcodeEncodeMode(‘B’);  

            //版本1為21*21矩陣,版本每增1,二維碼的兩個邊長都增4;所以版本7為45*45的矩陣;最高版本為是40,是177*177的矩陣  

            qrcodeHandler.setQrcodeVersion(version);

            //根據版本計算尺寸

            int imgSize = 67 + 12 * (version – 1) ;  

            byte[] contentBytes = content.getBytes(“gb2312”);  

            BufferedImage bufImg = new BufferedImage(imgSize , imgSize ,BufferedImage.TYPE_INT_RGB);  

            Graphics2D gs = bufImg.createGraphics();  

            gs.setBackground(Color.WHITE);  

            gs.clearRect(0, 0, imgSize , imgSize);  

            // 設定圖像顏色  BLACK

            gs.setColor(Color.BLACK);

            // 設置偏移量 不設置可能導致解析出錯  

            int pixoff = 2;

            // 輸出內容  二維碼  

            if (contentBytes.length  0  contentBytes.length  130) {

                boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);

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

                    for (int j = 0; j  codeOut.length; j++) {

                        if (codeOut[j][i]) {  

                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);

                        }  

                    }  

                }  

            } else {  

                System.err.println(“QRCode content bytes length = ” + contentBytes.length + ” not in [ 0,130 ]. “);  

            }

           /* 判斷是否需要添加logo圖片 */

            if(logoPath != null){

                File icon = new File(logoPath);

                if(icon.exists()){

                    int width_4 = imgSize / 4;

                    int width_8 = width_4 / 2;

                    int height_4 = imgSize / 4;

                    int height_8 = height_4 / 2;

                    Image img = ImageIO.read(icon);

                    gs.drawImage(img, width_4 + width_8, height_4 + height_8,width_4,height_4, null);

                    gs.dispose();

                    bufImg.flush();

                }else{

                    System.out.println(“Error: login圖片還在在!”);

                }

            }

            gs.dispose();

            bufImg.flush();

            //創建二維碼文件

            File imgFile = new File(imgPath);

            if(!imgFile.exists())

                imgFile.createNewFile();

            //根據生成圖片獲取圖片

            String imgType = imgPath.substring(imgPath.lastIndexOf(“.”) + 1, imgPath.length());

            // 生成二維碼QRCode圖片  

            ImageIO.write(bufImg, imgType, imgFile);  

         } catch (Exception e) {  

             e.printStackTrace();  

         }

    }

    

    /**

     * span style=”font-size:18px;font-weight:blod;”QRCode二維碼解析/span

     * @param codePath    二維碼路徑

     * @return    解析結果

     */

    public static String QRCodeAnalyze(String codePath) {

        File imageFile = new File(codePath);

        BufferedImage bufImg = null;  

        String decodedData = null;  

        try {

            if(!imageFile.exists())

                return “二維碼不存在”;

            bufImg = ImageIO.read(imageFile);

          

            QRCodeDecoder decoder = new QRCodeDecoder();  

            decodedData = new String(decoder.decode(new ImageUtil(bufImg)), “gb2312”);  

        } catch (IOException e) {  

            System.out.println(“Error: ” + e.getMessage());  

            e.printStackTrace();  

        } catch (DecodingFailedException dfe) {  

            System.out.println(“Error: ” + dfe.getMessage());  

            dfe.printStackTrace();  

        }

        return decodedData;

    }

}

3、最後貼測試代碼:

package test;

import java.awt.image.BufferedImage;

import java.io.InputStream;

import java.net.URL;

import javax.imageio.ImageIO;

import common.ImageUtil;

import common.QRCodeUtil;

import jp.sourceforge.qrcode.QRCodeDecoder;

/**

 * 二維碼生成測試類

 * @author Cloud

 * @data   2016-11-21

 * QRCodeTest

 */

public class QRCodeTest {

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

        /**

         *    QRcode 二維碼生成測試

         *    QRCodeUtil.QRCodeCreate(“”, “E://qrcode.jpg”, 15, “E://icon.png”);

         */

        /**

         *     QRcode 二維碼解析測試

         *    String qrcodeAnalyze = QRCodeUtil.QRCodeAnalyze(“E://qrcode.jpg”);

         */

        /**

         * ZXingCode 二維碼生成測試

         * QRCodeUtil.zxingCodeCreate(“”, 300, 300, “E://zxingcode.jpg”, “jpg”);

         */

        /**

         * ZxingCode 二維碼解析

         *    String zxingAnalyze =  QRCodeUtil.zxingCodeAnalyze(“E://zxingcode.jpg”).toString();

         */

        System.out.println(“success”);

    }

}

我已經用java生成了一個二維碼了,怎樣讓掃描二維碼後,讀取到一個word文檔,大神。

不用這麼麻煩,直接使用二維碼生成器就行了,只要上傳文檔,自動直接生成二維碼。方便有快捷。

推薦一款目前市面上比較不錯的二維碼生成工具。

登錄網站進入操作後台。

點擊添加二維碼內容。(可查看管理製作過的二維碼,如果是第一次使用直接顯示第三步的編輯頁面。

3.編輯二維碼里的內容,上傳你的文檔。

上傳完成後保存即可生成二維碼,並且生成的二維碼內容支持隨時修改,原碼不變!

希望對你有幫助!

怎麼用java代碼把一個鏈接生成二維碼

參考代碼

import java.io.*;

import java.util.Date;

import java.awt.*;

import java.awt.image.*;

import javax.imageio.*;

public class QRCodeEncoderTest

{

/** Creates a new instance of QRCodeEncoderTest */

public QRCodeEncoderTest()

{

}

public static void create_image(String sms_info)throws Exception{

try{

qrcode testQrcode =new qrcode();

testQrcode.setQrcodeErrorCorrect(‘M’);

testQrcode.setQrcodeEncodeMode(‘B’);

testQrcode.setQrcodeVersion(7);

String testString = sms_info;

byte[] d = testString.getBytes(“gbk”);

System.out.println(d.length);

//BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_INT_RGB);

BufferedImage bi = new BufferedImage(98, 98,

怎麼使用java生成DataMatrix格式的二維碼?

參考:

import com.spire.barcode.BarCodeGenerator;

import com.spire.barcode.BarCodeType;

import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

public class CreateDataMatrix {

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

      //生成BarcodeSettings實例

      BarcodeSettings settings = new BarcodeSettings();

      //設置條形碼類型為DataMatrix

      settings.setType(BarCodeType.Data_Matrix);

      //設置條形碼模型寬度

      settings.setX(1.5f);

      //設置數據和顯示文本

      settings.setData(“ABC 123456789ABC 123456789ABC 123456789”);

      settings.setData2D(“ABC 123456789ABC 123456789ABC 123456789”);

      //創建BarCodeGenerator實例

      BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);

      //根據settings生成圖像數據,保存至BufferedImage實例

      BufferedImage bufferedImage = barCodeGenerator.generateImage();

      //保存為PNG圖片

      ImageIO.write(bufferedImage, “png”, new File(“DataMatrix.png”));

      System.out.println(“Complete!”);

  }

}

用到了spire.barcode for java庫

java 生成二維碼後如何給該二維碼添加信息

java可使用zxing生成二維碼並為其添加信息。

以下是詳細步驟:

1、創建MatrixToImageWriter類

import com.google.zxing.common.BitMatrix;   

 import javax.imageio.ImageIO;  

 import java.io.File;  

 import java.io.OutputStream;  

 import java.io.IOException;  

 import java.awt.image.BufferedImage;  

    

    

 public final class MatrixToImageWriter {  

    

   private static final int BLACK = 0xFF000000;  

   private static final int WHITE = 0xFFFFFFFF;  

    

   private MatrixToImageWriter() {}  

    

      

   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;  

   }  

    

      

   public static void writeToFile(BitMatrix matrix, String format, File file)  

       throws IOException {  

     BufferedImage image = toBufferedImage(matrix);  

     if (!ImageIO.write(image, format, file)) {  

       throw new IOException(“Could not write an image of format ” + format + ” to ” + file);  

     }  

   }  

    

      

   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)  

       throws IOException {  

     BufferedImage image = toBufferedImage(matrix);  

     if (!ImageIO.write(image, format, stream)) {  

       throw new IOException(“Could not write an image of format ” + format);  

     }  

   }  

    

 }

2、生成二維碼並添加信息

import java.io.File;  

import java.util.Hashtable;  

   

import com.google.zxing.BarcodeFormat;  

import com.google.zxing.EncodeHintType;  

import com.google.zxing.MultiFormatWriter;  

import com.google.zxing.WriterException;  

import com.google.zxing.common.BitMatrix;  

   

public class Test {  

   

    /** 

     * @param args 

     * @throws Exception  

     */ 

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

        String text = “”;  

        int width = 300;  

        int height = 300;  

        //二維碼的圖片格式  

        String format = “gif”;  

        Hashtable hints = new Hashtable();  

        //內容所使用編碼  

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

        BitMatrix bitMatrix = new MultiFormatWriter().encode(text,  

                BarcodeFormat.QR_CODE, width, height, hints);  

        //生成二維碼  

        File outputFile = new File(“d:”+File.separator+”new.gif”);  

        MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);  

   

    }  

   

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/192721.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-01 10:29
下一篇 2024-12-01 10:29

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論