二维码生产与解析

 

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeUtils {
    private static final String CODE = "utf-8";
    private static final String IMAGE_TYPE = "png";
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    private static final int LOGO_SIZE = 60;  

     /** 
     * 生成RQ二维码    默认生成二维码
     *  
     * @author wfhe 
     * @param content 
     *            内容 
     * @param height 
     *            高度(px) 
     * @param width 
     *            高度(px)
     *  
     */  
    public static BufferedImage getRQ(String content, Integer height, Integer width, 
             String logoPath, boolean needCompress) {
        if(height == null || height < 100) {
            height = 200;
        }
        if(width == null || width < 50) {
            width = height;
        }

        try {
            Hashtable<EncodeHintType, Object> h = new Hashtable<EncodeHintType, Object>();
            h.put(EncodeHintType.CHARACTER_SET, CODE);
            h.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            h.put(EncodeHintType.MARGIN, 1);

            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, h);

            return toBufferedImage(bitMatrix, logoPath, needCompress);

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

        return null;
    }

     /** 
     * 转换成图片 
     *  
     * @author wfhe 
     * @param bitMatrix 
     * @param logoPath logo文件路径
     * @param needCompress logo是否要压缩 
     * @return 
     */  
    private static BufferedImage toBufferedImage(BitMatrix bitMatrix, String logoPath, boolean needCompress ){
        int width = bitMatrix.getWidth();
        int height = bitMatrix.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, bitMatrix.get(x, y)? BLACK : WHITE);
            }
        }

        if(logoPath != null && !"".equals(logoPath)) wlogo(image, logoPath, needCompress);
        return image;
    }

    /**
     *  生成一、二维码,写到文件中
     *  
     * @author wfhe
     * @param content 内容
     * @param height 高度
     * @param width 宽度
     * @param filePath 文件路径
     * @throws Exception 
     */
    public static void wQRFile(String content, Integer height, Integer width, String filePath,
             String logoPath, boolean needCompress
            ) throws Exception {
        if(filePath == null || "".equals(filePath)) throw new Exception("filePath erorr : " + filePath);
        File file = new File(filePath);
        if(file == null || file.exists() == false) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                throw new Exception("filePath create fail : " + filePath);
            }
        }


        BufferedImage image  = getRQ(content, height, width, logoPath, needCompress);
        ImageIO.write(image, IMAGE_TYPE, file);
    }

    /**
     * @author wfhe
     * @param filePath 需要解析的二维码图片路径
     * @return
     */
    public static String resolve(String filePath) {
        try {
            if(filePath == null || "".equals(filePath)) throw new Exception("filePath erorr : " + filePath);
            File file = new File(filePath);
            if(file == null || file.exists() == false) throw new Exception("File Not Found : " + filePath);

            BufferedImage imge = ImageIO.read(file);

            LuminanceSource source = new BufferedImageLuminanceSource(imge);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            // 解码设置编码方式为:utf-8
            Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();  
            hints.put(DecodeHintType.CHARACTER_SET, CODE);  

            Result result = new MultiFormatReader().decode(bitmap, hints);

            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author wfhe
     * @param bufferedImage 图片缓存
     * @param logoPath logo 图片文件的路径
     * @param needCompress 是否要压缩logo
     */
    private static void wlogo(BufferedImage bufferedImage, String logoPath, boolean needCompress) { 
        try {
            File file = new File(logoPath);  
            if (!file.exists()) {  
                    throw new Exception("File Not Found : " + logoPath);
            }
            Image logo = ImageIO.read(file);
            int width = logo.getWidth(null);
            int height = logo.getHeight(null);
            if(needCompress){// 压缩LOGO
                width = LOGO_SIZE;
                height = LOGO_SIZE;

                logo = logo.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
                Graphics g = tag.getGraphics();  
                g.drawImage(logo, 0, 0, null); // 绘制缩小后的图  
                g.dispose();  
            }
            // 插入LOGO  
            Graphics2D grap = bufferedImage.createGraphics();
            int x = (bufferedImage.getWidth() - width) >> 1;
            int y = (bufferedImage.getHeight() - height) >> 1;
            grap.drawImage(logo, x, y, width, height, null);
            Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
            grap.setStroke(new BasicStroke(3f));
            grap.draw(shape);
            grap.dispose();

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

    public static void main(String[] args) throws Exception {
        String filePath = "d://1.png";
        String logoPath = "d://111.jpg";

        QRCodeUtils.wQRFile("http://www.baidu.com", null, null, filePath, logoPath, true);

        System.out.println("-----成生成功----");

        String s = QRCodeUtils.resolve(filePath);

        System.out.println("-----解析成功----");
        System.out.println(s);

    }

}

 

 

转载于:https://my.oschina.net/u/2376377/blog/827213

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: LabVIEW是一个流行的图形化编程环境,主要用于控制和测量应用程序的开发。虽然LabVIEW自身不提供直接生成二维码的功能,但可以通过调用其他库或插件来实现生成二维码的功能。 LabVIEW具有强大的外部接口和灵活的功能扩展能力,可以与其他编程语言或库进行集成。例如,可以使用C/C++库或第三方插件将二维码生成功能添加到LabVIEW中。 一种常见的方法是使用Python编写生成二维码的代码,然后通过LabVIEW的Python节点将其嵌入到LabVIEW中。Python中有许多开源库可以生成二维码,例如qrcode和Pillow。通过此方法,LabVIEW可以调用Python代码来生成二维码图像并将其集成到LabVIEW应用程序中。 另一种方法是通过调用其他生成二维码的库或API。一些商业或开源库提供了生成二维码的功能,例如ZXing和ZBar。通过使用LabVIEW的外部调用功能,可以调用这些库来生成二维码图像。 综上所述,虽然LabVIEW本身没有直接的二维码生成功能,但可以通过调用其他库或插件来实现此功能。这使LabVIEW可以与其他编程语言和工具进行集成,从而扩展其应用范围。 ### 回答2: 是的,LabVIEW可以用于生成二维码。LabVIEW是一种程序设计语言和开发环境,它提供了丰富的图形化编程工具和函数库,可以轻松地处理图像和图形。 在LabVIEW中,我们可以使用内置的函数来生成和处理二维码。例如,可以使用Vision Development Module中的函数来生成二维码,并可以通过设置不同的参数来控制二维码的大小、纠错级别、内容等。 生成二维码的过程通常涉及到将文本或其他数据编码为二维码图像。LabVIEW提供了处理字符串和图像的工具,可以将字符串转换为图像,并将图像保存为文件或在界面中显示。 除了生成二维码,LabVIEW还可以用于解码已有的二维码。通过使用Vision Development Module中的函数,LabVIEW可以读取和解析图像中的二维码,并提取出其中的数据。 总而言之,LabVIEW作为一种强大的图形化编程工具,具备生成和处理二维码的能力。无论是生成还是解码二维码,LabVIEW都可以快速且有效地完成任务。 ### 回答3: LabVIEW是一款功能强大的虚拟仪器软件,可以用于控制和监测各种实验室设备。LabVIEW本身没有专门用于生成二维码的功能,但可以通过结合其他工具和库来实现二维码的生成。 例如,可以通过调用第三方的二维码生成库,如ZBar或ZXing,使用LabVIEW编写代码以实现生成二维码的功能。这些库通常提供了方便的API和函数,可以将文本、URL或其他信息转换为二维码图像。 在LabVIEW中,可以使用代码模块或自定义函数来调用这些库,将所需的信息传递给库函数,然后获取生成的二维码图像。通过将生成的图像显示在面板上或保存为文件,可以将二维码用于各种应用,如标识产品、扫描二维码获取信息等。 尽管LabVIEW本身没有直接的二维码生成功能,但作为一款灵活的开发平台,它可以通过与其他工具和库的集成来实现各种功能。这使得LabVIEW可以适应各种实验室需求,并实现二维码生成及相关功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值