Java实现二维码QRCode的编码和解码

java

周末试用下Android手机的二维码扫描软件,扫描了下火车票、名片等等,觉得非常不错很有意思的。当然Java也可以实现这些,现在就分享下如何简单用Java实现二维码中QRCode的编码和解码(可以手机扫描验证)。

    涉及到的一些主要类库,方便大家下载:

  • 编码 lib:Qrcode_swetake.jar   (官网介绍 -- http://www.swetake.com/qr/index-e.html)              
  • 解码 lib:qrcode.jar                 (官网介绍 -- http://sourceforge.jp/projects/qrcode/)

【一】、编码:

QRCodeEncoderHandler.java

 1 package michael.qrcode;

2

3 import java.awt.Color;

4 import java.awt.Graphics2D;

5 import java.awt.image.BufferedImage;

6 import java.io.File;

7

8 import javax.imageio.ImageIO;

9

10 import com.swetake.util.Qrcode;

11

12 /**

13 * 二维码生成器

14 * @blog http://sjsky.iteye.com

15 * @author Michael

16 */

17 public class QRCodeEncoderHandler {

18

19 /**

20 * 生成二维码(QRCode)图片

21 * @param content

22 * @param imgPath

23 */

24 public void encoderQRCode(String content, String imgPath) {

25 try {

26

27 Qrcode qrcodeHandler = new Qrcode();

28 qrcodeHandler.setQrcodeErrorCorrect('M');

29 qrcodeHandler.setQrcodeEncodeMode('B');

30 qrcodeHandler.setQrcodeVersion(7);

31

32 System.out.println(content);

33 byte[] contentBytes = content.getBytes("gb2312");

34

35 BufferedImage bufImg = new BufferedImage(140, 140,

36 BufferedImage.TYPE_INT_RGB);

37

38 Graphics2D gs = bufImg.createGraphics();

39

40 gs.setBackground(Color.WHITE);

41 gs.clearRect(0, 0, 140, 140);

42

43 // 设定图像颜色 > BLACK

44 gs.setColor(Color.BLACK);

45

46 // 设置偏移量 不设置可能导致解析出错

47 int pixoff = 2;

48 // 输出内容 > 二维码

49 if (contentBytes.length > 0 && contentBytes.length < 120) {

50 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);

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

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

53 if (codeOut[j][i]) {

54 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);

55 }

56 }

57 }

58 } else {

59 System.err.println("QRCode content bytes length = "

60 + contentBytes.length + " not in [ 0,120 ]. ");

61 }

62

63 gs.dispose();

64 bufImg.flush();

65

66 File imgFile = new File(imgPath);

67

68 // 生成二维码QRCode图片

69 ImageIO.write(bufImg, "png", imgFile);

70

71 } catch (Exception e) {

72 e.printStackTrace();

73 }

74

75 }

76

77 /**

78 * @param args the command line arguments

79 */

80 public static void main(String[] args) {

81 String imgPath = "D:/test/twocode/Michael_QRCode.png";

82

83 String content = "Hello 大大、小小,welcome to QRCode!"

84 + "\nMyblog [ http://sjsky.iteye.com ]"

85 + "\nEMail [ sjsky007@gmail.com ]" + "\nTwitter [ @suncto ]";

86

87 QRCodeEncoderHandler handler = new QRCodeEncoderHandler();

88 handler.encoderQRCode(content, imgPath);

89

90 System.out.println("encoder QRcode success");

91 }

92 }

【二】、解码: 
 QRCodeDecoderHandler.java

 1 package michael.qrcode;

2

3 import java.awt.image.BufferedImage;

4 import java.io.File;

5 import java.io.IOException;

6

7 import javax.imageio.ImageIO;

8

9 import jp.sourceforge.qrcode.QRCodeDecoder;

10 import jp.sourceforge.qrcode.data.QRCodeImage;

11 import jp.sourceforge.qrcode.exception.DecodingFailedException;

12

13 /**

14 * @blog http://sjsky.iteye.com

15 * @author Michael

16 */

17 public class QRCodeDecoderHandler {

18

19 /**

20 * 解码二维码

21 * @param imgPath

22 * @return String

23 */

24 public String decoderQRCode(String imgPath) {

25

26 // QRCode 二维码图片的文件

27 File imageFile = new File(imgPath);

28

29 BufferedImage bufImg = null;

30 String decodedData = null;

31 try {

32 bufImg = ImageIO.read(imageFile);

33

34 QRCodeDecoder decoder = new QRCodeDecoder();

35 decodedData = new String(decoder.decode(new J2SEImage(bufImg)));

36

37 // try {

38 // System.out.println(new String(decodedData.getBytes("gb2312"),

39 // "gb2312"));

40 // } catch (Exception e) {

41 // // TODO: handle exception

42 // }

43 } catch (IOException e) {

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

45 e.printStackTrace();

46 } catch (DecodingFailedException dfe) {

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

48 dfe.printStackTrace();

49 }

50 return decodedData;

51 }

52

53 /**

54 * @param args the command line arguments

55 */

56 public static void main(String[] args) {

57 QRCodeDecoderHandler handler = new QRCodeDecoderHandler();

58 String imgPath = "d:/test/twocode/Michael_QRCode.png";

59 String decoderContent = handler.decoderQRCode(imgPath);

60 System.out.println("解析结果如下:");

61 System.out.println(decoderContent);

62 System.out.println("========decoder success!!!");

63 }

64

65 class J2SEImage implements QRCodeImage {

66 BufferedImage bufImg;

67

68 public J2SEImage(BufferedImage bufImg) {

69 this.bufImg = bufImg;

70 }

71

72 public int getWidth() {

73 return bufImg.getWidth();

74 }

75

76 public int getHeight() {

77 return bufImg.getHeight();

78 }

79

80 public int getPixel(int x, int y) {

81 return bufImg.getRGB(x, y);

82 }

83

84 }

85 }

转载请注明来自:Michael's blog @ http://sjsky.iteye.com 

以上是 Java实现二维码QRCode的编码和解码 的全部内容, 来源链接: utcz.com/z/393332.html

回到顶部