Java生成二维码或一维条形码(待续 未完)

Java生成二维码或一维条形码(待续)

 

前段时间用了“我查查”的软件

 

手机可以直接扫描条码,所以自己也想来试试

 

需要研究的如下

1、一维码的 读取、生成

2、二维码的 读取、生成

3、使用摄像头 直接读取条码(待研究)

4、使用条码枪,利用dll读取条码(待研究)

 

下面是收集的相关资料

QR Code二维条形码的生成和读取解析和摄像头的读取  http://www.i5a6.com/?p=556
Java实现二维码QRCode的编码和解码  http://www.2cto.com/kf/201108/98471.html

条形码处理类库 ZXing  http://www.oschina.net/p/zxing

相关jar 包下载 http://swetake.com/qr/

 

 

代码包结构如下

├─一维条码
│      Read.java
│      Write.java

└─二维条码
        QRCodeDecoderHandler.java
        QRCodeEncoderHandler.java
        Read2.java
        TxmWrite.java
        Write2.java

 

jar包含

    jbarcode-0.2.8.jar
    qrcode.jar
    Qrcode_swetake.jar
    zxing1.3_core.jar
    zxing1.3_javase.jar

 

 

 

 

package 二维条码;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.util.ContentConverter;

import com.swetake.util.Qrcode;

//二维条形码

public class TxmWrite
{
    public static void main(String[] args) throws Exception
    {
        TxmWrite test = new TxmWrite();
        test.creatTxm("676317283718啊好的乖哈苏德");
        test.readTxm("TxmQRCode.png");
    }

    /**
     * 创建二维条形码
     *
     * @param param
     *            比如身份证号码
     * @throws Exception
     */
    public void creatTxm(String param) throws Exception
    {
        Qrcode qrcode = new Qrcode();
        qrcode.setQrcodeErrorCorrect('M');
        qrcode.setQrcodeEncodeMode('B');
        qrcode.setQrcodeVersion(7);

        byte[] bstr = param.getBytes("UTF-8");
        BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setBackground(Color.WHITE); // 背景颜色
        g.clearRect(0, 0, 139, 139);
        g.setColor(Color.BLACK); // 条码颜色
        if (bstr.length > 0 && bstr.length < 123)
        {
            boolean[][] b = qrcode.calQrcode(bstr);
            for (int i = 0; i < b.length; i++)
            {
                for (int j = 0; j < b.length; j++)
                {
                    if (b[j][i])
                    {
                        g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
                    }
                }

            }
        }
        g.dispose();
        bi.flush();
        String FilePath = "TxmQRCode.png";
        File f = new File(FilePath);
        ImageIO.write(bi, "png", f);
    }

    /**
     * 解析二维条形码
     *
     * @param path
     *            条形码图片的路径
     * @throws Exception
     */
    public void readTxm(String path) throws Exception
    {
        QRCodeDecoder decoder = new QRCodeDecoder();
        BufferedImage image = null;
        if (path.startsWith("http://"))
        {
            image = ImageIO.read(new URL(path));
        } else
        {
            image = ImageIO.read(new File(path));
        }
        String decodedString = new String(decoder.decode(new J2SEImage1(image)), "UTF-8");
        decodedString = ContentConverter.convert(decodedString);
        System.out.println("条码内容:" + decodedString);
    }

}

class J2SEImage1 implements QRCodeImage
{
    BufferedImage image;

    public J2SEImage1(BufferedImage source)
    {
        this.image = source;
    }

    public int getWidth()
    {
        return image.getWidth();
    }

    public int getHeight()
    {
        return image.getHeight();
    }

    public int getPixel(int x, int y)
    {
        return image.getRGB(x, y);

    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Java生成二维码的代码示例: ``` import java.io.File; import java.nio.file.Path; import java.util.EnumMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.Writer; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) throws Exception { String text = "Hello, World!"; // 要生成二维码的文本内容 int width = 300; // 二维码图片的宽度 int height = 300; // 二维码图片的高度 String format = "png"; // 二维码图片的格式 Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码格式 BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); Path outputPath = new File("qrcode.png").toPath(); // 生成的二维码图片保存的路径 MatrixToImageWriter.writeToPath(bitMatrix, format, outputPath); // 将二维码图片写入文件 } } ``` 这段代码使用了Google的ZXing库来生成二维码。首先,指定了要生成的文本内容、二维码图片的宽度、高度和格式。然后,通过`hints`参数指定了编码格式为UTF-8。接着,使用`QRCodeWriter`类的`encode`方法生成一个`BitMatrix`对象,该对象包含了生成的二维码图案。最后,将`BitMatrix`对象写入文件,生成二维码图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值