熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java二維碼的生成以及解析

2022-06-13   來源: Java核心技術 

  日本人寫的解碼與編碼實現Sourceforgejp與

  這裡把它們merge到一個jar文件裡

  編碼測試

  Java代碼

  import javaawtColor;

  import javaawtGraphicsD;

  import javaawtimageBufferedImage;

  import javaioFile;

  import javaximageioImageIO;

  import comswetakeutilQrcode;

  public class QRCodeEncoderTest {

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

  Qrcode qrcode=new Qrcode();

  qrcodesetQrcodeErrorCorrect(M);

  qrcodesetQrcodeEncodeMode(B);

  qrcodesetQrcodeVersion();

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

  byte[] d =testStringgetBytes(GBK);

  BufferedImage bi = new BufferedImage( BufferedImageTYPE_INT_RGB);

  // createGraphics

  GraphicsD g = bicreateGraphics();

  // set background

  gsetBackground(ColorWHITE);

  gclearRect( );

  gsetColor(ColorBLACK);

  if (dlength> && dlength <){

  boolean[][] b = qrcodecalQrcode(d);

  for (int i=;i<blength;i++){

  for (int j=;j<blength;j++){

  if (b[j][i]) {

  gfillRect(j*+i*+);

  }

  }

  }

  }

  gdispose();

  biflush();

  String FilePath=TestQRCodepng;

  File f = new File(FilePath);

  ImageIOwrite(bi png f);

  Systemoutprintln(doned!);

  }

  }

  import javaawtColor; import javaawtGraphicsD; import javaawtimageBufferedImage; import javaioFile; import javaximageioImageIO; import comswetakeutilQrcode; public class QRCodeEncoderTest { public static void main(String[] args) throws Exception { Qrcode qrcode=new Qrcode(); qrcodesetQrcodeErrorCorrect(M); qrcodesetQrcodeEncodeMode(B); qrcodesetQrcodeVersion(); String testString = _+^%$#@!~`=&)*(; byte[] d =testStringgetBytes(GBK); BufferedImage bi = new BufferedImage( BufferedImageTYPE_INT_RGB); // createGraphics GraphicsD g = bicreateGraphics(); // set background gsetBackground(ColorWHITE); gclearRect( ); gsetColor(ColorBLACK); if (dlength> && dlength <){ boolean[][] b = qrcodecalQrcode(d); for (int i=;i<blength;i++){ for (int j=;j<blength;j++){ if (b[j][i]) { gfillRect(j*+i*+); } } } } gdispose(); biflush(); String FilePath=TestQRCodepng; File f = new File(FilePath); ImageIOwrite(bi png f); Systemoutprintln(doned!); } }

  解碼測試

  Java代碼

  import javaawtimageBufferedImage;

  import javaioFile;

  import javaioIOException;

  import javaioUnsupportedEncodingException;

  import javaximageioImageIO;

  import jpsourceforgeqrcodeQRCodeDecoder;

  import jpsourceforgeqrcodedataQRCodeImage;

  import jpsourceforgeqrcodeexceptionDecodingFailedException;

  public class QRCodeDecoderTest {

  public QRCodeDecoderTest() {

  }

  public static void main(String[] args) {

  QRCodeDecoder decoder = new QRCodeDecoder();

  File imageFile = new File(TestQRCodepng);

  BufferedImage image = null;

  try {

  image = ImageIOread(imageFile);

  } catch (IOException e) {

  Systemoutprintln(Error: + egetMessage());

  }

  try {

  String decodedData = new String(decoderdecode(new JSEImage(image))GBK);

  Systemoutprintln(decodedData);

  } catch (DecodingFailedException dfe) {

  Systemoutprintln(Error: + dfegetMessage());

  } catch (UnsupportedEncodingException e) {

  eprintStackTrace();

  }

  }

  }

  class JSEImage implements QRCodeImage {

  BufferedImage image;

  public JSEImage(BufferedImage image) {

  thisimage = image;

  }

  public int getWidth() {

  return imagegetWidth();

  }

  public int getHeight() {

  return imagegetHeight();

  }

  public int getPixel(int x int y) {

  return imagegetRGB(x y);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25835.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.