package com.matrix.component.tools; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ImageUtil { public static void main(String[] args) throws Exception { // drawString(); fonts(); // System.out.println(4 % 2); } public static void drawString(BufferedImage bimg, ImageMerge imgMerger) throws IOException, FileNotFoundException { // 得到Graphics2D 对象 Graphics2D g2d = (Graphics2D) bimg.getGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 设置颜色和画笔粗细57bec0 g2d.setColor(toColorFromString(imgMerger.getColor())); g2d.setFont(new Font(imgMerger.getFontFamily(), Font.ITALIC, imgMerger.getFontSize())); // 绘制图案或文字 // g2d.drawString("红星国际会展中心", 445, 630); drawStringWithSpan(g2d, imgMerger.getText(), imgMerger.getX(), imgMerger.getY(), imgMerger.getWordSpacing()); } /** * 绘制字符间距 */ private static void drawStringWithSpan(Graphics2D g2d, String text, int i, int y, int k) { List txtList = new ArrayList<>(); // 匹配中英文 String s = "\\d+.\\d+|\\w+|[\u4e00-\u9fa5]"; Pattern pattern = Pattern.compile(s); Matcher ma = pattern.matcher(text); while (ma.find()) { txtList.add(ma.group()); } int x = i; for (int m = 0; m < txtList.size(); m++) { String target = txtList.get(m); g2d.drawString(target, x, y); // 没写一个字就增加一个间距,2个哦英文单词占用1个中文字符空间 int b = (target.length() / 2) + (target.length() % 2); x += b * (g2d.getFont().getSize() + k); } } /** * 小图片贴到大图片形成一张图(合成) * @author:liuyc * @throws IOException * @time:2016年5月27日 下午5:51:20 public static final void overlapImage(ImageMerge imgMerge) throws IOException { BufferedImage big = ImageIO.read(new File(imgMerge.getSrcPath())); BufferedImage small = ImageIO.read(new File(imgMerge.getSmallPath())); Graphics2D g = big.createGraphics(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, imgMerge.getAlpha())); g.drawImage(small, imgMerge.getX(), imgMerge.getY(), imgMerge.getWidth(), imgMerge.getHeight(), null); g.dispose(); ImageIO.write(big, imgMerge.getOutFilePath().split("\\.")[1], new File(imgMerge.getOutFilePath())); } */ public static Color toColorFromString(String colorStr) { Color color = new Color(Integer.parseInt(colorStr, 16)); return color; } public static void fonts() { String[] fonts = GraphicsEnvironment // GraphicsEnvironment(抽象类) 图形环境类 .getLocalGraphicsEnvironment() // 获取本地图形环境 .getAvailableFontFamilyNames(); // 获取可用字体family名 int fontCount = 0; // 字体数统计 for (String font : fonts) { fontCount++; System.out.println(font); } System.out.println("系统字体数:" + fontCount); } public static void downloadPicture(String imgUrl,String savePath) { URL url = null; int imageNumber = 0; try { InputStream inputStream = null; HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(imgUrl).openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"); httpURLConnection.setRequestProperty("Accept-Encoding", "gzip"); httpURLConnection.setRequestProperty("Referer","no-referrer"); httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setReadTimeout(20000); inputStream = httpURLConnection.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath)); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { output.write(buffer, 0, length); } byte[] context=output.toByteArray(); fileOutputStream.write(output.toByteArray()); inputStream.close(); fileOutputStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }