美文网首页
java Graphics2D 将两张图片叠加

java Graphics2D 将两张图片叠加

作者: 飘雨0103 | 来源:发表于2019-03-25 14:58 被阅读0次

图片自己去找!

public class Picture {

private BufferedImageimage;

    private int CANVAS_W =1136; //底图宽度

    private int CANVAS_H =640; //底图宽度

    private int BOX_X =80; //信息框X坐标

    private int BOX_Y =145; /信息框Y坐标

//设置图片素材地址

    public static String$bg_path ="./static/picture/bg.png"; //背景图片路径

    public static String$player_default_box_path ="./static/picture/empty_box.png"; //玩家默认信息框图路径

    /**

* 获取战绩背景图  ---素材转化成图片

*/

    public void setMaterial(){

//获取底图

        BufferedImage bufferedImage = getImage($bg_path);

        BufferedImage bufferedImagePlayer = getImage($player_default_box_path);

        //创建画布

        image =new BufferedImage(CANVAS_W, CANVAS_H, BufferedImage.TYPE_INT_RGB);

        //开启画图

        Graphics2D graphics =image.createGraphics();

        graphics.drawImage(bufferedImage,0,0,bufferedImage.getWidth(),bufferedImage.getHeight(),null);

        graphics.drawImage(bufferedImagePlayer,BOX_X,BOX_Y,bufferedImagePlayer.getWidth(),bufferedImagePlayer.getHeight(),null);

    }

/**

* 获取图片资源

*/

    public BufferedImagegetImage(String path){

try {

//读入文件

            URL url = ClassLoader.getSystemResource(path);

            File file =new File(url.getPath());

            //构造Image对象

            BufferedImage image = ImageIO.read(file);

            return image;

        }catch (IOException e) {

throw new RuntimeException("获取图片资源异常",e);

        }

}

/**

* //生成图片文件

    * @param fileLocation

    */

    public void createImage(String fileLocation) {

BufferedOutputStream bos =null;

        if(image !=null){

try {

FileOutputStream fos =new FileOutputStream(fileLocation);

                bos =new BufferedOutputStream(fos);

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

                encoder.encode(image);

                bos.close();

            }catch (Exception e) {

e.printStackTrace();

            }finally{

if(bos!=null){//关闭输出流

                    try {

bos.close();

                    }catch (IOException e) {

e.printStackTrace();

                    }

}

}

}

}

/**

* 图片合成

*/

    public void graphicsGeneration(){

setMaterial();

        createImage("c:\\haha.jpg");

    }

public static void main(String[] args) {

Picture cg =new Picture();

        try {

cg.graphicsGeneration();

        }catch (Exception e) {

e.printStackTrace();

        }

}

}

相关文章

网友评论

      本文标题:java Graphics2D 将两张图片叠加

      本文链接:https://www.haomeiwen.com/subject/bwavvqtx.html