在下载的压缩包里,有一个“应用开发示例”目录,里面有各种api调用的示例。
在示例包里,有个MyQRcodeCell.java文件里展示了getImage的用法- public byte[] getImage(int width, int height) {
-
- byte[] contentBytes = codeContent.getBytes();
- code = new Qrcode();
- code.setQrcodeErrorCorrect(codeErrorCorrect);
- code.setQrcodeEncodeMode(codeEncodeMode);
- code.setQrcodeVersion(codeVersion);
- BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics2D g2d = bufImg.createGraphics();
- g2d.setBackground(Color.WHITE);
- g2d.clearRect(0, 0, width, height);
- g2d.setColor(Color.BLACK);
- // 设置偏移量 不设置可能导致解析出错
- int pixoffset = 2;
-
- if (contentBytes.length > 0)
- {
- boolean[][] codeOut = code.calQrcode(contentBytes);
- for (int i = 0; i < codeOut.length; i++){
- for (int j = 0; j < codeOut.length; j++){
- if (codeOut[j][i]){
- g2d.fillRect(j * 3 + pixoffset, i * 3 + pixoffset, 3, 3);
- }
- }
- }
- }else{
- System.err.println("内容长度不对");
- }
- g2d.dispose();
- bufImg.flush();
- ByteArrayOutputStream output = new ByteArrayOutputStream(20000);
- try {
- ImageIO.write(bufImg, "png", output);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return output.toByteArray();
- }
复制代码
|