通过原图,在随机位置生成小方格,并根据小方格生成底图,底图由小方格的灰度化之后,绘制到原图上。
private Map createImg(File file, String filename) throws IOException {
BufferedImage sourceBuff = ImageIO.read(file);
int width = sourceBuff.getWidth();
int height = sourceBuff.getHeight(); //生成随机x,y
Random random = new Random(); //X轴距离右端tailoring_w 以上) Y轴距离底部tailoring_y以上
this.location_x = random.nextInt(width - tailoring_w * 2) + tailoring_w; this.location_y = random.nextInt(height - tailoring_h); //裁剪小图
BufferedImage sourceSmall = cutImg(file, location_x, location_y, tailoring_w, tailoring_h); //创建shape区域
List shapes = createSmallShape();
Shape area = shapes.get(0);
Shape bigarea = shapes.get(1); //创建图层用于处理小图的阴影
BufferedImage bfm1 = new BufferedImage(tailoring_w, tailoring_h, BufferedImage.TYPE_INT_ARGB); //创建图层用于处理大图的凹槽
BufferedImage bfm2 = new BufferedImage(tailoring_w, tailoring_h, BufferedImage.TYPE_INT_ARGB); for (int i = 0; i < tailoring_w; i++) { for (int j = 0; j < tailoring_h; j++) { if (area.contains(i, j)) {
bfm1.setRGB(i, j, sourceSmall.getRGB(i, j));
} if (bigarea.contains(i, j)) {
bfm2.setRGB(i, j, Color.black.getRGB());
}
}
} //处理图片的边缘高亮及其阴影效果
BufferedImage resultImgBuff = dealLightAndShadow(bfm1, area); //生成随机名称
String smallFileName = randomImgName("small_" + filename.replaceAll(".png", "") + "_");
File smallfile = new File(imgPath + File.separator + smallFileName); if (!smallfile.exists()) { //因为根据坐标点生成小图,如果已经存在,那么就不需要重复创建,直接使用
ImageIO.write(resultImgBuff, "png", smallfile);
} Map result = new HashMap();
result.put("smallImgName", smallFileName); //将灰色图当做水印印到原图上
String bigImgName = createBigImg(bfm2, new File(sourceImgPath + File.separator + filename), filename);
result.put("bigImgName", bigImgName);
result.put("location_y", String.valueOf(location_y));
result.put("sourceImgName", filename); return result;
}