Skip to content

Instantly share code, notes, and snippets.

@dingyaguang117
Created October 22, 2013 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingyaguang117/7097992 to your computer and use it in GitHub Desktop.
Save dingyaguang117/7097992 to your computer and use it in GitHub Desktop.
Java图片裁剪合并
/*
* 2012/12/11
* ding
* */
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageUtil
{
public static BufferedImage ResizeToWidth(BufferedImage image ,int width)
{
if (image.getWidth() == width) return image;
int height = image.getHeight() * width / image.getWidth();
BufferedImage ret = new BufferedImage(width, height, image.getType());
Graphics g = ret.getGraphics();
g.drawImage(image,0,0,width,height,null);
g.dispose();
return ret;
}
public static byte[] mergeImage(byte[] image1,byte[] image2,Integer width,String format) throws IOException
{
BufferedImage bi1 = ImageIO.read(new ByteArrayInputStream(image1));
BufferedImage bi2 = ImageIO.read(new ByteArrayInputStream(image2));
int des_width;
if(width == null)
{
des_width = bi1.getWidth() < bi2.getWidth() ? bi1.getWidth():bi2.getWidth();
}else
{
des_width = width;
}
//resize to same width
bi1 = ResizeToWidth(bi1, des_width);
bi2 = ResizeToWidth(bi2, des_width);
//merge
int [] rgb1 = new int[bi1.getWidth() * bi1.getHeight()];
int [] rgb2 = new int[bi2.getWidth() * bi2.getHeight()];
rgb1 = bi1.getRGB(0, 0, bi1.getWidth(), bi1.getHeight(), rgb1, 0, des_width);
rgb2 = bi2.getRGB(0, 0, bi2.getWidth(), bi2.getHeight(), rgb2, 0, des_width);
BufferedImage ret = new BufferedImage(des_width, bi1.getHeight() + bi2.getHeight(),bi1.getType());
ret.setRGB(0, 0, bi1.getWidth(), bi1.getHeight(), rgb1, 0, bi1.getWidth());
ret.setRGB(0, bi1.getHeight(), bi2.getWidth(), bi2.getHeight(), rgb2, 0, bi2.getWidth());
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (format == null)
{
format = "JPG";
}
ImageIO.write(ret, format, out);
return out.toByteArray();
}
public static byte[] readFile2Bytes(String filename) throws IOException
{
File file = new File(filename);
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
fi.close();
return buffer;
}
public static void createFile(String path, byte[] content) throws IOException
{
FileOutputStream fos = new FileOutputStream(path);
fos.write(content);
fos.close();
}
public static void main(String[] args) throws FileNotFoundException, IOException
{
createFile("out.png",mergeImage(readFile2Bytes("E:/HuohuaWorkspace/java/anteater-kernel/src/main/java/com/huohua/anteater/util/180.png"),
readFile2Bytes("E:/HuohuaWorkspace/java/anteater-kernel/src/main/java/com/huohua/anteater/util/180.png"),null,null));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment