67,513
社区成员
发帖
与我相关
我的任务
分享
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.bcsfll.postman.mail.utility;
import com.swetake.util.Qrcode;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.bcsfll.postman.base.utility.ConfigUtil;
/**
*
* @author Tone
*/
public class QRCodeUtils {
public static byte[] createQrCode(String codeData) {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M'); // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7); // 设置Qrcode包的版本
BufferedImage bufferedImage = null;
ByteArrayOutputStream byteArrayOutputStream = null;
codeData = ConfigUtil.getInstance().getValueByKey("qr.url") + codeData;
try {
byte[] codeBytes = codeData.getBytes("UTF-8"); // 字符集
bufferedImage = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
// createGraphics // 创建图层
Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setBackground(Color.WHITE); // 设置背景颜色(白色)
graphics2D.clearRect(0, 0, 140, 140); // 矩形 X、Y、width、height
graphics2D.setColor(Color.BLACK); // 设置图像颜色(黑色)
if (codeBytes.length > 0 && codeBytes.length < 120) {
boolean[][] b = qrcode.calQrcode(codeBytes);
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[j][i]) {
graphics2D.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
}
}
}
}
graphics2D.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
bufferedImage.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源
byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
} catch (IOException ex) {
Logger.getLogger(QRCodeUtils.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public static byte[] changePicSize(InputStream inputStream) {
int new_w = 40; //输出的图片宽度
int new_h = 40; //输出的图片高度
//图片字节数组返回流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Image image = null;
// Toolkit toolkit = Toolkit.getDefaultToolkit();
Applet applet = new Applet();
MediaTracker mediaTracker = new MediaTracker(applet);
try {
// image = toolkit.getImage(path);//读取图片
image=ImageIO.read(inputStream);
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
} catch (Exception e) {
Logger.getLogger(QRCodeUtils.class.getName()).log(Level.SEVERE, null, e);
}
if (image.getWidth(null) == -1) {
//System.out.println(" can't read,retry!" + "<BR>");
return null;
} else {
BufferedImage bufferedImage = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.createGraphics();
graphics.setColor(Color.white);
graphics.fillRect(0, 0, new_w, new_h);
graphics.drawImage(image, 0, 0, new_w, new_h, null);
graphics.dispose();
try {
ImageIO.write(bufferedImage, "JPEG", byteArrayOutputStream);
} catch (IOException e) {
Logger.getLogger(QRCodeUtils.class.getName()).log(Level.SEVERE, null, e);
}
}
return byteArrayOutputStream.toByteArray();
}
public static byte[] createQrCode(String codeData, InputStream inputStream) {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M'); // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7); // 设置Qrcode包的版本
BufferedImage bufferedImage = null;
ByteArrayOutputStream byteArrayOutputStream = null;
codeData = ConfigUtil.getInstance().getValueByKey("qr.url") + codeData;
try {
byte[] codeBytes = codeData.getBytes("UTF-8"); // 字符集
bufferedImage = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
// createGraphics // 创建图层
Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setBackground(Color.WHITE); // 设置背景颜色(白色)
graphics2D.clearRect(0, 0, 140, 140); // 矩形 X、Y、width、height
graphics2D.setColor(Color.BLACK); // 设置图像颜色(黑色)
if (codeBytes.length > 0 && codeBytes.length < 120) {
boolean[][] b = qrcode.calQrcode(codeBytes);
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[j][i]) {
graphics2D.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
}
}
}
}
//读取头像图片,调整图片大小。
byte[] bytes = changePicSize(inputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
BufferedImage bufferedImage_Logo = ImageIO.read(byteArrayInputStream);
graphics2D.drawImage(bufferedImage_Logo, 50, 50, null);
graphics2D.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
bufferedImage.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源
byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
} catch (IOException ex) {
Logger.getLogger(QRCodeUtils.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}