码迷,mamicode.com
首页 > 其他好文 > 详细

二维码图片生成

时间:2017-05-27 12:19:08      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:放大   util   graphics   path   package   except   public   大小   draw   

  new QREncoder().encoderQRCode("12345678910", "D:/YYYYY.png", 10);

 

 

package com.XXXX.qrcode;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class QREncoder {

/**
* 生成二维码(QRCode)图片
* @param content 二维码内容
* @param imgPath 生成图片地址
* @param size 二维码大小
*/
public void encoderQRCode(String content, String imgPath, int size) {
int ver = 3;
int s = 2;
int imgSize = 61;
if (size == 0) {

} else if (size == 1) {
ver = 3;
s = 3;
imgSize = 91;
} else if (size == 2) {
ver = 3;
s = 4;
imgSize = 120;
} else if (size == 3) {
ver = 3;
s = 5;
imgSize = 150;
} else if (size == 4) {
ver = 4;
s = 5;
imgSize = 168;
} else if (size == 5) {
ver = 5;
s = 5;
imgSize = 190;
} else if (size == 6) {
ver = 5;
s = 6;
imgSize = 228;
} else if (size == 7) {
ver = 5;
s = 7;
imgSize = 260;
} else if (size == 8) {
ver = 6;
s = 8;
imgSize = 330;
} else if (size == 9) {
ver = 6;
s = 9;
imgSize = 370;
} else {
throw new RuntimeException("二维码尺寸不合法,size只包含:0-9");
}

try {

Qrcode qrcodeHandler = new Qrcode();
qrcodeHandler.setQrcodeErrorCorrect(‘M‘);
qrcodeHandler.setQrcodeEncodeMode(‘B‘);
qrcodeHandler.setQrcodeVersion(ver);
byte[] contentBytes = content.getBytes("GBK");

BufferedImage bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);

Graphics2D gs = bufImg.createGraphics();

// 设置背景颜色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);

// 设定图像颜色 > BLACK
gs.setColor(Color.BLACK);

// 设置偏移量 不设置可能导致解析出错
int pixoff = 2;
// 输出内容 > 二维码
if (contentBytes.length > 0 && contentBytes.length < 2331) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * s + pixoff, i * s + pixoff, s, s);
}
}
}
} else {
System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,2331 ]. ");
}

gs.dispose();
bufImg.flush();

File imgFile = new File(imgPath);

// 生成二维码QRCode图片
ImageIO.write(bufImg, "png", imgFile);

// scale("D:/QRCode.png", "D:/XXXX.png", 2, false);
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 缩放图像
* @param srcImageFile 源图像文件地址
* @param result 缩放后的图像地址
* @param scale 缩放比例
* @param flag 缩放选择:true 放大; false 缩小;
*/
public void scale(String srcImageFile, String result, int scale, boolean flag) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
// System.out.println(width);
// System.out.println(height);

if (flag) {
// 放大
width = width * scale;
height = height * scale;
} else {
// 缩小
width = width / scale;
height = height / scale;
}
width = 200;
height = 200;

Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
ImageIO.write(tag, "png", new File(result));// 输出到文件流

} catch (IOException e) {
e.printStackTrace();
}
}

}

二维码图片生成

标签:放大   util   graphics   path   package   except   public   大小   draw   

原文地址:http://www.cnblogs.com/xiaohaizhuimeng/p/6912055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!