Scene: might encounter such a demand, let's look at one day for each hour of changes, and reflected in a picture, that picture which requires the use of cutting and synthesis techniques.
First, we need every hour of the day picture, cut a picture, and then 24 hours spliced into a complete picture
Original:
Cutting chart:
Cutting pictures, see https://blog.csdn.net/qq_16855077/article/details/90378173
These two places the code to 12 1.jpg 13 2.jpg 14 3.jpg, the resulting image as FIG.
FIG Synthesis:
2. Code
-
Package Penalty for
com.qihui.qxj.utils;
-
-
import java.awt.image.BufferedImage;
-
import
java.io.File;
-
-
import javax.imageio.ImageIO;
-
-
/**
-
* The picture stitching multiple images together to form the same as the width of a large picture
-
*
-
* @author Administrator
-
*
-
*/
-
public
class CreateBigImage
{
-
-
public static void main(String[] args) {
-
// get the same width of the image
-
String img1 =
"D:/a/1.jpg";
-
String img2 =
"D:/a/2.jpg";
-
String img3 =
"D:/a/3.jpg";
-
String[] imgs =
new String[] { img1, img2, img3 };
-
-
//Image.mergeImage(imgs, 1, "D:/big.jpg");
-
// //图片拼接
-
merge(imgs,
"jpg",
"D:/big.jpg");
-
-
}
-
-
/**
-
* Java拼接多张图片
-
*
-
* @param pics:图片源文件
-
* (必须要宽度一样),如: String img1 = "D:/imgs/3.jpg"; String img2 =
-
* "D:/imgs/3.jpg"; String img3 = "D:/imgs/big.jpg"; String[]
-
* pics = new String[]{img1,img2,img3};
-
* @param type
-
* :图片输出类型(jpg,png,jpeg...)
-
* @param dst_pic
-
* :图片输出绝对路径,如 String dst_pic="D:/imgs/big2.jpg";
-
* @return
-
*/
-
public static boolean merge(String[] pics, String type, String dst_pic) {
-
-
int len = pics.length;
// 图片文件个数
-
if (len <
1) {
-
System.out.println(
"pics len < 1");
-
return
false;
-
}
-
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(pics[i]);
-
images[i] = ImageIO.read(src[i]);
-
}
catch (Exception e) {
-
e.printStackTrace();
-
return
false;
-
}
-
int width = images[i].getWidth();
-
int height = images[i].getHeight();
-
ImageArrays[i] =
new
int[width * height];
// 从图片中读取RGB
-
ImageArrays[i] = images[i].getRGB(
0,
0, width, height, ImageArrays[i],
0, width);
-
}
-
-
int dst_width =
0;
-
int dst_height =
0;
-
for (
int i =
0; i < images.length; i++) {
-
dst_width += images[i].getWidth();
-
dst_height = dst_height > images[i].getHeight() ? dst_height : images[i].getHeight();
-
}
-
System.out.println(dst_width);
-
System.out.println(dst_height);
-
-
// 生成新图片
-
try {
-
BufferedImage ImageNew =
new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
-
int width_i =
0;
-
for (
int i =
0; i < images.length; i++) {
-
ImageNew.setRGB(width_i,
0, images[i].getWidth(), dst_height, ImageArrays[i],
0, images[i].getWidth());
-
width_i += images[i].getWidth();
-
}
-
-
File outFile =
new File(dst_pic);
-
ImageIO.write(ImageNew, type, outFile);
// 写图片
-
}
catch (Exception e) {
-
e.printStackTrace();
-
return
false;
-
}
-
return
true;
-
}
-
-
}
到这里,图片的合成就完成勒。
转载自:https://blog.csdn.net/qq_16855077/article/details/90408532