Created
May 28, 2014 07:58
-
-
Save cuter44/08793191adb891f2a7e7 to your computer and use it in GitHub Desktop.
for 倩怡, 能纵向拼合图片的东东
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.cuter44.photomerge; | |
import java.io.*; | |
import java.awt.*; | |
import java.awt.image.*; | |
import javax.imageio.*; | |
import java.util.logging.*; | |
public class PhotoMerge | |
{ | |
public static void main(String[] args) | |
{ | |
int padding = 0; | |
Logger logger = Logger.getLogger(PhotoMerge.class.getName()); | |
//logger.addHandler(new ConsoleHandler()); | |
logger.setLevel(Level.CONFIG); | |
ImageObserver io = new ImageObserver(){ | |
@Override | |
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) | |
{ | |
return(infoflags == ImageObserver.ALLBITS); | |
} | |
}; | |
int x = 0; | |
int y = 0; | |
int w = 0; | |
int h = 0; | |
for (int i=0; i<args.length; i++) | |
{ | |
try | |
{ | |
logger.info("Fetch image: "+args[i]); | |
BufferedImage in = ImageIO.read( | |
new File(args[i]) | |
); | |
w = in.getWidth(io)>w ? in.getWidth(io) : w; | |
h += in.getHeight(io) + padding; | |
} | |
catch (Exception ex) | |
{ | |
logger.log(Level.SEVERE, "Error encounted, skipped.", ex); | |
} | |
} | |
x = padding; | |
y = padding; | |
h += padding; | |
w += padding*2; | |
BufferedImage out = new BufferedImage(w, h ,BufferedImage.TYPE_3BYTE_BGR); | |
Graphics g = out.getGraphics(); | |
logger.info("Prepare output:("+w+","+h+")"); | |
for (int i=0; i<args.length; i++) | |
{ | |
try | |
{ | |
logger.info("Write image: ("+x+","+y+") "+args[i]); | |
BufferedImage in = ImageIO.read( | |
new File(args[i]) | |
); | |
g.drawImage(in, x, y, io); | |
y += in.getHeight(io) + padding; | |
} | |
catch (Exception ex) | |
{ | |
logger.log(Level.SEVERE, "Error encounted, skipped.", ex); | |
} | |
} | |
logger.info("Write out."); | |
try | |
{ | |
ImageIO.write(out, "jpg", new File(args[args.length-1]+".out.jpg")); | |
} | |
catch (Exception ex) | |
{ | |
logger.log(Level.SEVERE, "Error encounted, skipped.", ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment