import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
/**
* 图片工具类
* @author cxp
*
*/
public final class ImgUtil {
private ImgUtil() {
}
/**
* 合并图片
*
* @param path1
* 图片路径1
* @param path2
* 图片路径2
* @param path
* 新图片路径
* @throws IOException
*/
public static void mergeImage(String path1, String path2, String path)
throws IOException {
BufferedImage image1 = ImageIO.read(new File(path1));
BufferedImage image2 = ImageIO.read(new File(path2));
BufferedImage combined = new BufferedImage(image1.getWidth(),
image1.getHeight(), BufferedImage.TYPE_INT_RGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, 0, 0, null);
// Save as new image
ImageIO.write(combined, "png", new File(path));
}
/**
* 小图片合并为大图片
* @param path 大图片的输出路径
* @param imgs 小图片地址
* @param colCount
* @param samllWidth
* @param smallHeigh
* @param picName
* @return
*/
public static String createBigPng(String path, List<String> imgs,
int colCount, int samllWidth, int smallHeigh, String picName) {
try {
File bigImgTxt = new File(path + File.separator + picName + ".txt");
if (!bigImgTxt.exists()) {
bigImgTxt.createNewFile();
}
FileWriter fileWriter = new FileWriter(bigImgTxt);
int imgCount = imgs.size();
int rowCount = imgCount / colCount
+ (imgCount % colCount == 0 ? 0 : 1); // 小图行数
int bigWidth = samllWidth * colCount;
int bigHeight = smallHeigh * rowCount;
// 按照大图片宽高绘制一个背景图片
BufferedImage bufImage = new BufferedImage(bigWidth, bigHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImage.createGraphics();
bufImage = g.getDeviceConfiguration().createCompatibleImage(
bigWidth, bigHeight, Transparency.TRANSLUCENT);
g.dispose();
g = bufImage.createGraphics();
int x = 0;
int y = 0; // 纵坐标
for (int i = 0; i < imgCount; i++) {
String imgName = imgs.get(i);
if (i % colCount == 0) {
x = 0;
if (i > 1) {
y += smallHeigh;
}
}
BufferedImage input = ImageIO.read(new File(imgName));
Image smallImg = input.getScaledInstance(samllWidth,
smallHeigh, Image.SCALE_SMOOTH);
g.drawImage(smallImg, x, y, null);
fileWriter.write(imgName.substring(
imgName.lastIndexOf("/") + 1, imgName.lastIndexOf("."))
+ ": ["
+ x
+ ","
+ y
+ ","
+ (x + samllWidth)
+ ","
+ (y + smallHeigh) + "]\r\n");
x += samllWidth;
}
g.dispose();
String bigPicPath = path + "/" + picName + ".png";
ImageIO.write(bufImage, "png", new File(bigPicPath));
if (fileWriter != null) {
fileWriter.flush();
fileWriter.close();
}
return bigPicPath;
} catch (Exception e) {
System.out.println("createBigJPG Failed!");
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String path = "C:\\Users\\tt\\Desktop\\tupian";
File file = new File(path);
String[] imgsFiles = file.list();
List<String> imgs = Arrays.asList(imgsFiles);
createBigPng(path, imgs, 10, 48, 48, "tt.png");
}
}
图片工具类——两张图片合并,小图合并成大图
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- TKImageView-可定制样式的图片裁剪工具类 最近接连做了几个项目,都用到了图片裁剪功能,每个项目对裁剪功能...
- 干货要点: 1、叙述、描写和说明是三种记“实”的能力。 2、议论和抒情是两种写“虚”的能力。 3、根据表达的中心,...