/**
* 将一组图片一次性附加合并到底图上
* @param negativeImagePath 源图像(底图)路径
* @param additionImageList 附加图像信息列表
* @param imageFormat 图像写入格式
* @param toPath 图像写入路径
* @throws IOException
*/
public void mergeImageList(String negativeImagePath,List additionImageList,String imageFormat, String toPath) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
BufferedImage image=ImageIO.read(is);
//Graphics g=image.getGraphics();
Graphics2D g = image.createGraphics();;
BufferedImage image2 = null;
if(additionImageList != null){
for(int i=0;i<additionImageList.size();i++){
//解析附加图片信息:x坐标、 y坐标、 additionImagePath附加图片路径
//图片信息存储在一个数组中
String[] additionImageInfo = (String[]) additionImageList.get(i);
int x = Integer.parseInt(additionImageInfo[0]);
int y = Integer.parseInt(additionImageInfo[1]);
String additionImagePath = additionImageInfo[2];
//读取文件输入流,并合并图片
is2 = new FileInputStream(additionImagePath);
//System.out.println(x+" : "+y+" : "+additionImagePath);
image2 = ImageIO.read(is2);
g.drawImage(image2,x,y,null);
}
}
os = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, os);//写图片
//JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
//enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的左上角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageTopleftcorner(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的右上角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageToprightcorner(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的左下角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageLeftbottom(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的左下角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageRightbottom(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的正中央
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageCenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的上边中央
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageTopcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的下边中央
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageBottomcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的左边中央
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageLeftcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的右边中央
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageRightcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 图片灰化操作
* @param srcImage 读取图片路径
* @param toPath 写入灰化后的图片路径
* @param imageFormat 图片写入格式
*/
public void grayImage(String srcImage,String toPath,String imageFormat){
try{
BufferedImage src = ImageIO.read(new File(srcImage));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, imageFormat, new File(toPath));
}catch(Exception e){
e.printStackTrace();
}
}