导航

Java 多张图片合成一张 drawImage

Posted on 2016-10-18 16:24  _eve  阅读(2515)  评论(0编辑  收藏  举报
 
package com.yunfengtech.solution.business;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class Twopic{
    public static void main(String[] args){
        File file = new File(".\\src\\image");
        System.out.println(file.getAbsolutePath());
        try
        {
            System.out.println(file.getCanonicalPath());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
        try {
            InputStream imagein1 = new FileInputStream("D:/twopic/n01.bmp");
            InputStream imagein2 = new FileInputStream("D:/twopic/n02.bmp");
            InputStream imagein3 = new FileInputStream("D:/twopic/n03.bmp");
            BufferedImage bg_image = new BufferedImage(192, 144, BufferedImage.TYPE_INT_BGR); 
            BufferedImage image1 = ImageIO.read(imagein1);
            BufferedImage image2 = ImageIO.read(imagein2);
            BufferedImage image3 = ImageIO.read(imagein3);
            Graphics g = bg_image.getGraphics();
            g.drawImage(image1, 0, 48, 48, 48,null);
            g.drawImage(image2, 48, 48, 48, 48,null);
            g.drawImage(image3, 96, 48, 48, 48,null);
            OutputStream outImage = new FileOutputStream("D:/twopic/playitemid_1_2.bmp");
            JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
            enc.encode(bg_image);
            imagein1.close();
            imagein2.close();
            imagein3.close();
            outImage.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
   }
}