需求,形成1天每个小时的画像,并合成一个图片。
思路:每个小时抽取一张图片,再把这种图片截取1/24并存放,后面,再把这24张图片合成一张图片。
准备一张原图
截图前:
截图后:(下午14点的截图)
package com.qihui.qxj.utils;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class ImageUtils {
public static void cutJPG(InputStream input, OutputStream out, int x,
int y, int width, int height) throws IOException {
ImageInputStream imageStream = null;
try {
Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = readers.next();
imageStream = ImageIO.createImageInputStream(input);
reader.setInput(imageStream, true);
ImageReadParam param = reader.getDefaultReadParam();
System.out.println(reader.getWidth(0));
System.out.println(reader.getHeight(0));
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, "jpg", out);
} finally {
imageStream.close();
}
}
public static void cutPNG(InputStream input, OutputStream out, int x,
int y, int width, int height) throws IOException {
ImageInputStream imageStream = null;
try {
Iterator readers = ImageIO.getImageReadersByFormatName("png");
ImageReader reader = readers.next();
imageStream = ImageIO.createImageInputStream(input);
reader.setInput(imageStream, true);
ImageReadParam param = reader.getDefaultReadParam();
System.out.println(reader.getWidth(0));
System.out.println(reader.getHeight(0));
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, "png", out);
} finally {
imageStream.close();
}
}
public static void cutImage(InputStream input, OutputStream out, String type,int x,
int y, int width, int height) throws IOException {
ImageInputStream imageStream = null;
try {
String imageType=(null==type||"".equals(type))?"jpg":type;
Iterator readers = ImageIO.getImageReadersByFormatName(imageType);
ImageReader reader = readers.next();
imageStream = ImageIO.createImageInputStream(input);
reader.setInput(imageStream, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, imageType, out);
} finally {
imageStream.close();
}
}
public static void main(String[] args) throws Exception {
String imgName = "D:\\test.jpg";
BufferedImage sourceImg = ImageIO.read(new FileInputStream(imgName));
//获取原始图片的宽高
int height = sourceImg.getHeight();
int width = sourceImg.getWidth();
sourceImg.flush();
int x = (width / 24) * (14);
int y = 0;
ImageUtils.cutJPG(new FileInputStream(imgName),
new FileOutputStream("D:\\test123.jpg"), x,y,width,height);
/* ImageUtils.cutPNG(new FileInputStream("c:\\1.png"),
new FileOutputStream("c:\\test3.png"), 0,0,50,40); */
}
}
本文章暂时只实现图片的截取,把一张图片切割成24份,取对应小时对应在原图的宽高。如何把24张图片合成一张图片。且看下下文。
如果你热衷技术,喜欢交流,欢迎加入我们!