Java二维码的解码和编码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
需要用到的Jar包:ZXing-core.jar和ZXing-javase.jar。
 
import java.io.File;
import java.util.Hashtable;
  
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
  
//编码
public class QREncoder {
  
    public static void main(String[] args) throws Exception {
        String content = "hello world";
        String encoding = "utf-8";
        int width = 150;
        int height = 150;
        String format = "jpg";
        File imagePath = new File("d:/test.jpg");
          
        com.google.zxing.Writer writer = new MultiFormatWriter();
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
        BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToFile(matrix, format, imagePath);
    }
  
}
  
  
  
import java.io.File;
import java.util.Hashtable;
  
import javax.imageio.ImageIO;
  
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.GlobalHistogramBinarizer;
  
//解码
public class QRDecoder {
  
    public static void main(String[] args) throws Exception {
        File imagePath = new File("d:/test.jpg");
        String encoding = "utf-8";
          
        Reader reader = new MultiFormatReader();
        LuminanceSource ls = new BufferedImageLuminanceSource(ImageIO.read(imagePath));
        Binarizer b = new GlobalHistogramBinarizer(ls);
        Hashtable<DecodeHintType, String> hint1 = new Hashtable<DecodeHintType, String>(2);
        hint1.put(DecodeHintType.CHARACTER_SET, encoding);
        Result r = reader.decode(new BinaryBitmap(b), hint1);
        System.out.println(r.getText());
    }
  
}
 

标签: Google

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:HttpUtils 用于进行网络请求的工具类

下一篇:遗传算法的神经网络python实现代码