最新帖子 精华区 社区服务 会员列表 统计排行
主题 : Java二维码的生成以及解析
ceshi123 离线
级别: 管理员
宣传大使奖 特殊贡献奖 灌水天才奖
显示用户信息 
0  发表于: 2010-08-02 20:06

Java二维码的生成以及解析

购买流程:注册论坛账号------->在线充值购买论坛交易币------->下载毕业设计将直接扣除交易币
  日本人写的解码与编码实现Sourceforge.jp与swetake.com

  这里把它们merge到一个jar文件里。

  编码测试:

  Java代码

  import java.awt.Color;

  import java.awt.Graphics2D;

  import java.awt.image.BufferedImage;

  import java.io.File;

  import javax.imageio.ImageIO;

  import com.swetake.util.Qrcode;

  public class QRCodeEncoderTest {

  public static void main(String[] args) throws Exception {

  Qrcode qrcode=new Qrcode();

  qrcode.setQrcodeErrorCorrect('M');

  qrcode.setQrcodeEncodeMode('B');

  qrcode.setQrcodeVersion(7);

  String testString = "_-+^%$#@!~`=&)*(";

  byte[] d =testString.getBytes("GBK");

  BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);

  // createGraphics

  Graphics2D g = bi.createGraphics();

  // set background

  g.setBackground(Color.WHITE);

  g.clearRect(0, 0, 139, 139);

  g.setColor(Color.BLACK);

  if (d.length>0 && d.length <123){

  boolean[][] b = qrcode.calQrcode(d);

  for (int i=0;i<b.length;i++){

  for (int j=0;j<b.length;j++){

  if (b[j]) {

  g.fillRect(j*3+2,i*3+2,3,3);

  }

  }

  }

  }

  g.dispose();

  bi.flush();

  String FilePath="TestQRCode.png";

  File f = new File(FilePath);

  ImageIO.write(bi, "png", f);

  System.out.println("doned!");

  }

  }

  import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import com.swetake.util.Qrcode; public class QRCodeEncoderTest { public static void main(String[] args) throws Exception { Qrcode qrcode=new Qrcode(); qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); qrcode.setQrcodeVersion(7); String testString = "_-+^%$#@!~`=&)*("; byte[] d =testString.getBytes("GBK"); BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); // createGraphics Graphics2D g = bi.createGraphics(); // set background g.setBackground(Color.WHITE); g.clearRect(0, 0, 139, 139); g.setColor(Color.BLACK); if (d.length>0 && d.length <123){ boolean[][] b = qrcode.calQrcode(d); for (int i=0;i<b.length;i++){ for (int j=0;j<b.length;j++){ if (b[j]) { g.fillRect(j*3+2,i*3+2,3,3); } } } } g.dispose(); bi.flush(); String FilePath="TestQRCode.png"; File f = new File(FilePath); ImageIO.write(bi, "png", f); System.out.println("doned!"); } }

  解码测试:

  Java代码

  import java.awt.image.BufferedImage;

  import java.io.File;

  import java.io.IOException;

  import java.io.UnsupportedEncodingException;

  import javax.imageio.ImageIO;

  import jp.sourceforge.qrcode.QRCodeDecoder;

  import jp.sourceforge.qrcode.data.QRCodeImage;

  import jp.sourceforge.qrcode.exception.DecodingFailedException;

  public class QRCodeDecoderTest {

  public QRCodeDecoderTest() {

  }

  public static void main(String[] args) {

  QRCodeDecoder decoder = new QRCodeDecoder();

  File imageFile = new File("TestQRCode.png");

  BufferedImage image = null;

  try {

  image = ImageIO.read(imageFile);

  } catch (IOException e) {

  System.out.println("Error: "+ e.getMessage());

  }

  try {

  String decodedData = new String(decoder.decode(new J2SEImage(image)),"GBK");

  System.out.println(decodedData);

  } catch (DecodingFailedException dfe) {

  System.out.println("Error: " + dfe.getMessage());

  } catch (UnsupportedEncodingException e) {

  e.printStackTrace();

  }

  }

  }

  class J2SEImage implements QRCodeImage {

  BufferedImage image;

  public J2SEImage(BufferedImage image) {

  this.image = image;

  }

  public int getWidth() {

  return image.getWidth();

  }

  public int getHeight() {

  return image.getHeight();

  }

  public int getPixel(int x, int y) {

  return image.getRGB(x, y);

  }

  }

快速回复 顶端
内容
HTML 代码不可用
使用签名
Wind Code自动转换

验证问题:本站域名是什么?答案:cccbbs.net  正确答案:cccbbs.net
按"Ctrl+Enter"直接提交