二维码java,二维码java生成

本文目录一览:

如何用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二维码只能扫码一次

只能扫一次。java扫过一次之后就无法展示内容,提交设置后这个二维码就只能扫一次。二维码又称二维条码,常见的二维码为QRCode,QR全称QuickResponse,是一种编码方式。

怎么使用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 如何完成二维码的制作

参考以下代码:

//创建BarcodeSettings实例

BarcodeSettings settings = new BarcodeSettings();

//设置条码类型为QR二维码

settings.setType(BarCodeType.QR_Code);       

//设置二维码数据

settings.setData(“Hello 123456789”);

//设置二维码显示数据

settings.setData2D(“Hello 123456789”);     

//设置数据类型

settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);

//设置二维码模型宽度

settings.setX(1.0f);

//设置二维码纠错级别

settings.setQRCodeECL(QRCodeECL.H);

//创建BarCodeGenerator实例

BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);

//根据settings生成图像数据,保存至BufferedImage实例

BufferedImage bufferedImage = barCodeGenerator.generateImage();

//保存为PNG图片

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

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

需要引用Spire.Barcode for java

原文:Java 生成二维码

我已经用java生成了一个二维码了,怎样让扫描二维码后,读取到一个word文档,大神。

不用这么麻烦,直接使用二维码生成器就行了,只要上传文档,自动直接生成二维码。方便有快捷。

推荐一款目前市面上比较不错的二维码生成工具。

登录网站进入操作后台。

点击添加二维码内容。(可查看管理制作过的二维码,如果是第一次使用直接显示第三步的编辑页面。

3.编辑二维码里的内容,上传你的文档。

上传完成后保存即可生成二维码,并且生成的二维码内容支持随时修改,原码不变!

希望对你有帮助!

原创文章,作者:MDPQ,如若转载,请注明出处:https://www.506064.com/n/140696.html

(0)
MDPQMDPQ
上一篇 2024-10-04
下一篇 2024-10-04

相关推荐

  • mysql数据库宕机了,mysql数据库崩溃

    本文目录一览: 1、mysql数据库无法正常使用,导致WWW服务宕机 2、如何优化mysql内存占用高导致宕机 3、MySQL数据库崩溃怎么办 4、mysql数据库主机宕机从机怎么…

    编程 2024-10-11
  • Python:理解对象是类的实例

    Python是一门面向对象的编程语言,在Python中,一切都是对象,包括函数、字符串、数字等等,甚至连模块也可以被视为对象。然而,在Python中,对象与类之间是一种特殊的关系,…

    编程 2024-10-11
  • 如何更新Python版本

    一、如何更新Python版本 Python是一种广泛使用的编程语言,随着新的Python版本不断推出,更新Python版本也愈发重要,因为新版本带来了更多的功能和修复了一些错误。下…

    编程 2024-10-04
  • Mat格式详解

    一、基本概念 Mat格式是OpenCV中的一种存储图像的数据格式,其全称为Matrix格式,表示一个二维矩阵。 在Mat中,每个像素都可以表示为一组数值,其中不同的通道可以表示不同…

    编程 2024-10-04
  • TCP报文格式详解

    一、TCP协议简介 TCP是传输控制协议(Transmission Control Protocol)的缩写,是互联网协议TCP/IP协议簇的一部分,是一种面向连接的、可靠的、基于…

    编程 2024-10-04
  • 使用PHP中数组的filter函数优雅地筛选数组元素

    一、什么是filter函数? 在PHP中,filter是一个非常强大且实用的函数,用于对数组中的元素进行过滤和筛选,能够便捷地完成复杂的数据筛选操作。filter函数采用了回调函数…

    编程 2024-10-04
  • PHP URL处理技巧

    一、URL的基本结构 URL是由多个部分组成,其中包括协议、域名、路径、查询参数等,如下所示: 协议://域名:端口号/路径?查询参数 协议是指访问该URL所使用的协议,如HTTP…

    编程 2024-10-04
  • java写界面,用java写一个界面

    本文目录一览: 1、用Java语言设计一个界面, 2、用java写一个登陆界面代码。 3、用java给一个系统写界面应该怎么写 4、用java程序编写一个简单的登录界面怎么写? 5…

    编程 2024-10-04
  • 全面解析Java AtomicReference

    Java的并发包(java.util.concurrent)提供了许多线程安全的工具类,它们被广泛应用于现代多线程编程的场景中。其中,AtomicReference类尤为重要,它提…

    编程 2024-10-04
  • Python列表操作:查找元素位置的方法

    一、使用index()方法查找元素位置 list1 = [‘apple’, ‘banana’, ‘cherry’] x = list1.index(“banana”) print(…

    编程 2024-10-04

发表回复

登录后才能评论