Java實現(xiàn)圖片拼接
更新時間:2018年08月07日 08:41:06 作者:Abel-Luan
這篇文章主要為大家詳細介紹了Java實現(xiàn)圖片拼接的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java實現(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實現(xiàn)將導(dǎo)出帶格式的Excel數(shù)據(jù)到Word表格
在Word中制作報表時,我們經(jīng)常需要將Excel中的數(shù)據(jù)復(fù)制粘貼到Word中,這樣則可以直接在Word文檔中查看數(shù)據(jù)而無需打開另一個Excel文件。本文將通過Java應(yīng)用程序詳細介紹如何把帶格式的Excel數(shù)據(jù)導(dǎo)入Word表格。希望這篇文章能對大家有所幫助2022-11-11
Spring?Boot項目部署命令java?-jar的各種參數(shù)及作用詳解
這篇文章主要介紹了Spring?Boot項目部署命令java?-jar的各種參數(shù)及作用的相關(guān)資料,包括設(shè)置內(nèi)存大小、垃圾回收器、線程棧大小、系統(tǒng)屬性等,還介紹了SpringBoot專用參數(shù),如修改端口、指定配置文件等,需要的朋友可以參考下2025-04-04
使用Java生成JWT(JSON Web Token)的方法示例
在現(xiàn)代應(yīng)用程序中,身份驗證和授權(quán)是至關(guān)重要的,JWT是一種簡單而強大的身份驗證和授權(quán)機制,可以在Web應(yīng)用程序中安全地傳輸用戶信息,本文主要介紹了使用Java生成JWT的方法示例,感興趣的可以了解一下2024-03-03
詳解在springboot中使用Mybatis Generator的兩種方式
這篇文章主要介紹了詳解在springboot中使用Mybatis Generator的兩種方式,本文將介紹到在springboot的項目中如何去配置和使用MBG以及MBG生成代碼的兩種方式,非常具有實用價值,需要的朋友可以參考下2018-11-11

