OpenJWeb新增二维码图片生成功能

迷途d书童 发布于 2012/03/09 12:17
阅读 1K+
收藏 2

    

 

package com.openjweb.erp.barcode;

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.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.encoder.ByteMatrix;


//http://hi.baidu.com/baid/blog/item/fd446e06d199a77702088102.html
//http://www.4ucode.com/Study/Topic/1162485
//http://www.baishaode.com/blog/?p=436
public class QRCodeGen
{
 /**
  * 生成二维码图片
  * @param code 待生成图片的二维码
  * @param destFile 生成二维码图片存储的文件
  */
 public static void createQRCodePic(String code,String destFile)
 {
  BitMatrix byteMatrix;
  try
  {
   byteMatrix = new MultiFormatWriter()
   .encode(code,BarcodeFormat.QR_CODE, 200, 200);
      File file = new File(destFile);
      MatrixToImageWriter.writeToFile(byteMatrix, destFile.substring(destFile.lastIndexOf(".")+1), file);
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  catch (WriterException e1)
  {
   e1.printStackTrace();
  }
 }
 
 /**
  * 反向解析二维码
  * @param picFile
  * @return
  */
 public static String decodeQRCode(String imgPath,String charset)
 {
  String code = "";
  try
  {
   Reader reader = new MultiFormatReader();
   File file = new File(imgPath);
   BufferedImage image;
   try
   {
    image = ImageIO.read(file);
       if (image == null)
       {
        System.out.println("Could not decode image");
       }
       LuminanceSource source = new BufferedImageLuminanceSource(image);
       BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
       Result result;
       Hashtable hints= new Hashtable();
       hints.put(DecodeHintType.CHARACTER_SET, charset); //utf-8
       //解码设置编码方式为:utf-8,
       result = new MultiFormatReader().decode(bitmap,hints);
       String resultStr = result.getText();
       code = resultStr;
       System.out.println("解析后内容:"+resultStr);
   }
   catch (IOException ioe)
   {
    System.out.println(ioe.toString());
   }
   catch (ReaderException re)
   {
    System.out.println(re.toString());
   }
   }
  catch(Exception ex)
  {
   System.out.println(ex.toString());
  }
  return code;
 }
 
 public static void main(String[] args) {

  String str = "test";//二维码内容
  String path = "d:/test";

  BitMatrix byteMatrix;
  try
  {
   byteMatrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 200, 200);

  File file = new File(path + ".png");
  MatrixToImageWriter.writeToFile(byteMatrix, "png", file);
  } catch (IOException e) {
  e.printStackTrace();
  } catch (WriterException e1) {
  e1.printStackTrace();
  }
  }
}

 

 

jsp调用示例:

 

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.openjweb.erp.barcode.*"%>
 
<%
try
{
 //测试通过程序生成二维码图形
 //String encodeStr = "中华人民共和国";

 //String encodeStr = "http://www.openjweb.com";

 String encodeStr = "老婆我爱你!";
 
 out.println("QRCode二维码测试:"+encodeStr+"<br>");
 encodeStr = new String(encodeStr.getBytes("utf-8"), "ISO-8859-1");//转换成ISO-8859-1

 String imgPath = "d:/barcode.png";
    QRCodeGen.createQRCodePic(encodeStr,imgPath);
 
 out.println("对生成的文件进行解码,读取生成的图片:");
    String decode = QRCodeGen.decodeQRCode(imgPath,"utf-8");  //还原成utf-8编码
 out.println(decode);
 
}
catch(Exception ex)
{

ex.printStackTrace();

}
%>

 

生成的二维码图片:

 


信息化时代宣泄爱情的最微妙的方式是什么呢?送给太太一个二维码图片,见上图。

 


原文链接:http://blog.csdn.net/baozhengw/article/details/6690285
加载中
0
mariahsharp468
mariahsharp468
嗯, 我之前试过用java生成 qr code
返回顶部
顶部