在Java中将两个图像拼接在一起,没有空格

时间:2014-03-05 08:14:29

标签: java

我有两个jpg图像,我应该并排平铺两个图像,这两个图像看起来像是连接在一起。

任何人都可以建议我如何克服这个困难????

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.FileInputStream;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Color;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class GetImage0 {

public static void main(String[] args) throws FileNotFoundException, IOException{
try {
File f = new File("1.jpg");
 BufferedImage image = ImageIO.read(f);
height = image.getHeight();
int width = image.getWidth();
System.out.println("Height of image 1 : "+ height);
System.out.println("Width of image 1 : "+ width);
File f1 = new File("2.jpg");
BufferedImage image1 = ImageIO.read(f1);
int h = image1.getHeight();
int w = image1.getWidth();
System.out.println("Height of image 2 : "+ h);
System.out.println("Width of image 2 : "+ w);

    String filename = System.getProperty("user.home")+File.separator;
    BufferedImage img1 = ImageIO.read(new File("1.jpg"));       
        BufferedImage img2=ImageIO.read(new File("2.jpg"));
        BufferedImage joinedImg = joinBufferedImage(img1,img2);
    boolean success = ImageIO.write(joinedImg, "jpg", new File("joined.jpg"));
        System.out.println("saved success? "+success);

} catch (IOException ioe) {
ioe.printStackTrace();
}


File file = new File("C:/Documents and Settings/LFFR1/Desktop/image_0.jpg");

    FileInputStream fis = new FileInputStream(file);
    //create FileInputStream which obtains input bytes from a file in a file system
    //FileInputStream is meant for reading streams of raw bytes such as image data. For         //reading streams of characters, consider using FileReader.

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    try {
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            //Writes to this byte array output stream
            bos.write(buf, 0, readNum); 
            System.out.println("read " + readNum + " bytes,");
        }
    } catch (IOException ex) {
        Logger.getLogger(GetImage0.class.getName()).log(Level.SEVERE, null, ex);
    }

    byte[] bytes = bos.toByteArray();
}

public static BufferedImage joinBufferedImage(BufferedImage img1,BufferedImage img2) {

    //do some calculate first
    int offset  = 5;
    int wid = img1.getWidth()+img2.getWidth()+offset;
    int height = Math.max(img1.getHeight(),img2.getHeight())+offset;
    //create a new buffer and draw two image into the new image
    BufferedImage newImage = new BufferedImage(wid,height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = newImage.createGraphics();
    Color oldColor = g2.getColor();
    //fill background
    g2.setPaint(Color.WHITE);
    g2.fillRect(0, 0, wid, height);
    //draw image
    g2.setColor(oldColor);
    g2.drawImage(img1, null, 0, 0);
    g2.drawImage(img2, null, img1.getWidth()+offset, 0);
    g2.dispose();
    return newImage;
}

}

这是代码。

1 个答案:

答案 0 :(得分:0)

总结一下上述内容:

首先,代码g2.drawImage(img2, null, img1.getWidth()+offset, 0);实际上粘贴了第二个图像。

       v--- offset
######...======
#img1#...=img2=
######...======

您可能想要g2.drawImage(img2, null, img1.getWidth(), 0);。然后图像将没有间隙。

######======
#img1#=img2=
######======

然后,您可能希望裁剪或缩放图像,使它们具有相同的高度。请查看cropping / scaling算法。

对于图像的混合,有很多算法。例如,您可以将Alpha通道淡入应用于角落的透明并重叠图像。您可以执行相同的alpha并拉伸图像的连接边和然后重叠。您可以应用与拍摄全景图像时手机相似的内容。您需要确切地确定联合方需要查看和查找/构建算法以完成该操作。