|
@@ -0,0 +1,280 @@
|
|
|
+package cn.com.ctop.common.utils;
|
|
|
+
|
|
|
+import javax.imageio.IIOException;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.swing.*;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.font.FontRenderContext;
|
|
|
+import java.awt.font.GlyphVector;
|
|
|
+import java.awt.font.TextLayout;
|
|
|
+import java.awt.geom.AffineTransform;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class ImageUtils {
|
|
|
+ public BufferedImage toBufferedImage(Image image) {
|
|
|
+ if (image instanceof BufferedImage) {
|
|
|
+ return (BufferedImage) image;
|
|
|
+ }
|
|
|
+
|
|
|
+ // This code ensures that all the pixels in the image are loaded
|
|
|
+ image = new ImageIcon(image).getImage();
|
|
|
+
|
|
|
+ // Determine if the image has transparent pixels; for this method's
|
|
|
+ // implementation, see e661 Determining If an Image Has Transparent Pixels
|
|
|
+ //boolean hasAlpha = hasAlpha(image);
|
|
|
+
|
|
|
+ // Create a buffered image with a format that's compatible with the screen
|
|
|
+ BufferedImage bimage = null;
|
|
|
+ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
|
|
+ try {
|
|
|
+ // Determine the type of transparency of the new buffered image
|
|
|
+ int transparency = Transparency.OPAQUE;
|
|
|
+ /* if (hasAlpha) {
|
|
|
+ transparency = Transparency.BITMASK;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ // Create the buffered image
|
|
|
+ GraphicsDevice gs = ge.getDefaultScreenDevice();
|
|
|
+ GraphicsConfiguration gc = gs.getDefaultConfiguration();
|
|
|
+ bimage = gc.createCompatibleImage(
|
|
|
+ image.getWidth(null), image.getHeight(null), transparency);
|
|
|
+ } catch (HeadlessException e) {
|
|
|
+ // The system does not have a screen
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bimage == null) {
|
|
|
+ // Create a buffered image using the default color model
|
|
|
+ int type = BufferedImage.TYPE_INT_RGB;
|
|
|
+ //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
|
|
|
+ /*if (hasAlpha) {
|
|
|
+ type = BufferedImage.TYPE_INT_ARGB;
|
|
|
+ }*/
|
|
|
+ bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Copy image to buffered image
|
|
|
+ Graphics g = bimage.createGraphics();
|
|
|
+
|
|
|
+ // Paint the image onto the buffered image
|
|
|
+ g.drawImage(image, 0, 0, null);
|
|
|
+ g.dispose();
|
|
|
+
|
|
|
+ return bimage;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void resize(int width, int height, String sourcePath, String targetPath) {
|
|
|
+ File inputFile = new File(sourcePath);
|
|
|
+ File outputFile = new File(targetPath);
|
|
|
+ try {
|
|
|
+ Image sourceImage = ImageIO.read(inputFile);
|
|
|
+ int sourceWidth = sourceImage.getWidth(null);
|
|
|
+ int sourceHeight = sourceImage.getHeight(null);
|
|
|
+ int newWidth = 0;
|
|
|
+ int newHeight = 0;
|
|
|
+ if (sourceWidth / sourceHeight >= width / height) {
|
|
|
+ //放大高度
|
|
|
+ newHeight = height;
|
|
|
+ newWidth = (int) (((float) height / (float) sourceHeight) * sourceWidth);
|
|
|
+ } else {
|
|
|
+ //放大宽度
|
|
|
+ newWidth = width;
|
|
|
+ newHeight = (int) (((float) width / (float) sourceWidth) * sourceHeight);
|
|
|
+ }
|
|
|
+ Image resizeImage = sourceImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
|
|
|
+ BufferedImage bufferedImage = toBufferedImage(resizeImage);
|
|
|
+ bufferedImage = bufferedImage.getSubimage((newWidth - width) / 2, (newHeight - height) / 2, width, height);
|
|
|
+ ImageIO.write(bufferedImage, "jpg", outputFile);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addTextInImage(InputStream input, String content, Color color, Font font) throws IOException {
|
|
|
+ Image image = ImageIO.read(input);
|
|
|
+ int width = image.getWidth(null);
|
|
|
+ int height = image.getHeight(null);
|
|
|
+ BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D graphics2D = bufferedImage.createGraphics();
|
|
|
+ graphics2D.drawImage(image, 0, 0, width, height, null);
|
|
|
+ graphics2D.setColor(color);
|
|
|
+ graphics2D.setFont(font);
|
|
|
+
|
|
|
+ Image borderImage = ImageIO.read(new File("D:/border/images/white01.png"));
|
|
|
+// BufferedImage bufferedImage2 = new BufferedImage(borderImage.getWidth(null),borderImage.getHeight(null),BufferedImage.TYPE_INT_RGB);
|
|
|
+ graphics2D.drawImage(borderImage, 50, height / 2 + 100, null);
|
|
|
+
|
|
|
+// graphics2D.setColor(Color.BLUE.darker().darker());
|
|
|
+// graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ graphics2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
|
|
|
+ graphics2D.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
|
|
|
+// graphics2D.setStroke(new BasicStroke(5.0f ,
|
|
|
+// BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 15.0f, new float[] {10.0f,10.0f}, 5.0f));
|
|
|
+ FontRenderContext frc = graphics2D.getFontRenderContext();
|
|
|
+ int x = 150;
|
|
|
+ int y = height / 2 + 220;
|
|
|
+ if (content.length() < 9) {
|
|
|
+// TextLayout tl = new TextLayout(content, font, frc);
|
|
|
+// Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(5,25));
|
|
|
+// graphics2D.setStroke(new BasicStroke(3.0f));
|
|
|
+// graphics2D.setColor(Color.black);
|
|
|
+// graphics2D.draw(sha);
|
|
|
+// graphics2D.setColor(Color.white);
|
|
|
+// graphics2D.fill(sha);
|
|
|
+// graphics2D.dispose();
|
|
|
+ graphics2D.drawString(content, x, y);
|
|
|
+ } else {
|
|
|
+ graphics2D.drawString(content.substring(0, 8), x, y);
|
|
|
+ graphics2D.drawString(content.substring(8), x, y + 60);
|
|
|
+// TextLayout tl = new TextLayout(content.substring(0,8), font, frc);
|
|
|
+// Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(5,25));
|
|
|
+// graphics2D.setStroke(new BasicStroke(3.0f));
|
|
|
+// graphics2D.setColor(Color.WHITE);
|
|
|
+// graphics2D.draw(sha);
|
|
|
+// graphics2D.setColor(Color.BLACK);
|
|
|
+// graphics2D.fill(sha);
|
|
|
+//// graphics2D.dispose();
|
|
|
+//
|
|
|
+// TextLayout t2 = new TextLayout(content.substring(8), font, frc);
|
|
|
+// Shape sha2 = t2.getOutline(AffineTransform.getTranslateInstance(5,25));
|
|
|
+// graphics2D.setStroke(new BasicStroke(3.0f));
|
|
|
+// graphics2D.setColor(Color.WHITE);
|
|
|
+// graphics2D.draw(sha2);
|
|
|
+// graphics2D.setColor(Color.BLACK);
|
|
|
+// graphics2D.fill(sha);
|
|
|
+//// graphics2D.dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ graphics2D.dispose();
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream("D:/image/cover/" + UUID.randomUUID() + ".jpg");
|
|
|
+ ImageIO.write(bufferedImage, "jpg", fileOutputStream);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getContentLength(String content, Graphics2D graphics2D) {
|
|
|
+ return graphics2D.getFontMetrics(graphics2D.getFont()).charsWidth(content.toCharArray(), 0, content.length());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ ImageUtils i = new ImageUtils();
|
|
|
+ Font font = new Font("微软雅黑", Font.BOLD + Font.PLAIN, 35);
|
|
|
+
|
|
|
+// String content = "测试封面文字内容\n最多可以十五字";
|
|
|
+ Color color = new Color(0, 0, 0, 255);
|
|
|
+ List<String> list = i.getContentList();
|
|
|
+// File file = new File("D:/bg1.jpg");
|
|
|
+ File file = new File("D:/image/target");
|
|
|
+ String[] files = file.list();
|
|
|
+ for (String str : files) {
|
|
|
+ String imagePath = file.getAbsolutePath() + "/" + str;
|
|
|
+ try {
|
|
|
+ File imageFile = new File(imagePath);
|
|
|
+ for (String content : list) {
|
|
|
+ InputStream inputStream = new FileInputStream(imageFile);
|
|
|
+ i.addTextInImage(inputStream, content, color, font);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+// new ImageUtils().resize(720,1280,imageFile,"D:/image/target/"+ UUID.randomUUID()+".jpg");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println();
|
|
|
+//
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getContentList() {
|
|
|
+ List<String> list = new ArrayList<String>();
|
|
|
+ list.add("想尝试一下这种风格");
|
|
|
+ list.add("想看一下效果");
|
|
|
+ list.add("这是个狠人");
|
|
|
+ list.add("难得一见");
|
|
|
+ list.add("每一个都很饱满");
|
|
|
+ list.add("土豪吃法");
|
|
|
+ list.add("怕了怕了");
|
|
|
+ list.add("我反正不敢");
|
|
|
+ list.add("贪吃的后果");
|
|
|
+ list.add("求打败,在线等");
|
|
|
+ list.add("女人心海底针");
|
|
|
+ list.add("撕开赚钱的真相");
|
|
|
+ list.add("这是啥味儿");
|
|
|
+ list.add("爪子的妙用");
|
|
|
+ list.add("把聊天记录删除后..");
|
|
|
+ list.add("章鱼,太狠了");
|
|
|
+ list.add("先下手为强");
|
|
|
+ list.add("爪子的妙用");
|
|
|
+ list.add("听说这是村里的帅哥");
|
|
|
+ list.add("听说这是村里的美女");
|
|
|
+ list.add("没想到,饭还能这样吃");
|
|
|
+ list.add("没想到,水还能这样喝");
|
|
|
+ list.add("没想到,衣服还能这样穿");
|
|
|
+ list.add("没想到,车还能这样骑");
|
|
|
+ list.add("没想到,妆还能这样画");
|
|
|
+ list.add("她扒拉我");
|
|
|
+ list.add("三者必有一伤");
|
|
|
+ list.add("最新机械皮肤");
|
|
|
+ list.add("妹子,这吃的啥");
|
|
|
+ list.add("活下来,算我输");
|
|
|
+ list.add("不哭算我输");
|
|
|
+ list.add("不摔算我输");
|
|
|
+ list.add("送礼物吓傻女友");
|
|
|
+ list.add("是谁在唱歌");
|
|
|
+ list.add("表情包不够,大长腿来凑");
|
|
|
+ list.add("这场面真没见过");
|
|
|
+ list.add("这蔬菜得回家供着");
|
|
|
+ list.add("挖掘机哪家强");
|
|
|
+ list.add("高手过招");
|
|
|
+ list.add("还没怕过谁");
|
|
|
+ list.add("手有点干巴");
|
|
|
+ list.add("准备装箱");
|
|
|
+ list.add("手把手教学");
|
|
|
+ list.add("别怪我不客气");
|
|
|
+ list.add("放大招了!");
|
|
|
+ list.add("难怪我这么穷");
|
|
|
+ list.add("招招毙命");
|
|
|
+ list.add("随时准备辞职");
|
|
|
+ list.add("不秀的李白");
|
|
|
+ list.add("没大的黄忠");
|
|
|
+ list.add("总是碰见猴子的鲁班");
|
|
|
+ list.add("这样哄女友很管用");
|
|
|
+ list.add("三年的聊天记录");
|
|
|
+ list.add("用它不怕女神追不到");
|
|
|
+ list.add("就靠你了");
|
|
|
+ list.add("比谁快!");
|
|
|
+ list.add("城里人真会玩");
|
|
|
+ list.add("斗图我还没输过谁");
|
|
|
+ list.add("开局斗图,把它斗哭了");
|
|
|
+ list.add("敢和我来斗图");
|
|
|
+ list.add("一起斗图啊");
|
|
|
+ list.add("斗图你怕了吗");
|
|
|
+ list.add("斗图输了的结果");
|
|
|
+ list.add("斗图王者重出江湖");
|
|
|
+ list.add("斗图还是我最强");
|
|
|
+ list.add("打字就是表情包");
|
|
|
+ list.add("现在表白都靠斗图了");
|
|
|
+ list.add("斗图啊,王者在线等");
|
|
|
+ list.add("斗图吗,年轻人");
|
|
|
+ list.add("斗图能赚零花钱");
|
|
|
+ list.add("确认过眼神,是我斗不过的人");
|
|
|
+ list.add("年轻人爱的斗图软件");
|
|
|
+ list.add("教你一招,包情包用不完");
|
|
|
+ list.add("从此人生很有趣");
|
|
|
+ list.add("我不认输,我只斗图");
|
|
|
+ list.add("表情包不够用?");
|
|
|
+ list.add("斗图,你过来呀!");
|
|
|
+ list.add("斗图输了不开心");
|
|
|
+ list.add("斗图输了不要怕");
|
|
|
+ list.add("还怕表情包不够用?");
|
|
|
+ list.add("表情包太少的后果");
|
|
|
+ list.add("斗图秒赢!");
|
|
|
+ list.add("是谁还在盗图?");
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+}
|