ZXing 生成二维码 QRCode 和条码 CODE128 和 ZXing 解析或读取 QR_Code 和 条码 CODE_128

二维码是国际标准,由日本某公司发明,并保留版权,免费让全世界使用,目前在中国金融支付领域大放异彩。

条码的联合发明人诺曼·约瑟夫·伍德兰德(Norman Joseph Woodland)于上世纪70年代发明了条形码,这一技术的发明几乎对全球商业活动产生了一场革命,并为消费者在超市的购物节约了大量时间。日前在自己位于新泽西的家中不幸去世,享年91岁。

二维码和条码的生成使用 zxing,zxing 是开源的条码及二维码生成框架,https://github.com/zxing/zxing/ 免费获取,目前版本 ver 3.3.3。

以下是测试生成的代码,内容将保存在 c:\temp\a.png 和 c:\temp\b.png 两个图片文件内。


/*
 * 2019-2-20
 * 测试 zxing 3.3.3 二维码和条码生成
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class WriteQRCode
{

  public static void main(String[] args)
  {
    int width = 200;
    int height = 200;

    // 写 二维码
    TestWriteRCode("c:/temp/a.png", BarcodeFormat.QR_CODE, "二维码文字信息, Hello world", width, height);

    // 写条码
    height = 50;
    TestWriteRCode("c:/temp/b.png", BarcodeFormat.CODE_128, "ABC123456abc", width, height);
  }

  /**
   * 将字符串信息写入 QRCode
   * 
   * @param fileName
   *          指定文件名
   * @param strContents
   *          写入的内容
   * @param width
   *          尺寸宽
   * @param height
   *          尺寸高
   */
  public static void TestWriteRCode(String fileName, BarcodeFormat typeCode, String strContents, int width, int height)
  {
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    // 字符集
    hints.put(EncodeHintType.CHARACTER_SET, "GBK");// Constant.CHARACTER);
    // 容错质量
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

    try
    {
      // 尺寸
      BitMatrix bitMatrix = new MultiFormatWriter().encode(strContents, typeCode, width, height, hints);
      BufferedOutputStream buffer = null;
      buffer = new BufferedOutputStream(new FileOutputStream(fileName));
      // ok
      MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File(fileName)));
      buffer.close();
    }
    catch (WriterException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }

  }
}

生成成功将得到两个 png 文件。

以下是读取 c:\temp\a.png 和 c:\temp\b.png 内的条码及二维码。


/*
 * 2019-2-20
 * 测试 zxing 3.3.3 二维码和条码读取
 */

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.ImageReader;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import com.google.zxing.multi.MultipleBarcodeReader;

public class ReadCode
{
  private static final Map<DecodeHintType, Object> HINTS;
  private static final Map<DecodeHintType, Object> HINTS_PURE;

  static
  {
    HINTS = new EnumMap<>(DecodeHintType.class);
    HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    HINTS.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
    HINTS_PURE = new EnumMap<>(HINTS);
    HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
  }

  public static void main(String[] args)
  {
    BufferedImage image = null;
    try
    {
      image = ImageIO.read(new File("c:/temp/a.png"));
      ProcessImage(image);

      image = ImageIO.read(new File("c:/temp/b.png"));
      ProcessImage(image);
    }
    catch (IOException e)
    {
    }

  }

  private static void ProcessImage(BufferedImage image)
  {
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
    Collection<Result> results = new ArrayList<>(1);

    try
    {

      Reader reader = new MultiFormatReader();
      ReaderException savedException = null;
      try
      {
        // Look for multiple barcodes
        MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
        Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
        if (theResults != null)
        {
          results.addAll(Arrays.asList(theResults));
        }
      }
      catch (ReaderException re)
      {
        savedException = re;
      }

      if (results.isEmpty())
      {
        try
        {
          // Look for pure barcode
          Result theResult = reader.decode(bitmap, HINTS_PURE);
          if (theResult != null)
          {
            results.add(theResult);
          }
        }
        catch (ReaderException re)
        {
          savedException = re;
        }
      }

      if (results.isEmpty())
      {
        try
        {
          // Look for normal barcode in photo
          Result theResult = reader.decode(bitmap, HINTS);
          if (theResult != null)
          {
            results.add(theResult);
          }
        }
        catch (ReaderException re)
        {
          savedException = re;
        }
      }

      if (results.isEmpty())
      {
        try
        {
          // Try again with other binarizer
          BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
          Result theResult = reader.decode(hybridBitmap, HINTS);
          if (theResult != null)
          {
            results.add(theResult);
          }
        }
        catch (ReaderException re)
        {
          savedException = re;
        }
      }

      if (results.isEmpty())
      {
        try
        {
          throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;
        }
        catch (FormatException | ChecksumException e)
        {
          // log.info(e.toString());
        }
        catch (ReaderException e)
        { // Including NotFoundException
          // log.info(e.toString());
        }
        return;
      }

      for (Result result : results)
      {
        System.out.println(result.getText());
      }

    }
    catch (RuntimeException re)
    {
      // Call out unexpected errors in the log clearly
      // log.log(Level.WARNING, "Unexpected exception from library", re);
    }
  }

}

运行读取测试程序,将输出相应的内容。

这里顺便提一下,若二维码或者条码在一个含有表格、文字的富文本文档转换的图片中,也是可以被识别出来的,但会略微消耗一点时间。

 

https://blog.inull.net
Q群讨论 236201801

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Java 中生成二维码,可以使用第三方库Zxing。首先需要在项目中引入该库,可以通过 Maven 或 Gradle 进行引入。 生成二维码的代码如下: ```java import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) { String qrCodeData = "R2022121200000027"; String filePath = "qr_code.png"; int size = 250; String fileType = "png"; File qrFile = new File(filePath); try { createQRCode(qrFile, qrCodeData, size, fileType); System.out.println("QR Code generated successfully!"); } catch (WriterException e) { System.out.println("Error generating QR Code: " + e.getMessage()); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } private static void createQRCode(File qrFile, String qrCodeData, int size, String fileType) throws WriterException, Exception { // Set QR code parameters Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>(); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hintMap.put(EncodeHintType.MARGIN, 2); hintMap.put(EncodeHintType.QR_VERSION, 4); // Create QR code writer and encode the data QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeData, BarcodeFormat.QR_CODE, size, size, hintMap); // Create QR code image Path path = Paths.get(qrFile.getAbsolutePath()); MatrixToImageWriter.writeToPath(byteMatrix, fileType, path); } } ``` 在上面的代码中,我们首先定义了要生成的二维码的内容、文件路径、大小和文件类型。然后使用 QRCodeWriter 对象将内容编码为二维码,再使用 MatrixToImageWriter 将其写入文件中。运行该代码,会在项目根目录下生成一个名为 qr_code.png 的二维码文件,包含订单号 R2022121200000027。 你也可以根据自己的需求修改代码中的参数,比如大小、文件类型等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值