java 合成图片

package com.yiwugo.external.service.controller;

import com.multiplyzero.mz.core.page.Pager;
import com.multiplyzero.mz.core.rest.Result;
import com.yiwugo.external.api.enums.Language;
import com.yiwugo.external.api.service.ImgSearchService;
import com.yiwugo.external.service.dubbo.DubboService;
import com.yiwugou.product.api.bean.product.ProductDetailBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.Buffer;
import java.util.HashMap;
import java.util.Map;


public class ImgSearchController {



    private static Map<String, Map<String, Object>> types = new HashMap<>();

    static {
        {
            Map<String, Object> map = new HashMap<>();
            types.put("4x6", map);
            map.put("cw", 1024);
            map.put("ch", 720);
            map.put("iw", 170);
            map.put("ih", 180);
            map.put("row", 6);
            map.put("max", 24);
            map.put("spacing", 6);
        }
        {
            Map<String, Object> map = new HashMap<>();
            types.put("4x4", map);
            map.put("cw", 1024);
            map.put("ch", 1024);
            map.put("iw", 256);
            map.put("ih", 256);
            map.put("row", 4);
            map.put("max", 16);
            map.put("spacing", 10);
        }
    }


    private static String size = "4x6";
    private static String[] images = new String[]{
            "https://img1.yiwugou.com/i004/2022/03/25/97/2462dc08b5f3fa4eccb999e068e8ef60.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/68/b41b4a541877e38a68e0e943280e0869.jpg",
            "https://img1.yiwugou.com/i004/2022/03/25/14/10b79a8ac907bbf5a2825f3d4cd737a5.jpg",
            "https://img1.yiwugou.com/i004/2020/03/06/28/d27f218b1ba99b7fc477719bcace4a2e.jpg",
            "https://img1.yiwugou.com/i004/2022/03/25/97/4625f3c7282f9ca855962b1977a0b038.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/41/0553a7be6d08a40b09d07f3b3e79df10.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/47/7c6687042592e892c3abf684d1afe942.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/56/379ad016f0fe6c204f07146700e752a4.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/00/ec5ec4c58ab7a1f3878b395e28d1fc52.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/69/215a035deeb1ce9f7fd04c60af1599e3.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/42/564794b3a30df0cbd978c49c3b05cd7e.jpg",
            "https://img1.yiwugou.com/i004/2022/03/25/54/g224755e31beb20ba25f90789a3d3d5c.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/47/0c600ef35e5b4e2707ee4f0b7e0defeb.jpg",
            "https://img1.yiwugou.com/i004/2022/03/24/98/40a74e04b620e758dd2496235dd6c977.jpg",
            "https://img1.yiwugou.com/i004/2022/03/25/38/Ab7bc52a63bb18aff7e43b72e839d2a5.png",
            "https://img1.yiwugou.com/i004/2021/03/18/61/28e732b7912711914ba38b6d7243db31.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/72/dbc0413fc1340670bceca724048745cc.jpg",
            "https://img1.yiwugou.com/i004/2022/03/24/34/2bb6512b06e7192b350394c0051448ae.jpg",
            "https://img1.yiwugou.com/i004/2016/03/04/47/ce24197d8c1ebd6f6c9f40ffa77f47b0.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/61/28e732b7912711914ba38b6d7243db31.jpg",
            "https://img1.yiwugou.com/i004/2021/03/18/72/dbc0413fc1340670bceca724048745cc.jpg",
            "https://img1.yiwugou.com/i004/2022/03/24/34/2bb6512b06e7192b350394c0051448ae.jpg",
            "https://img1.yiwugou.com/i004/2016/03/04/47/ce24197d8c1ebd6f6c9f40ffa77f47b0.jpg",
            "https://img1.yiwugou.com/i004/2022/03/24/34/2bb6512b06e7192b350394c0051448ae.jpg",
            "https://img1.yiwugou.com/i004/2016/03/04/47/ce24197d8c1ebd6f6c9f40ffa77f47b0.jpg"
    };

    private static Double[] prices = new Double[]{1.24, 2.25, 123.1, 12.4, 15568.1, 354.1, 1.1, 2.2, 3.33, 4.4, 5.5, 6.6, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 12.4, 1268.4, 15.22, 1268.4, 15.22, 1268.4, 15.22};

    public static void main(String[] args) throws Exception {
        long time = System.currentTimeMillis();
        System.out.println();
        int canvasWidth = (int) types.get(size).get("cw");
        int canvasHeight = (int) types.get(size).get("ch");
        int imgWidth = (int) types.get(size).get("iw");
        int imgHeight = (int) types.get(size).get("ih");
        int oneRowQuantity = (int) types.get(size).get("row");
        int max = (int) types.get(size).get("max");
        int spacing = (int) types.get(size).get("spacing");
        BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = bi.createGraphics();
        graphics.setColor(Color.WHITE);//设置笔刷白色
        graphics.fillRect(0, 0, canvasWidth, canvasHeight);//填充整个屏幕

        for (int i = 0; i < images.length; i++) {
            int dx = (i % oneRowQuantity) * imgWidth;
            int dy = new Double(Math.ceil(i / oneRowQuantity)).intValue() * imgHeight;
            BufferedImage one = drawOneImage(ImageIO.read(new URL(images[i])), 20, 2, spacing / 2, imgWidth, imgHeight, i);
            graphics.drawImage(one, dx, dy, imgWidth, imgHeight, null);
        }

        graphics.dispose();


        ImageIO.write(bi, "png", new File("D:\\Desktop\\abc.png"));

        System.out.println(System.currentTimeMillis() - time);
    }


    public static BufferedImage drawOneImage(BufferedImage srcImage, int radius, int border, int padding, int width, int height, int i) throws IOException {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();
        gs.setComposite(AlphaComposite.Src);
        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setColor(Color.WHITE);
        gs.fill(new RoundRectangle2D.Float(0, 0, width, height, radius, radius));
        gs.setComposite(AlphaComposite.SrcAtop);
        gs.drawImage(clip(srcImage, width, height, radius, border, padding, i), padding, padding, null);
        if (border != 0) {
            gs.setColor(Color.GRAY);
            gs.setStroke(new BasicStroke(border));
            gs.drawRoundRect(padding, padding, width - 2 * padding, height - 2 * padding, radius, radius);
        }
        gs.dispose();
        return image;
    }

    public static BufferedImage clip(BufferedImage srcImage, int width, int height, int radius, int border, int padding, int i) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();

        // 截圆角
        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setClip(new RoundRectangle2D.Double(0, 0, width - padding * 2, height - (padding) * 2, radius, radius));
        gs.drawImage(srcImage, 0, 0, width - padding * 2, height - (padding) * 2, null);

        // 画序号与价格所在的图层 透明度设置为0.5f
        gs.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
        gs.setColor(Color.gray);
        gs.fillRect(0, height - 60, width, height);

        // 画序号 (L1 L2)
        gs.setColor(Color.white);
        gs.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
        gs.setFont(new Font(null, Font.BOLD, 20));
        gs.drawString("L" + (i + 1), 10, height - 30);

        // 画价格
        String price = "¥" + prices[i];
        int s = gs.getFontMetrics().stringWidth(price);
        gs.drawString(price, width - 20 - s, height - 30);


        gs.dispose();
        return image;
    }

}


结果

abc.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342