java png图片合并_Java——Image 图片合并

packagecom.tb.image;importjava.awt.Image;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjavax.imageio.ImageIO;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEncoder;/*** 图片拼接

* 把多张宽度一样的图片拼接成一张大图片

*@authorAdministrator

**/

public classCreateBigImage {public static voidmain(String[] args) {//System.out.println("123");//

// //设置图片宽度相同//changeImage("D:/imgs/", "1.jpg", "1.jpg", 300,200);//changeImage("D:/imgs/", "2.jpg", "2.jpg", 300,200);//changeImage("D:/imgs/", "3.jpg", "3.jpg", 300,200);// //获取宽度相同的图片//String img1 = "D:/imgs/1.jpg";//String img2 = "D:/imgs/2.jpg";//String img3 = "D:/imgs/3.jpg";//String[] imgs = new String[]{img1,img2,img3};// //图片拼接//merge(imgs,"jpg","D:/imgs/big.jpg");

String folderPath= "D:/imgs";

changeFolderImages(folderPath,600,400);

mergeFolderImgs(folderPath,"jpg","D:/imgs/merge.jpg");

}/*** 合并图片

*@paramfolderPath 图片所在文件夹的绝对路径

*@paramimgType 合并后的图片类型(jpg、png...)

*@paramoutAbsolutePath(输出合并后文件的绝对路径)

*@return

*/

public staticString mergeFolderImgs(String folderPath,String imgType,String outAbsolutePath){

File folder= newFile(folderPath);

File[] imgList=folder.listFiles();

String[] imgPaths= newString[imgList.length];for (int i = 0; i < imgList.length; i++) {//System.out.println("文件个数:"+imgList[i].length());

imgPaths[i] =imgList[i].getAbsolutePath();

System.out.println("第"+i+"张图片途径:"+imgPaths[i]);

}

merge(imgPaths,imgType,outAbsolutePath);

System.out.println("---------------------");

File newImg= newFile(outAbsolutePath);

System.out.println(newImg.getName());returnnewImg.getName();

}/*** 设置图片大小(单张图片)

*@parampath 路径

*@paramoldimg 旧图片名称

*@paramnewimg 新图片名称

*@paramnewWidth 新图片宽度

*@paramnewHeight 新图片高度*/

public static void changeImage(String path, String oldimg, String newimg, int newWidth,intnewHeight) {try{

File file= new File(path +oldimg);

Image img=ImageIO.read(file);//构造Image对象//int wideth = img.getWidth(null);//得到源图宽//int height = img.getHeight(null);//得到源图长

BufferedImage tag = newBufferedImage(newWidth, newHeight,

BufferedImage.TYPE_INT_RGB);

tag.getGraphics()

.drawImage(img,0, 0, newWidth, newHeight, null); //绘制后的图

FileOutputStream out = new FileOutputStream(path +newimg);

JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);//近JPEG编码

out.close();

}catch(IOException e) {

System.out.println("处理文件出现异常");

e.printStackTrace();

}

}/*** 设置图片大小(批量处理整个文件夹中的图片)

*@paramfolderPath 文件夹路径

*@paramnewWidth 新图片宽度

*@paramnewHeight 新图片高度*/

public static void changeFolderImages(String folderPath, int newWidth,intnewHeight) {try{

File folder= new File(folderPath);//得到文件夹

File[] imgList = folder.listFiles();//得到文件夹中的所有图片

Image image = null;//定义一张图片

BufferedImage bfImg= newBufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

FileOutputStream outputStream= null;

JPEGImageEncoder encoder= null;for (int i = 0; i < imgList.length; i++) {

image= ImageIO.read(imgList[i]);//将得到的图片放入新定义的图片中

bfImg.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);//绘制后的图

outputStream = newFileOutputStream(imgList[i]);

encoder=JPEGCodec.createJPEGEncoder(outputStream);

encoder.encode(bfImg);

}

outputStream.close();

}catch(IOException e) {

System.out.println("处理文件出现异常");

e.printStackTrace();

}

}/*** Java拼接多张图片

*

*@parampics:图片源文件 (必须要宽度一样),如:

* String img1 = "D:/imgs/3.jpg";

* String img2 = "D:/imgs/3.jpg";

* String img3 = "D:/imgs/big.jpg";

* String[] pics = new String[]{img1,img2,img3};

*@paramtype :图片输出类型(jpg,png,jpeg...)

*@paramdst_pic :图片输出绝对路径,如 String dst_pic="D:/imgs/big2.jpg";

*@return

*/

public static booleanmerge(String[] pics, String type, String dst_pic) {int len = pics.length; //图片文件个数

if (len < 1) {

System.out.println("pics len < 1");return false;

}

File[] src= newFile[len];

BufferedImage[] images= newBufferedImage[len];int[][] ImageArrays = new int[len][];for (int i = 0; i < len; i++) {try{

src[i]= newFile(pics[i]);

images[i]=ImageIO.read(src[i]);

}catch(Exception e) {

e.printStackTrace();return false;

}int width =images[i].getWidth();int height =images[i].getHeight();

ImageArrays[i]= new int[width * height];//从图片中读取RGB

ImageArrays[i] = images[i].getRGB(0, 0, width, height,

ImageArrays[i],0, width);

}int dst_height = 0;int dst_width = images[0].getWidth();for (int i = 0; i < images.length; i++) {

dst_width= dst_width > images[i].getWidth() ?dst_width

: images[i].getWidth();

dst_height+=images[i].getHeight();

}

System.out.println(dst_width);

System.out.println(dst_height);if (dst_height < 1) {

System.out.println("dst_height < 1");return false;

}//生成新图片

try{//dst_width = images[0].getWidth();

BufferedImage ImageNew = newBufferedImage(dst_width, dst_height,

BufferedImage.TYPE_INT_RGB);int height_i = 0;for (int i = 0; i < images.length; i++) {

ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(),

ImageArrays[i],0, dst_width);

height_i+=images[i].getHeight();

}

File outFile= newFile(dst_pic);

ImageIO.write(ImageNew, type, outFile);//写图片

} catch(Exception e) {

e.printStackTrace();return false;

}return true;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值