zoukankan      html  css  js  c++  java
  • 条形码和二维码编码解码

    public class ZxingHandler {

    /**
    * 条形码编码
    *
    * @param contents
    * @param width
    * @param height
    * @param imgPath
    */
    public static void encode(String contents, int width, int height, String imgPath) {
    int codeWidth = 3 + // start guard
    (7 * 6) + // left bars
    5 + // middle guard
    (7 * 6) + // right bars
    3; // end guard
    codeWidth = Math.max(codeWidth, width);
    try {
    BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
    BarcodeFormat.EAN_13, codeWidth, height, null);

    MatrixToImageWriter
    .writeToFile(bitMatrix, "png", new File(imgPath));

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 条形码解码
    *
    * @param imgPath
    * @return String
    */
    public static String decode(String imgPath) {
    BufferedImage image = null;
    Result result = null;
    try {
    image = ImageIO.read(new File(imgPath));
    if (image == null) {
    System.out.println("the decode image may be not exit.");
    }
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    result = new MultiFormatReader().decode(bitmap, null);
    return result.getText();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }

    /**
    * 二维码编码
    *
    * @param contents
    * @param width
    * @param height
    * @param imgPath
    */
    public static void encode2(String contents, int width, int height, String imgPath) {
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    // 指定纠错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    // 指定编码格式
    hints.put(EncodeHintType.CHARACTER_SET, "GBK");
    try {
    BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
    BarcodeFormat.QR_CODE, width, height, hints);

    MatrixToImageWriter
    .writeToFile(bitMatrix, "png", new File(imgPath));

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 二维码解码
    *
    * @param imgPath
    * @return String
    */
    public static String decode2(String imgPath) {
    BufferedImage image = null;
    Result result = null;
    try {
    image = ImageIO.read(new File(imgPath));
    if (image == null) {
    System.out.println("the decode image may be not exit.");
    }
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.CHARACTER_SET, "GBK");

    result = new MultiFormatReader().decode(bitmap, hints);
    return result.getText();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }

    /**
    * @param args
    */
    public static void main(String[] args) {

    // 条形码
    String imgPath = "target\zxing_EAN13.png";
    String contents = "6923450657713";
    int width = 105, height = 50;

    ZxingHandler.encode(contents, width, height, imgPath);
    System.out.println("finished zxing EAN-13 encode.");

    String decodeContent = ZxingHandler.decode(imgPath);
    System.out.println("解码内容如下:" + decodeContent);
    System.out.println("finished zxing EAN-13 decode.");

    // 二维码
    String imgPath2 = "target\zxing.png";
    String contents2 = "Hello Gem, welcome to Zxing!"
    + " Blog [ http://thinkgem.iteye.com ]"
    + " EMail [ thinkgem@163.com ]";
    int width2 = 300, height2 = 300;

    ZxingHandler.encode2(contents2, width2, height2, imgPath2);
    System.out.println("finished zxing encode.");

    String decodeContent2 = ZxingHandler.decode2(imgPath2);
    System.out.println("解码内容如下:" + decodeContent2);
    System.out.println("finished zxing decode.");

    }

    }
  • 相关阅读:
    判断是否是三角形,三角形面积,三角形内外切圆半径和面积
    输入从a加到b的两个数字
    九九乘法表
    某公司销售员工的年终奖根据该员工的年销售总额s提成,年销售总额超过1万元才提成,超过部分提成比例如下:
    判断是否是闰年?
    从键盘上输入三个点的坐标值(1,1)、(2,4)、(3,2),编程求该三角形的面积。
    输入一个正方形的边长,输出正方形的外接圆和内接圆的面积。
    .输入一个4位正整数,以相反的次序输出,例如,输入1234,输出为4321。
    SecoClient在win10系统中连接失败解决方案
    PHP 关于判断输入日期是否合法
  • 原文地址:https://www.cnblogs.com/luyuefei/p/13386356.html
Copyright © 2011-2022 走看看