Java實(shí)現(xiàn)圖片拼接
本文實(shí)例為大家分享了Java實(shí)現(xiàn)圖片拼接的具體代碼,供大家參考,具體內(nèi)容如下
/**
* 拼接圖片(注:圖片需長寬一致)
* @param files img1 ,img2
* @param type 1:橫向拼接 2:縱向拼接
* @param targetFile 合成新的圖片地址
*/
public static void mergeImage(String[] files, int type, String targetFile) {
int len = files.length;
if (len < 1) {
throw new RuntimeException("圖片數(shù)量小于1");
}
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
src[i] = new File(files[i]);
images[i] = ImageIO.read(src[i]);
} catch (Exception e) {
throw new RuntimeException(e);
}
int width = images[i].getWidth();
int height = images[i].getHeight();
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}
int newHeight = 0;
int newWidth = 0;
for (int i = 0; i < images.length; i++) {
// 橫向
if (type == 1) {
newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
newWidth += images[i].getWidth();
} else if (type == 2) {// 縱向
newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
newHeight += images[i].getHeight();
}
}
if (type == 1 && newWidth < 1) {
return;
}
if (type == 2 && newHeight < 1) {
return;
}
// 生成新圖片
try {
BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
int width_i = 0;
for (int i = 0; i < images.length; i++) {
if (type == 1) {
ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
images[i].getWidth());
width_i += images[i].getWidth();
} else if (type == 2) {
ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
height_i += images[i].getHeight();
}
}
//輸出想要的圖片
ImageIO.write(ImageNew, targetFile.split("\\.")[1], new File(targetFile));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)將導(dǎo)出帶格式的Excel數(shù)據(jù)到Word表格
在Word中制作報(bào)表時(shí),我們經(jīng)常需要將Excel中的數(shù)據(jù)復(fù)制粘貼到Word中,這樣則可以直接在Word文檔中查看數(shù)據(jù)而無需打開另一個(gè)Excel文件。本文將通過Java應(yīng)用程序詳細(xì)介紹如何把帶格式的Excel數(shù)據(jù)導(dǎo)入Word表格。希望這篇文章能對(duì)大家有所幫助2022-11-11
Java實(shí)現(xiàn)FutureTask的示例詳解
在并發(fā)編程當(dāng)中我們最常見的需求就是啟動(dòng)一個(gè)線程執(zhí)行一個(gè)函數(shù)去完成我們的需求,而在這種需求當(dāng)中,我們需要函數(shù)有返回值。Java給我們提供了這種機(jī)制,去實(shí)現(xiàn)這一個(gè)效果:FutureTask。本文為大家準(zhǔn)備了Java實(shí)現(xiàn)FutureTask的示例代碼,需要的可以參考一下2022-08-08
Spring?Boot項(xiàng)目部署命令java?-jar的各種參數(shù)及作用詳解
這篇文章主要介紹了Spring?Boot項(xiàng)目部署命令java?-jar的各種參數(shù)及作用的相關(guān)資料,包括設(shè)置內(nèi)存大小、垃圾回收器、線程棧大小、系統(tǒng)屬性等,還介紹了SpringBoot專用參數(shù),如修改端口、指定配置文件等,需要的朋友可以參考下2025-04-04
@SpringBootTest 注解報(bào)紅問題及解決
這篇文章主要介紹了@SpringBootTest 注解報(bào)紅問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
使用Java生成JWT(JSON Web Token)的方法示例
在現(xiàn)代應(yīng)用程序中,身份驗(yàn)證和授權(quán)是至關(guān)重要的,JWT是一種簡單而強(qiáng)大的身份驗(yàn)證和授權(quán)機(jī)制,可以在Web應(yīng)用程序中安全地傳輸用戶信息,本文主要介紹了使用Java生成JWT的方法示例,感興趣的可以了解一下2024-03-03
詳解在springboot中使用Mybatis Generator的兩種方式
這篇文章主要介紹了詳解在springboot中使用Mybatis Generator的兩種方式,本文將介紹到在springboot的項(xiàng)目中如何去配置和使用MBG以及MBG生成代碼的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11

