java-二维码编写zxing

本文介绍了一种使用Google的Zxing库生成二维码的方法。通过Java代码示例,展示了如何设置参数,如编码格式、边距、宽度和高度,并将生成的二维码保存为PNG文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

zxing 这个是google的 
下载地址 
http://code.google.com/p/zxing/downloads/list

二维码源码案例

package com.utils;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class UtilsQrCode {
    
    private static final int BLACK = 0xff000000;
    private static final int WHITE = 0xFFFFFFFF;


    /**
     * @param args
     */
    public static void main(String[] args) {
        File file = new File("C:\\Users\\huage\\Desktop\\图片\\"+new Date().getTime()+".png");
        encode("http://www.baidu.com", file, BarcodeFormat.QR_CODE, 200, 200);
    }
    
    /**
     * @param contents
     * @param file
     * @param format
     * @param width
     * @param height
     */
    public static  void encode(String contents, File file, BarcodeFormat format, int width, int height) {
        try {
            contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1"); 
            Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height,hints);
            BufferedImage image = toBufferedImage(bitMatrix);
            writeToFile(image, "png", file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public static byte[] imgToBytes(BufferedImage image){
        if( image == null ) return new byte[0];
        byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData();
        return data;
    }
    

    public static void writeToFile(BufferedImage image, String format, File file) throws IOException {
        if( image == null || file == null ) return;
        ImageIO.write(image, format, file);
    }

    
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
            }
        }
        return image;
    }




}

 

转载于:https://www.cnblogs.com/hwaggLee/p/5207250.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值