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> types = new HashMap<>();

    static {
        {
            Map 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 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

你可能感兴趣的:(java 合成图片)