Bootstrap

中国地图上描绘中国气象观测点位置

package com.example.demo.controller;

import com.example.demo.base.mybatis.util.Picture;
import com.example.demo.base.mybatis.util.PictureUtil;
import com.example.demo.domain.Name;
import com.example.demo.service.NameService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class HelloController {

    @Autowired
    private NameService nameService;

    /**
     * 中国地图上描绘中国气象观测点位置
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/draw")
    @ResponseBody
    public String draw() throws Exception {
        List<Name> names = nameService.findAll();//经纬度
        List<Picture> pictures = new ArrayList<>();
        for (Name name : names){
            Picture picture = new Picture();
            if(StringUtils.isNotBlank(name.getW()) && StringUtils.isNotBlank(name.getL())){
                int x = (int)(PictureUtil.getW(73.47,Double.parseDouble(name.getL())));
                int y = (int)(PictureUtil.getH(53.54,Double.parseDouble(name.getW())));
                picture.setX(x+125);
                picture.setY(y-60);
                pictures.add(picture);
            }
        }
        PictureUtil.overlapBackground(pictures,"C:/下载/picture.png","C:/下载/lunkuo3.jpg");
        return names.get(0).getW()+"+"+names.get(0).getL();
    }
}
package com.example.demo.base.mybatis.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

public class PictureUtil {

    private static final Logger log = LoggerFactory.getLogger(PictureUtil.class);

    /**
     * 遍历集合合成图片
     * @param pictures 画图需要的画笔
     * @param backPicturePath 背景图片位置
     * @param outFile 合成后图片位置
     * @throws Exception
     */
    public static final void overlapBackground(List<Picture> pictures,String outFile,String backPicturePath) throws Exception {
        BufferedImage big = ImageIO.read(new File(backPicturePath));
        Graphics2D g = big.createGraphics();
        for (Picture picture:pictures) {
            BufferedImage small = ImageIO.read(new File(outFile));
            g.setColor(Color.RED);
            g.setStroke(new BasicStroke(5));
            g.drawImage(small, picture.getX(), picture.getY(), small.getWidth(), small.getHeight(), null);
        }
        g.dispose();
        ImageIO.write(big, outFile.split("\\.")[1], new File(outFile));
    }

    public static double getH(double ydwd, double dwd){
        return (ydwd-dwd)*(116.2+(31.54-(ydwd-dwd))*0.9);
    }

    public static double getW(double ydjd, double djd){
        return (djd-ydjd)*86.5;
    }

}

lunkuo3picture运行结果

转载于:https://my.oschina.net/zhangshsURL/blog/1810618

;