From 0881acf434510a6827d103403131a3392c69bdc8 Mon Sep 17 00:00:00 2001 From: Menethil Date: Sun, 29 Jul 2018 03:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4=E5=AD=97?= =?UTF-8?q?=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litemall/admin/CreateShareImageTest.java | 27 +++++++++++++++++++ .../litemall/core/qcode/QCodeService.java | 10 ++++--- 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 litemall-admin-api/src/test/java/org/linlinjava/litemall/admin/CreateShareImageTest.java diff --git a/litemall-admin-api/src/test/java/org/linlinjava/litemall/admin/CreateShareImageTest.java b/litemall-admin-api/src/test/java/org/linlinjava/litemall/admin/CreateShareImageTest.java new file mode 100644 index 00000000..317b8aa3 --- /dev/null +++ b/litemall-admin-api/src/test/java/org/linlinjava/litemall/admin/CreateShareImageTest.java @@ -0,0 +1,27 @@ +package org.linlinjava.litemall.admin; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.linlinjava.litemall.core.qcode.QCodeService; +import org.linlinjava.litemall.db.domain.LitemallGoods; +import org.linlinjava.litemall.db.service.LitemallGoodsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@WebAppConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class CreateShareImageTest { + @Autowired + QCodeService qCodeService; + @Autowired + LitemallGoodsService litemallGoodsService; + + @Test + public void test() { + LitemallGoods good = litemallGoodsService.findById(1181010); + qCodeService.createGoodShareImage(good.getId().toString(), good.getPicUrl(), good.getName()); + } +} diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/qcode/QCodeService.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/qcode/QCodeService.java index 81ed380d..df0a2d0d 100644 --- a/litemall-core/src/main/java/org/linlinjava/litemall/core/qcode/QCodeService.java +++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/qcode/QCodeService.java @@ -46,6 +46,8 @@ public class QCodeService { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); + } catch (FontFormatException e) { + e.printStackTrace(); } } @@ -66,7 +68,7 @@ public class QCodeService { * @return * @throws IOException */ - private byte[] drawPicture(InputStream qrCodeImg, String goodPicUrl, String goodName) throws IOException { + private byte[] drawPicture(InputStream qrCodeImg, String goodPicUrl, String goodName) throws IOException, FontFormatException { //底图 ClassPathResource redResource = new ClassPathResource("back.jpg"); BufferedImage red = ImageIO.read(redResource.getInputStream()); @@ -111,10 +113,12 @@ public class QCodeService { return bs.toByteArray(); } - private void drawTextInImg(BufferedImage baseImage, String textToWrite, int x, int y) { + private void drawTextInImg(BufferedImage baseImage, String textToWrite, int x, int y) throws IOException, FontFormatException { Graphics2D g2D = (Graphics2D) baseImage.getGraphics(); g2D.setColor(new Color(167, 136, 69)); - g2D.setFont(new Font("黑体", Font.PLAIN, 42)); + + //TODO 注意,这里的字体必须安装在服务器上 + g2D.setFont(new Font("Microsoft YaHei", Font.PLAIN, 42)); g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2D.drawString(textToWrite, x, y);