Zxing方式生产二维码方法

简介:
  1. ackage com.wmsDemo.QRCode;  

    import com.google.zxing.common.BitMatrix;  

        

     import javax.imageio.ImageIO;  

     import java.io.File;  

     import java.io.OutputStream;  

     import java.io.IOException;  

     import java.awt.image.BufferedImage;  

        

        

     public final class MatrixToImageWriter {  

        

       private static final int BLACK = 0xFF000000;  

       private static final int WHITE = 0xFFFFFFFF;  

        

       private MatrixToImageWriter() {}  

        

          

       public static BufferedImage toBufferedImage(BitMatrix matrix) {  

         int width = matrix.getWidth();  

         int height = matrix.getHeight();  

         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  

         for (int x = 0; x < width; x++) {  

           for (int y = 0; y < height; y++) {  

             image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);  

           }  

         }  

         return image;  

       }  

        

       public static void writeToFile(BitMatrix matrix, String format, File file)  

           throws IOException {  

         BufferedImage image = toBufferedImage(matrix);  

         if (!ImageIO.write(image, format, file)) {  

           throw new IOException("Could not write an image of format " + format + " to " + file);  

         }  

       }  

        

          

       public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)  

           throws IOException {  

         BufferedImage image = toBufferedImage(matrix);  

         if (!ImageIO.write(image, format, stream)) {  

           throw new IOException("Could not write an image of format " + format);  

         }  

       }  

        

     }

  2.  

    package com.wmsDemo.QRCode;

     

    import java.io.File;

    import java.util.HashMap;

    import java.util.Map;

     

    import com.google.zxing.BarcodeFormat;

    import com.google.zxing.EncodeHintType;

    import com.google.zxing.MultiFormatWriter;

    import com.google.zxing.common.BitMatrix;

       

    public class Test {  

       

        /** 

         * @param args 

         * @throws Exception  

         */ 

        public static void main(String[] args){  

       

    try {

            String content = "这是测试xing二维码生成"; 

    //        String path = "D:/tt";

            String path = "D:/java/apache-tomcat-7.0.47/webapps/wmsDemo/QRCode";

            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

            Map hints = new HashMap();  

            //内容所使用编码  

            hints.put(EncodeHintType.CHARACTER_SET, "gb2312");  

            BitMatrix bitMatrix = multiFormatWriter.encode(content,BarcodeFormat.QR_CODE, 200, 200, hints);  

            //生成二维码  

            File outputFile = new File(path,"14.jpg"); 

            MatrixToImageWriter.writeToFile(bitMatrix, "jpg", outputFile);  

    } catch (Exception e) {

        e.printStackTrace();

    }

     

        }  

       

    }

相关文章
|
4月前
|
算法 小程序 Java
java制作海报三:获取微信二维码详情,并改变大小,合成到海报(另一张图片)上
这篇文章介绍了如何使用Java获取微信小程序的二维码,并将其调整大小后合成到海报(另一张图片)上。
85 0
|
存储 搜索推荐 算法
分享一个在线二维码生成器(基于qrcode.js开发)
一种二维码扫描与生成的工具, 它可生成个性化二维码, 支持文本、网址、图片、短信、电话等格式及主题,提供融合码功能
237 1
|
9月前
|
小程序 开发者
微信小程序保存二维码的过程
微信小程序保存二维码的过程
224 0
|
传感器 数据采集 编解码
|
传感器 API Android开发
|
存储 算法 Java
Qt三方库开发技术:二维码生成、识别以及条码识别
Qt三方库开发技术:二维码生成、识别以及条码识别
Qt三方库开发技术:二维码生成、识别以及条码识别
|
前端开发 API
Axure实战21:使用Axure和API接口创建一个QRCode二维码生成器
Axure实战21:使用Axure和API接口创建一个QRCode二维码生成器
1214 0
Axure实战21:使用Axure和API接口创建一个QRCode二维码生成器
|
编译器 Android开发 iOS开发
iOS应用程序打包ipa转化为二维码
iOS应用程序打包ipa转化为二维码
877 0
iOS应用程序打包ipa转化为二维码
|
移动开发 JSON 前端开发
如何利用Qrcode制作一个二维码生成器?
玲琅满目的二维码在我们的都市和朋友圈中随处可见, 很多平台都提供了定制二维码的服务, 那么作为一名程序员, 我们如何自己实现一个简单的二维码生成器呢? 接下来笔者就来带大家一起利用Qrcode实现一个二维码生成器.
353 0