From 6a80821cea1d71ca4cb6c45239274898f46143f2 Mon Sep 17 00:00:00 2001 From: yujian <yujian@123.com> Date: 星期三, 05 六月 2019 14:55:48 +0800 Subject: [PATCH] 分类 + 封禁用户方法封装 --- fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java | 263 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 263 insertions(+), 0 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java index 7ecbde2..18f7349 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java @@ -753,6 +753,269 @@ return null; } + + /** + * 缁樺埗澶х殑鍟嗗搧鍔ㄦ�佸垎浜浘 + * + * @param qrcodeStream + * @param portrait + * @param goods + * @return + */ + public static InputStream drawGoodsSharePicture(InputStream qrcodeStream, InputStream portrait, + List<TaoBaoGoodsBrief> goodsList) { + + String fontPath = "/usr/share/fonts/PingFang_Medium.ttf"; + String os = System.getProperty("os.name"); + if (os.toLowerCase().startsWith("win")) { + fontPath = "D:/PingFang_Medium.ttf"; + } + + String fontBoldPath = "/usr/share/fonts/PingFang_Heavy_0.ttf"; + if (os.toLowerCase().startsWith("win")) { + fontBoldPath = "D:/PingFang_Heavy_0.ttf"; + } + + final BufferedImage targetImg = new BufferedImage(1420, 1334, BufferedImage.TYPE_INT_RGB); + HashMap<Key, Object> mapH = new HashMap<Key, Object>(); + mapH.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 鎶楅敮榻柯狅紙鎶楅敮榻挎�诲紑鍏筹級聽聽 + mapH.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);// 鏂囧瓧鎶楅敮榻柯� + + final Graphics2D g2d = (Graphics2D) targetImg.getGraphics(); + g2d.setRenderingHints(mapH); + + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, 1420, 1334); + Font font = null; + try { + font = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(30.0f); + } catch (FontFormatException e1) { + e1.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } + + Font boldFont = null; + try { + boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(50.0f); + } catch (FontFormatException e2) { + e2.printStackTrace(); + } catch (IOException e2) { + e2.printStackTrace(); + } + g2d.setFont(font); + + + try { + // 鐢荤涓�寮� + for (int i = 0; i < goodsList.size(); i++) { + TaoBaoGoodsBrief goods = goodsList.get(i); + BigDecimal couplePrice = TaoBaoUtil.getAfterUseCouplePrice(goods); + // 绗竴寮犲浘闇�瑕佹湁浠锋牸淇℃伅 + if (i == 0) { + int topX = 50; + int topY = 50; + + + // 鐢诲ぇ鍥� 璧峰鐐瑰潗鏍囦负锛�50,50锛� + int widht = 650; + int height = 650; + InputStream goodsPicture = TaoBaoHttpUtil.getAsInputStream(goods.getPictUrl().replace("_.webp", "").replace("_220x220", "")); + BufferedImage picImage = ImageIO.read(goodsPicture); + picImage = ImageUtil.zoomInImage(picImage, widht, height); + g2d.drawImage(picImage, topX, topX, null); + + // 鐢昏竟妗� + g2d.setColor(new Color(224, 224, 224)); + g2d.setStroke(new BasicStroke(1.0f)); + g2d.drawRect(topX - 1, topY - 1, widht + 1, widht + 1); + + // 鐢婚�忔槑鑳屾櫙 + g2d.setColor(new Color(255, 255, 255, 210)); + g2d.fillRect(10, height - 50, widht + 40, 100); + + + // 鐢诲埜鍚庝环閲戦 + boldFont = boldFont.deriveFont(42.0f); + g2d.setFont(boldFont); + g2d.setColor(new Color(229, 0, 92)); + + g2d.drawString("楼", 70 + 10, height); + g2d.drawString(couplePrice + "", 70 + 45, height); + + // 鐢诲師浠� + g2d.setColor(new Color(102, 102, 102)); + font = font.deriveFont(24.0f); + g2d.setFont(font); + + String zkPrice = new BigDecimal((couplePrice + "").replace("楼", "")).add(goods.getCouponAmount()) + .toString(); + g2d.drawString("楼 " + zkPrice, 70 + 15, height + 30); + + // 鐢诲垹闄ょ嚎 + FontMetrics fm = g2d.getFontMetrics(font); + int textLength = fm.stringWidth(zkPrice); + g2d.setStroke(new BasicStroke(2.0f)); + g2d.drawLine(70 + 20, height + 21, 90 + 10 + textLength, height + 21); + + BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); + if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) { + g2d.setRenderingHints(mapH); + + // 鍒稿�煎緱闀垮害 + int size = withNoZera.toString().length() * 22; + int place = withNoZera.toString().length() * 22; + + // 鍒哥孩鑹插簳閮� + int quanBGWidht = widht-100 - place; + int quanBGHeight = height - 33; + g2d.setColor(new Color(229, 0, 92)); + g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10); + + // 鐢诲埜瀛� + boldFont = boldFont.deriveFont(40.0f); + g2d.setFont(boldFont); + g2d.setColor(new Color(255, 255, 255)); + g2d.drawString("鍒�",quanBGWidht + 8, quanBGHeight + 48); + + // 鍒哥櫧鑹插簳閮� + g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4 , 70 + size, 62, 10, 10); + + // 鐢诲埜闈㈠�� + g2d.setFont(boldFont); + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("楼 " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48); + } + + } else { + // 璁$畻宸︿笂瑙掑潗鏍� + int topX = 0; + int topY = 0; + if (i == 1) + topX = 50 + (315 + 20) * 0; + else if (i == 2 || i % 2 != 0)// 2,3,5,7 + topX = 50 + (315 + 20) * (i % 2 + 1); + else if (i % 2 == 0)// 4,6,8 + topX = 50 + (315 + 20) * 3; + + if (i == 1 || i == 2) + topY = 50 + 650 + 20; + else if (i == 3 || i == 4) + topY = 50; + else if (i == 5 || i == 6) + topY = 50 + (315 + 20) * 1; + else if (i == 7 || i == 8) + topY = 50 + (315 + 20) * 2; + + // 鐢诲ぇ鍥� + InputStream goodsPicture = TaoBaoHttpUtil + .getAsInputStream(goods.getPictUrl().replace("_.webp", "")); + BufferedImage picImage = ImageIO.read(goodsPicture); + picImage = ImageUtil.zoomInImage(picImage, 315, 315); + g2d.drawImage(picImage, topX, topY, null); + + g2d.setColor(new Color(224, 224, 224)); + // 鐢昏竟妗� + g2d.setStroke(new BasicStroke(1.0f)); + g2d.drawRect(topX - 1, topY - 1, 316, 316); + + // 鐢诲埜 + BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); + if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) { +// // 鐢婚�忔槑鑳屾櫙 +// g2d.setColor(new Color(255, 255, 255, 210)); +// g2d.fillRect(topX + 8, topY + 240, 300, 65); + + g2d.setRenderingHints(mapH); + + // 鍒稿�煎緱闀垮害 + int size = withNoZera.toString().length() * 22; + int place = withNoZera.toString().length() * 22; + +// // 鍒哥孩鑹插簳閮� + int quanBGWidht = topX + 170 - place; + int quanBGHeight = topY + 220; + g2d.setColor(new Color(229, 0, 92)); + g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10); + + // 鐢诲埜瀛� + boldFont = boldFont.deriveFont(40.0f); + g2d.setFont(boldFont); + g2d.setColor(new Color(255, 255, 255)); + g2d.drawString("鍒�",quanBGWidht + 8, quanBGHeight + 48); + + // 鍒哥櫧鑹插簳閮� + g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4 , 70 + size, 62, 10, 10); + + // 鐢诲埜闈㈠�� + g2d.setFont(boldFont); + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("楼 " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48); + } + } + } + + g2d.setColor(new Color(247, 247, 247)); + g2d.fillRect(50, 1070, 1320, 214); + + // 鐢讳簩缁寸爜 + + BufferedImage qrcodeImage = ImageIO.read(qrcodeStream); + qrcodeImage = ImageUtil.zoomInImage(qrcodeImage, 170, 170); + + g2d.drawImage(qrcodeImage, 70, 1092, null); + + // 鐢诲ご鍍� + if (portrait != null) { + BufferedImage portraitImg = ImageIO.read(portrait); + // 鏀剧缉澶у皬 + portraitImg = ImageUtil.zoomInImage(portraitImg, 40, 40); + // 鍦嗚 + portraitImg = ImageUtil.roundImage(portraitImg, 10); + + g2d.drawImage(portraitImg, 70 + (qrcodeImage.getWidth() - portraitImg.getWidth()) / 2, + 1092 + (qrcodeImage.getHeight() - portraitImg.getHeight()) / 2, null); + } + + font = font.deriveFont(50.0f); + g2d.setFont(font); + g2d.setColor(new Color(229, 0, 93)); + + g2d.setColor(new Color(102, 102, 102)); + g2d.drawString("闀挎寜璇嗗埆浜岀淮鐮佸厤璐归鍒�", 312, 1150); + + g2d.setColor(new Color(102, 102, 102)); + g2d.drawString("鍏�", 1000, 1150); + + g2d.setColor(new Color(229, 0, 93)); + g2d.drawString(goodsList.size() + "", 1055, 1150); + + g2d.setColor(new Color(102, 102, 102)); + g2d.drawString("涓晢鍝�", 1090, 1150); + + // 鎻愮ず璇� + BufferedImage tips = ImageIO + .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/share/tips1.png")); + tips = ImageUtil.zoomInImage(tips, 850, 65); + g2d.drawImage(tips, 312, 1190, null); + g2d.dispose(); + + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + ImageIO.write(targetImg, "JPEG", aos); + InputStream is = new ByteArrayInputStream(aos.toByteArray()); + return is; + + } catch (Exception e) { + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + return null; + } + + // 鐢诲晢鍝佸垎浜浘 public static InputStream drawGoodsShareImgHCJ(InputStream qrcodeStream, InputStream portrait, TaoBaoGoodsBrief goods) throws Exception { -- Gitblit v1.8.0