二维码的生成

  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, 200200,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. }  
  1. package liupeng;  
  2.   
  3. import com.google.zxing.LuminanceSource;  
  4.   
  5. import java.awt.Graphics2D;  
  6. import java.awt.geom.AffineTransform;  
  7. import java.awt.image.BufferedImage;  
  8.   
  9. public final class BufferedImageLuminanceSource extends LuminanceSource {  
  10.   
  11.   private final BufferedImage image;  
  12.   private final int left;  
  13.   private final int top;  
  14.   
  15.   public BufferedImageLuminanceSource(BufferedImage image) {  
  16.     this(image, 00, image.getWidth(), image.getHeight());  
  17.   }  
  18.   
  19.   public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {  
  20.     super(width, height);  
  21.   
  22.     int sourceWidth = image.getWidth();  
  23.     int sourceHeight = image.getHeight();  
  24.     if (left + width > sourceWidth || top + height > sourceHeight) {  
  25.       throw new IllegalArgumentException("Crop rectangle does not fit within image data.");  
  26.     }  
  27.   
  28.     for (int y = top; y < top + height; y++) {  
  29.       for (int x = left; x < left + width; x++) {  
  30.         if ((image.getRGB(x, y) & 0xFF000000) == 0) {  
  31.           image.setRGB(x, y, 0xFFFFFFFF); // = white  
  32.         }  
  33.       }  
  34.     }  
  35.   
  36.     this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);  
  37.     this.image.getGraphics().drawImage(image, 00null);  
  38.     this.left = left;  
  39.     this.top = top;  
  40.   }  
  41.   
  42.   @Override  
  43.   public byte[] getRow(int y, byte[] row) {  
  44.     if (y < 0 || y >= getHeight()) {  
  45.       throw new IllegalArgumentException("Requested row is outside the image: " + y);  
  46.     }  
  47.     int width = getWidth();  
  48.     if (row == null || row.length < width) {  
  49.       row = new byte[width];  
  50.     }  
  51.     image.getRaster().getDataElements(left, top + y, width, 1, row);  
  52.     return row;  
  53.   }  
  54.   
  55.   @Override  
  56.   public byte[] getMatrix() {  
  57.     int width = getWidth();  
  58.     int height = getHeight();  
  59.     int area = width * height;  
  60.     byte[] matrix = new byte[area];  
  61.     image.getRaster().getDataElements(left, top, width, height, matrix);  
  62.     return matrix;  
  63.   }  
  64.   
  65.   @Override  
  66.   public boolean isCropSupported() {  
  67.     return true;  
  68.   }  
  69.   
  70.   @Override  
  71.   public LuminanceSource crop(int left, int top, int width, int height) {  
  72.     return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);  
  73.   }  
  74.   
  75.   @Override  
  76.   public boolean isRotateSupported() {  
  77.     return true;  
  78.   }  
  79.   
  80.   @Override  
  81.   public LuminanceSource rotateCounterClockwise() {  
  82.   
  83.       int sourceWidth = image.getWidth();  
  84.     int sourceHeight = image.getHeight();  
  85.   
  86.     AffineTransform transform = new AffineTransform(0.0, -1.01.00.00.0, sourceWidth);  
  87.   
  88.     BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);  
  89.   
  90.     Graphics2D g = rotatedImage.createGraphics();  
  91.     g.drawImage(image, transform, null);  
  92.     g.dispose();  
  93.   
  94.     int width = getWidth();  
  95.     return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);  
  96.   }  
  97.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二维码生成驱动C语言的过程如下: 首先,需要了解二维码生成原理和格式。二维码是一种矩阵型的图形编码,可以存储大量信息。它的生成过程包括数据编码、纠错码计算和图形绘制三个主要步骤。 在C语言中,可以使用开源的二维码生成库,如ZXing或QRcode等。这些库提供了控制二维码生成的函数和接口,方便我们使用C语言进行二维码生成。 首先,需要初始化一个二维码生成器对象。然后,调用相关函数设置生成器的参数,如二维码版本、纠错级别、编码方式等。这些参数将影响生成二维码的容量和可靠性。 接下来,需要将待编码的数据传递给生成器。生成器会根据设置的参数将数据编码成二维码格式。编码过程中,可以根据需要进行错误校正,以提高二维码的可靠性。 最后,调用绘制函数将生成二维码图形绘制到指定的输出设备或文件中。绘制过程中,可以设置二维码的尺寸、颜色、间距等属性。 需要注意的是,二维码生成需要依赖相应的驱动程序和库文件,因此在使用C语言进行二维码生成前,需要安装和配置相应的驱动和库。 总而言之,生成二维码的过程需要借助于C语言的二维码生成库,通过设置参数、传递数据、进行编码和绘制等步骤来实现。通过合理的调用和配置,可以生成符合要求的二维码图形。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值