提交 8bb0b506 编写于 作者: O o2null

使用appche base64

上级 8abedf20
...@@ -12,7 +12,8 @@ import java.util.Map; ...@@ -12,7 +12,8 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
...@@ -20,200 +21,203 @@ import com.google.zxing.EncodeHintType; ...@@ -20,200 +21,203 @@ import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter; import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
public class CodeUtil { public class CodeUtil {
private static final int QRCOLOR = 0xFF000000; // 默认是黑色 private static final int QRCOLOR = 0xFF000000; // 默认是黑色
private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色 private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色
private static final int WIDTH = 300; // 二维码宽 private static final int WIDTH = 300; // 二维码宽
private static final int HEIGHT = 300; // 二维码高 private static final int HEIGHT = 300; // 二维码高
/** /**
*生成带logo的二维码图片 * 生成带logo的二维码图片
* @param logoFile /logo图片文件 *
* @param codeFile /二维码图片 * @param logoFile /logo图片文件
* @param qrUrl /二维码存储的信息:vcard格式 * @param codeFile /二维码图片
* @param note /二维码描述信息 * @param qrUrl /二维码存储的信息:vcard格式
*/ * @param note /二维码描述信息
public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) { */
try { public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); try {
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints); // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < WIDTH; x++) { // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE); for (int y = 0; y < HEIGHT; y++) {
} image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
} }
}
int width = image.getWidth();
int height = image.getHeight(); int width = image.getWidth();
if (Objects.nonNull(logoFile) && logoFile.exists()) { int height = image.getHeight();
// 构建绘图对象 if (Objects.nonNull(logoFile) && logoFile.exists()) {
Graphics2D g = image.createGraphics(); // 构建绘图对象
// 读取Logo图片 Graphics2D g = image.createGraphics();
BufferedImage logo = ImageIO.read(logoFile); // 读取Logo图片
// 开始绘制logo图片 BufferedImage logo = ImageIO.read(logoFile);
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null); // 开始绘制logo图片
g.dispose(); g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
logo.flush(); g.dispose();
} logo.flush();
}
// 自定义文本描述
if (StringUtils.isNotEmpty(note)) { // 自定义文本描述
// 新的图片,把带logo的二维码下面加上文字 if (StringUtils.isNotEmpty(note)) {
BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR); // 新的图片,把带logo的二维码下面加上文字
Graphics2D outg = outImage.createGraphics(); BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
// 画二维码到新的面板 Graphics2D outg = outImage.createGraphics();
outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); // 画二维码到新的面板
// 画文字到新的面板 outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
outg.setColor(Color.BLACK); // 画文字到新的面板
outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号 outg.setColor(Color.BLACK);
int strWidth = outg.getFontMetrics().stringWidth(note); outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
if (strWidth > WIDTH) { int strWidth = outg.getFontMetrics().stringWidth(note);
// //长度过长就截取前面部分 if (strWidth > WIDTH) {
// 长度过长就换行 // //长度过长就截取前面部分
String note1 = note.substring(0, note.length() / 2); // 长度过长就换行
String note2 = note.substring(note.length() / 2, note.length()); String note1 = note.substring(0, note.length() / 2);
int strWidth1 = outg.getFontMetrics().stringWidth(note1); String note2 = note.substring(note.length() / 2, note.length());
int strWidth2 = outg.getFontMetrics().stringWidth(note2); int strWidth1 = outg.getFontMetrics().stringWidth(note1);
outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12); int strWidth2 = outg.getFontMetrics().stringWidth(note2);
BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR); outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
Graphics2D outg2 = outImage2.createGraphics(); BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null); Graphics2D outg2 = outImage2.createGraphics();
outg2.setColor(Color.BLACK); outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号 outg2.setColor(Color.BLACK);
outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5); outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
outg2.dispose(); outg2.drawString(note2, 200 - strWidth2 / 2,
outImage2.flush(); outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
outImage = outImage2; outg2.dispose();
} else { outImage2.flush();
outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字 outImage = outImage2;
} } else {
outg.dispose(); outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
outImage.flush(); }
image = outImage; outg.dispose();
} outImage.flush();
image = outImage;
image.flush(); }
ImageIO.write(image, "png", codeFile); // TODO image.flush();
if (Objects.nonNull(logoFile) && logoFile.exists()) {
logoFile.delete(); ImageIO.write(image, "png", codeFile); // TODO
} if (Objects.nonNull(logoFile) && logoFile.exists()) {
} catch (Exception e) { logoFile.delete();
e.printStackTrace(); }
} } catch (Exception e) {
} e.printStackTrace();
}
/** }
*生成带logo的二维码图片
* @param logoFile /logo图片文件 /**
* @param codeFile /二维码图片 * 生成带logo的二维码图片
* @param qrUrl /二维码存储的信息:vcard格式 *
* @param note /二维码描述信息 * @param logoFile /logo图片文件
*/ * @param codeFile /二维码图片
public static byte[] drawLogoQRCodeByte(BufferedImage logo, ByteArrayOutputStream codeFile, String qrUrl, String note) { * @param qrUrl /二维码存储的信息:vcard格式
byte[] bs = null; * @param note /二维码描述信息
try { */
MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); public static byte[] drawLogoQRCodeByte(BufferedImage logo, ByteArrayOutputStream codeFile, String qrUrl,
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 String note) {
BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints); byte[] bs = null;
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色 // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
for (int x = 0; x < WIDTH; x++) { BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
for (int y = 0; y < HEIGHT; y++) { BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
} // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
} for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
int width = image.getWidth(); image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
int height = image.getHeight(); }
if (Objects.nonNull(logo)) { }
// 构建绘图对象
Graphics2D g = image.createGraphics(); int width = image.getWidth();
// 读取Logo图片 int height = image.getHeight();
//BufferedImage logo = ImageIO.read(logoFile); if (Objects.nonNull(logo)) {
// 开始绘制logo图片 // 构建绘图对象
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null); Graphics2D g = image.createGraphics();
g.dispose(); // 读取Logo图片
logo.flush(); // BufferedImage logo = ImageIO.read(logoFile);
} // 开始绘制logo图片
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
// 自定义文本描述 g.dispose();
if (StringUtils.isNotEmpty(note)) { logo.flush();
// 新的图片,把带logo的二维码下面加上文字 }
BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics(); // 自定义文本描述
// 画二维码到新的面板 if (StringUtils.isNotEmpty(note)) {
outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); // 新的图片,把带logo的二维码下面加上文字
// 画文字到新的面板 BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
outg.setColor(Color.BLACK); Graphics2D outg = outImage.createGraphics();
outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号 // 画二维码到新的面板
int strWidth = outg.getFontMetrics().stringWidth(note); outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
if (strWidth > WIDTH) { // 画文字到新的面板
// //长度过长就截取前面部分 outg.setColor(Color.BLACK);
// 长度过长就换行 outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
String note1 = note.substring(0, note.length() / 2); int strWidth = outg.getFontMetrics().stringWidth(note);
String note2 = note.substring(note.length() / 2, note.length()); if (strWidth > WIDTH) {
int strWidth1 = outg.getFontMetrics().stringWidth(note1); // //长度过长就截取前面部分
int strWidth2 = outg.getFontMetrics().stringWidth(note2); // 长度过长就换行
outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12); String note1 = note.substring(0, note.length() / 2);
BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR); String note2 = note.substring(note.length() / 2, note.length());
Graphics2D outg2 = outImage2.createGraphics(); int strWidth1 = outg.getFontMetrics().stringWidth(note1);
outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null); int strWidth2 = outg.getFontMetrics().stringWidth(note2);
outg2.setColor(Color.BLACK); outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号 BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5); Graphics2D outg2 = outImage2.createGraphics();
outg2.dispose(); outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
outImage2.flush(); outg2.setColor(Color.BLACK);
outImage = outImage2; outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
} else { outg2.drawString(note2, 200 - strWidth2 / 2,
outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字 outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
} outg2.dispose();
outg.dispose(); outImage2.flush();
outImage.flush(); outImage = outImage2;
image = outImage; } else {
} outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
}
image.flush(); outg.dispose();
outImage.flush();
ImageIO.write(image, "png", codeFile); // TODO image = outImage;
bs = codeFile.toByteArray(); }
} catch (Exception e) {
e.printStackTrace(); image.flush();
}
return bs; ImageIO.write(image, "png", codeFile); // TODO
} bs = codeFile.toByteArray();
} catch (Exception e) {
// 用于设置QR二维码参数 e.printStackTrace();
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() { }
private static final long serialVersionUID = 1L; return bs;
{ }
//put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式 // 用于设置QR二维码参数
put(EncodeHintType.MARGIN, 1); private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
} private static final long serialVersionUID = 1L;
}; {
// put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//
/** // 设置QR二维码的纠错级别(H为最高级别)具体级别信息
* 将base64字符解码保存文件 put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
* put(EncodeHintType.MARGIN, 1);
* @param base64Code }
* @param targetPath };
* @throws Exception
*/ /**
public static File decoderBase64File(String targetPath, String base64Code) throws Exception { * 将base64字符解码保存文件
System.out.println("base64Code="+base64Code); *
System.out.println("targetPath="+targetPath); * @param base64Code
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code); * @param targetPath
FileOutputStream out = new FileOutputStream(targetPath); * @throws Exception
out.write(buffer); */
out.close(); public static File decoderBase64File(String targetPath, String base64Code) throws Exception {
File file = new File(targetPath); byte[] buffer = Base64.decodeBase64(base64Code);
return file; FileOutputStream out = new FileOutputStream(targetPath);
} out.write(buffer);
out.close();
File file = new File(targetPath);
return file;
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册