7 changed files with 254 additions and 37 deletions
@ -1,33 +1,150 @@ |
|||||
package com.dreamchaser.depository_manage; |
package com.dreamchaser.depository_manage; |
||||
|
|
||||
import com.alibaba.fastjson.JSONObject; |
|
||||
import com.dreamchaser.depository_manage.config.JM_3DES; |
import com.google.zxing.BarcodeFormat; |
||||
import com.dreamchaser.depository_manage.config.QyWxConfig; |
import com.google.zxing.EncodeHintType; |
||||
import com.dreamchaser.depository_manage.service.impl.QyWxOperationService; |
import com.google.zxing.WriterException; |
||||
|
import com.google.zxing.client.j2se.MatrixToImageWriter; |
||||
|
import com.google.zxing.common.BitMatrix; |
||||
|
import com.google.zxing.oned.Code128Writer; |
||||
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; |
||||
|
import org.apache.commons.lang.StringUtils; |
||||
import org.junit.Test; |
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
|
||||
import org.springframework.boot.test.context.SpringBootTest; |
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
|
||||
import java.util.ArrayList; |
import javax.imageio.ImageIO; |
||||
import java.util.Collections; |
import java.awt.*; |
||||
import java.util.List; |
import java.awt.image.BufferedImage; |
||||
import java.util.Random; |
import java.io.File; |
||||
|
import java.io.IOException; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
@SpringBootTest |
@SpringBootTest |
||||
@RunWith(SpringRunner.class) |
@RunWith(SpringRunner.class) |
||||
public class TestOther { |
public class TestOther { |
||||
|
|
||||
|
/** |
||||
|
* 条形码宽度 |
||||
|
*/ |
||||
|
public static final int WIDTH = 300; |
||||
|
/** |
||||
|
* 条形码高度 |
||||
|
*/ |
||||
|
public static final int HEIGHT = 52; |
||||
|
/** |
||||
|
* 加文字 条形码 |
||||
|
*/ |
||||
|
public static final int WORD_HEIGHT = 80; |
||||
|
|
||||
|
/** |
||||
|
* 设置 条形码参数 |
||||
|
*/ |
||||
|
public Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() { |
||||
|
public static final long serialVersionUID = 1L; |
||||
|
|
||||
|
{ |
||||
|
// 设置编码方式
|
||||
|
put(EncodeHintType.CHARACTER_SET, "utf-8"); |
||||
|
// 容错级别 这里选择最高H级别
|
||||
|
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); |
||||
|
put(EncodeHintType.MARGIN, 1); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
@Test |
@Test |
||||
public void Test() { |
public void Test() throws IOException { |
||||
ArrayList<String> list = new ArrayList<String>(); |
BufferedImage image = insertWords(getBarCode("123456"), "123456"); |
||||
list.add("PangFuZhen"); |
ImageIO.write(image, "jpg", new File("D:/superMan.png")); |
||||
list.add("KaiXinGuo"); |
} |
||||
list.add("WoBenShanLiang_3"); |
|
||||
System.out.println(list.contains("PangFuZhen")); |
/** |
||||
System.out.println(list.contains("PangFuZhen1")); |
* 生成 图片缓冲 |
||||
|
* |
||||
|
* @param vaNumber VA 码 |
||||
|
* @return 返回BufferedImage |
||||
|
* @author fxbin |
||||
|
*/ |
||||
|
public BufferedImage getBarCode(String vaNumber) { |
||||
|
try { |
||||
|
Code128Writer writer = new Code128Writer(); |
||||
|
// 编码内容, 编码类型, 宽度, 高度, 设置参数
|
||||
|
BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints); |
||||
|
return MatrixToImageWriter.toBufferedImage(bitMatrix); |
||||
|
} catch (WriterException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 把带logo的二维码下面加上文字 |
||||
|
* |
||||
|
* @param image 条形码图片 |
||||
|
* @param words 文字 |
||||
|
* @return 返回BufferedImage |
||||
|
* @author fxbin |
||||
|
*/ |
||||
|
public BufferedImage insertWords(BufferedImage image, String words) { |
||||
|
// 新的图片,把带logo的二维码下面加上文字
|
||||
|
if (StringUtils.isNotEmpty(words)) { |
||||
|
BufferedImage outImage = new BufferedImage(WIDTH, WORD_HEIGHT, BufferedImage.TYPE_INT_RGB); |
||||
|
Graphics2D g2d = outImage.createGraphics(); |
||||
|
// 抗锯齿
|
||||
|
setGraphics2D(g2d); |
||||
|
// 设置白色
|
||||
|
setColorWhite(g2d); |
||||
|
// 画条形码到新的面板
|
||||
|
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); |
||||
|
// 画文字到新的面板
|
||||
|
Color color = new Color(0, 0, 0); |
||||
|
g2d.setColor(color); |
||||
|
// 字体、字型、字号
|
||||
|
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 16)); |
||||
|
//文字长度
|
||||
|
int strWidth = g2d.getFontMetrics().stringWidth(words); |
||||
|
//总长度减去文字长度的一半 (居中显示)
|
||||
|
int wordStartX = (WIDTH - strWidth) / 2; |
||||
|
//height + (outImage.getHeight() - height) / 2 + 12
|
||||
|
int wordStartY = HEIGHT + 20; |
||||
|
// 画文字
|
||||
|
g2d.drawString(words, wordStartX, wordStartY); |
||||
|
g2d.dispose(); |
||||
|
outImage.flush(); |
||||
|
return outImage; |
||||
|
} |
||||
|
return null; |
||||
} |
} |
||||
|
|
||||
|
/** |
||||
|
* 设置 Graphics2D 属性 (抗锯齿) |
||||
|
* |
||||
|
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制 |
||||
|
*/ |
||||
|
public void setGraphics2D(Graphics2D g2d) { |
||||
|
// 消除画图锯齿
|
||||
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
||||
|
// 消除文字锯齿
|
||||
|
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
||||
|
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT); |
||||
|
Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); |
||||
|
g2d.setStroke(s); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置背景为白色 |
||||
|
* |
||||
|
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制 |
||||
|
*/ |
||||
|
public void setColorWhite(Graphics2D g2d) { |
||||
|
g2d.setColor(Color.WHITE); |
||||
|
//填充整个屏幕
|
||||
|
g2d.fillRect(0, 0, 302, 113); |
||||
|
//设置笔刷
|
||||
|
g2d.setColor(Color.BLACK); |
||||
|
} |
||||
|
|
||||
|
|
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue