为了给YiYi节省时间,写了个能自动拼图贴水印的机器人,很多bug,能用就行。...

package boneyiyi.tool.image;


import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;


import javax.imageio.ImageIO;


/**
* 为了给YiYi节省时间,写了个能自动拼图贴水印的机器人,很多bug,能用就行。
*
* lazybone 2012.04.05
*/
public class Utils {


/************** 需要配置 **********************/
// 是否只保存最终的作品到作品文件夹
static boolean isSaveFinalWorks = true;


// 每个图片上下填充的高度,
static final int modify = 50;


// 是否添加水印
static boolean isAddLogo = true;


// 水印搜索的像素数
static final int logoModify = 80;


/************** 无需配置 *********************/
// 工作室文件夹 "d:/boneyiyi"
static final String workspace = "d:/boneyiyi";


// 水印文件夹
static final String logoAPNG = workspace + "/素材/logo_425_198.png";
static final String logoAPNGTest = workspace + "/素材/logonew.png";


// 工作室文件夹 "d:/boneyiyi"
static final String workspace_todo = workspace + "/车间";


// 文件名后缀,用于搜索
static final String fileName_750wok = "_750wok.jpg";


// 宝贝内页顶部图片
static final String titleFileName = workspace + "/素材/title.jpg";


// 文件名后缀,用于搜索
static final String fileName_ok = "_ok.jpg";


// 作品文件夹
static final String workspace_done = workspace + "/完成作品";


/**
* 开工
*
* @param args
*/
public static void main(String args[]) {
new Utils().startWork();
}


public void startWork() {
// 初始化logo
initLogo();


File[] files = new File(workspace_todo).listFiles();


for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
System.out.println(files[i].getPath() + " is doing...");
new Utils().createThePic(files[i]);
System.out.println(files[i].getPath() + " is done.");
System.out.println();
}
}


System.out.println("well done!");
}


public void createThePic(File mainFile) {
Graphics2D panel;
Graphics2D panel750;
int countLen = 0;
int width = 0;


int curX = 0;
int curY = 0;


ArrayList<BufferedImage> imageList = new ArrayList<BufferedImage>();
File[] files = mainFile.listFiles();


for (int i = 0; i < files.length; i++) {
if (files[i].getPath().contains(fileName_750wok)
|| files[i].getPath().contains(fileName_ok)) {
continue;
}
BufferedImage bImage = loadImageLocal(files[i].getPath());
countLen += bImage.getHeight();
imageList.add(bImage);
width = (width > bImage.getWidth() ? width : bImage.getWidth());
}


// 如果图都是很小的,小于750,就默认width=750;
width = (width > 750 ? width : 750);


BufferedImage panelImage = new BufferedImage(width, countLen,
BufferedImage.TYPE_INT_RGB);


panel = panelImage.createGraphics();
panel.setBackground(Color.WHITE);
panel.setColor(Color.WHITE);
panel.fillRect(0, 0, width, countLen);


// 添加标题
BufferedImage title = loadImageLocal(titleFileName);
curX = (width - title.getWidth()) / 2;
panel.drawImage(title, curX, curY, title.getWidth(), title.getHeight(),
null);
curY += title.getHeight();


for (int i = 0; i < imageList.size(); i++) {
panel.setColor(Color.WHITE);
BufferedImage img = imageList.get(i);


curX = (width - img.getWidth()) / 2;
// 去掉虚边
panel.drawImage(img, curX, curY, img.getWidth(), img.getHeight(),
null);
panel.fillRect(curX, curY, img.getWidth(), modify);
panel.fillRect(curX, curY + img.getHeight() - modify,
img.getWidth(), img.getHeight());


// 加水印
if (isAddLogo == true) {
drawLogo(panel, (width - 750) / 2, curY + modify, 750,
img.getHeight() - modify);
}


curY += img.getHeight();
}


// 文件夹的名字,用在区分不同文件夹的作品
String fnFirst = mainFile.getPath().replace(mainFile.getParent(), "");
if (isSaveFinalWorks == false) {
String fnAll = mainFile.getPath() + "/" + fnFirst + "_" + width
+ "_" + countLen + fileName_ok;// "_ok.jpg"
writeImageLocal(fnAll, panelImage);
System.out.println(fnAll + " ok");
}
// 切割750
BufferedImage panelImage750 = new BufferedImage(750, countLen,
BufferedImage.TYPE_INT_RGB);
panel750 = panelImage750.createGraphics();
panel750.setBackground(Color.WHITE);
panel750.setColor(Color.WHITE);
panel750.fillRect(0, 0, 750, countLen);
panel750.drawImage(panelImage, 0, 0, 750, countLen, (width - 750) / 2,
0, 750 + (width - 750) / 2, countLen, null);
// 写图片750
if (isSaveFinalWorks == false) {
String fn750 = mainFile.getPath() + "/" + fnFirst + "_750_"
+ countLen + fileName_750wok;// "_750wok.jpg";
writeImageLocal(fn750, panelImage750);
System.out.println(fn750 + " ok");
}
// 写图片到外层文件夹
File done = new File(workspace_done);
if (!done.exists()) {
done.mkdir();
}
String fn750OutSide = workspace_done + "/" + fnFirst + "_750_"
+ countLen + fileName_750wok;// "_750wok.jpg";
writeImageLocal(fn750OutSide, panelImage750);
// 关闭
panel.dispose();
panel750.dispose();
}


/**
* 导入本地图片到缓冲区
*/
public static BufferedImage loadImageLocal(String imgName) {
try {
return ImageIO.read(new File(imgName));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}


/**
* 生成新图片到本地
*/
public static void writeImageLocal(String newImage, BufferedImage img) {
if (newImage != null && img != null) {
try {
File outputfile = new File(newImage);
ImageIO.write(img, "jpg", outputfile);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}


/**
* 绘制logo
*/
public void drawLogo(Graphics2D panel, int x, int y, int w, int h) {
initLogo();


int logoW = logoBufferedImage.getWidth();
int logoH = logoBufferedImage.getHeight();
// logoModify 绘制logo时,往里绘制
int startX = x + logoModify
+ new Random().nextInt(w - logoW - logoModify);
int startY = y + logoModify
+ new Random().nextInt(h - logoH - logoModify);


panel.drawImage(logoBufferedImage, startX, startY, logoW, logoH, null);
}


/**
* 初始化logo
*/
public void initLogo() {
if (logoBufferedImage != null) {
return;
}
BufferedImage img;
try {
// 读取透明png
img = ImageIO.read(new File(logoAPNG));
// 生成一个存放png数据的数组
int rgb[] = new int[img.getWidth() * img.getHeight()];
// 读取png信息
img.getRGB(0, 0, img.getWidth(), img.getHeight(), rgb, 0,
img.getWidth());
// 一个新的png
logoBufferedImage = new BufferedImage(img.getWidth(),
img.getHeight(), BufferedImage.TYPE_INT_ARGB);
// 设置数据
logoBufferedImage.setRGB(0, 0, img.getWidth(), img.getHeight(),
rgb, 0, img.getWidth());
System.out.println("Logo is init ok.");


} catch (IOException e) {
e.printStackTrace();
}
}


public static BufferedImage logoBufferedImage;


/**
* 读取透明png水印
*/
public static int[] readLogo() {
BufferedImage img;
try {
// 读取透明png
img = ImageIO.read(new File(logoAPNG));
// 生成一个存放png数据的数组
int rgb[] = new int[img.getWidth() * img.getHeight()];
// 读取png信息
img.getRGB(0, 0, img.getWidth(), img.getHeight(), rgb, 0,
img.getWidth());
// 一个新的png
BufferedImage imgNew = new BufferedImage(img.getWidth(),
img.getHeight(), BufferedImage.TYPE_INT_ARGB);
// 设置数据
imgNew.setRGB(0, 0, img.getWidth(), img.getHeight(), rgb, 0,
img.getWidth());


return rgb;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}


/**
* 读取透明png水印
*/
public static void readAPng() {
BufferedImage img;
try {
// 读取透明png
img = ImageIO.read(new File(logoAPNG));
// 生成一个存放png数据的数组
int rgb[] = new int[img.getWidth() * img.getHeight()];
// 读取png信息
img.getRGB(0, 0, img.getWidth(), img.getHeight(), rgb, 0,
img.getWidth());
// 一个新的png
BufferedImage imgNew = new BufferedImage(img.getWidth(),
img.getHeight(), BufferedImage.TYPE_INT_ARGB);
// 设置数据
imgNew.setRGB(0, 0, img.getWidth(), img.getHeight(), rgb, 0,
img.getWidth());
// 保存到新文件half.png里面
ImageIO.write(imgNew, "PNG", new File(logoAPNGTest));
} catch (IOException e) {
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值