「java生成带参数二维码」java生产二维码

博主:adminadmin 2022-11-25 05:14:07 56

今天给各位分享java生成带参数二维码的知识,其中也会对java生产二维码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

怎么使用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如何用代码生成二维码

引用spire.barcode.jar包

//创建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"));

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);  

   

    }  

   

}

java生成带参数二维码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java生产二维码、java生成带参数二维码的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。