打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
二维码的生成
  1. package liupeng;  
  2. import java.io.File;  
  3. import org.json.JSONException;  
  4. import org.json.JSONObject;  
  5. public class Test {  
  6.     /** 
  7.      * 描述:二维码的生成 
  8.      * @author liupeng 
  9.      * @param args 
  10.      * @throws JSONException  
  11.      */  
  12.     public static void main(String[] args) throws JSONException {  
  13.         JSONObject obj = new JSONObject();  
  14.         obj.put("name", "liupeng");  
  15.         obj.put("age", 22);  
  16.         obj.put("school", "[盐城师范学院]信息科学与技术学院->软件工程");  
  17.         //String tomcatRoot = httpRequest.getSession().getServletContext().getRealPath("/SealAndSignImages");  
  18.         //String path = QRCodeOperator.MakeQRCode(tomcatRoot,"刘鹏的二维码", obj.toString());  
  19.         String path = QRCodeOperator.MakeQRCode("F:"+File.separator,"刘鹏的二维码", obj.toString());  
  20.         if(path != ""){  
  21.             System.out.println("二维码内容为:"+obj.toString());  
  22.         }  
  23.     }  
  24.   
  25. }  
  26.   
  27.   
  28.   
  29. package liupeng;  
  30. import java.awt.image.BufferedImage;  
  31. import java.io.File;  
  32. import java.util.HashMap;  
  33. import java.util.Map;  
  34. import javax.imageio.ImageIO;  
  35. import javax.servlet.http.HttpServletRequest;  
  36. import com.google.zxing.BarcodeFormat;  
  37. import com.google.zxing.Binarizer;  
  38. import com.google.zxing.BinaryBitmap;  
  39. import com.google.zxing.EncodeHintType;  
  40. import com.google.zxing.LuminanceSource;  
  41. import com.google.zxing.MultiFormatReader;  
  42. import com.google.zxing.MultiFormatWriter;  
  43. import com.google.zxing.Result;  
  44. import com.google.zxing.common.BitMatrix;  
  45. import com.google.zxing.common.HybridBinarizer;  
  46. public class QRCodeOperator {  
  47.     /** 
  48.      * 生成二维码 
  49.      * @param name 二维码文件名称(不带后缀) 
  50.      * @param content 二维码内容 
  51.      * @return 二维码图片路径 
  52.      */  
  53.     public static String MakeQRCode(String filepath,String name, String content){  
  54.         String retPath = "";  
  55.         try {  
  56.               
  57.              //String content = "120605181003;http://www.cnblogs.com/jtmjx";  
  58.              //String path = FileOperator.getRootPath();  
  59.             String path = filepath;//httpRequest.getSession().getServletContext().getRealPath("/SealAndSignImages");  
  60.                
  61.              MultiFormatWriter multiFormatWriter = new MultiFormatWriter();  
  62.                
  63.              Map hints = new HashMap();  
  64.              hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
  65.              BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 200, 200,hints);  
  66.              File file1 = new File(path, name+".jpg");  
  67.              MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);  
  68.              retPath = file1.getAbsolutePath();  
  69.          } catch (Exception e) {  
  70.              e.printStackTrace();  
  71.          }  
  72.         return retPath;  
  73.     }  
  74.     public static void AnalysisQRCode(){  
  75.         try {  
  76.             MultiFormatReader formatReader = new MultiFormatReader();  
  77.             String filePath = "F:/Users/Administrator/Desktop/testImage/test.jpg";  
  78.             File file = new File(filePath);  
  79.             BufferedImage image = ImageIO.read(file);;  
  80.             LuminanceSource source = new BufferedImageLuminanceSource(image);  
  81.             Binarizer  binarizer = new HybridBinarizer(source);  
  82.             BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);  
  83.             Map hints = new HashMap();  
  84.             hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
  85.             Result result = formatReader.decode(binaryBitmap,hints);  
  86.               
  87.             System.out.println("result = "+ result.toString());  
  88.             System.out.println("resultFormat = "+ result.getBarcodeFormat());  
  89.             System.out.println("resultText = "+ result.getText());  
  90.                           
  91.         }   
  92.         catch (Exception e) {  
  93.             e.printStackTrace();  
  94.         }  
  95.     }  
  96. }  
  97.   
  98.   
  99. package liupeng;  
  100. import com.google.zxing.common.BitMatrix;  
  101. import javax.imageio.ImageIO;  
  102. import java.io.File;  
  103. import java.io.OutputStream;  
  104. import java.io.IOException;  
  105. import java.awt.image.BufferedImage;  
  106. public final class MatrixToImageWriter {  
  107.   private static final int BLACK = 0xFF000000;  
  108.   private static final int WHITE = 0xFFFFFFFF;  
  109.   private MatrixToImageWriter() {}  
  110.   public static BufferedImage toBufferedImage(BitMatrix matrix) {  
  111.     int width = matrix.getWidth();  
  112.     int height = matrix.getHeight();  
  113.     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  114.     for (int x = 0; x < width; x++) {  
  115.       for (int y = 0; y < height; y++) {  
  116.         image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);  
  117.       }  
  118.     }  
  119.     return image;  
  120.   }  
  121.   public static void writeToFile(BitMatrix matrix, String format, File file)  
  122.       throws IOException {  
  123.     BufferedImage image = toBufferedImage(matrix);  
  124.     if (!ImageIO.write(image, format, file)) {  
  125.       throw new IOException("Could not write an image of format " + format + " to " + file);  
  126.     }  
  127.   }  
  128.   
  129.     
  130.   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)  
  131.       throws IOException {  
  132.     BufferedImage image = toBufferedImage(matrix);  
  133.     if (!ImageIO.write(image, format, stream)) {  
  134.       throw new IOException("Could not write an image of format " + format);  
  135.     }  
  136.   }  
  137.   
  138. }  


注意:需要导入org.json.jar和zxing-core-2.0.jar

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SpringBoot项目替换内部的依赖jar包
打包sqlserver 2005的java驱动包sqljdbc.jar
执行可执行的jar包
Jar Of Love
Jar of Love -曲婉婷
jar.exe
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服