先将Base64位字符转成图片
/**
* base64字符串转化成图片
* @param imgStr Base64位编码
* @return
* @throws Exception
*/
public static File GenerateImage(String imgStr) throws Exception { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return null;
imgStr = imgStr.substring(22, imgStr.length());
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
//图片文件目录
File imgDir = new File(PropertiesLoader.get("attachment.image.server.local") + "projectRevealReport/revealReportTemp");
//如果图片目录不存在 则创建目录
if(!imgDir.exists()) imgDir.mkdirs();
// 生成jpeg图片
String imgFilePath = PropertiesLoader.get("attachment.image.server.local") + "projectRevealReport/revealReportTemp/revealReportTemp.png";
File imgFile = new File(imgFilePath);
if (!imgFile.exists()) {
imgFile.createNewFile();
}
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
// 拆分图片
splidImg(imgFilePath, 3508);
imgFile.delete();
return imgDir;
}
先按照A4纸的大小去截取图片;
如果高度上有内容,则截取高度+10px,直到无内容时,截取;
/**
* 拆分图片
* @param imgPath
*/
private static void splidImg(String imgPath, int height){
try {
String basePath = imgPath.substring(0,imgPath.lastIndexOf("/"));
BufferedImage im = ImageIO.read(new File(imgPath));
if(im.getHeight() > height){
// 判断是否是纯色;每次高度可自定义
if(isSimpleColorImg(im, 0, height, im.getWidth(), height + 10, 0.98f)){
imageCut(0, 0, im.getWidth(), height, imgPath, basePath + "/1.png");
imageCut(0, height, im.getWidth(), im.getHeight() - height, imgPath, basePath + "/2-Temp.png");
// 操作第二页图片
File file2 = new File(basePath + "/2.png");
file2.createNewFile();
File fileTemp2 = new File(basePath + "/2-Temp.png");
BufferedImage bi = new BufferedImage(im.getWidth(), height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
BufferedImage imTemp2 = ImageIO.read(fileTemp2);
g2.clearRect(0, 0, imTemp2.getWidth(), height);
g2.drawImage(imTemp2, 0, 0, imTemp2.getWidth(), imTemp2.getHeight(), null);
g2.dispose();
ImageIO.write(bi, "png", file2);
// 删除第二页临时文件
fileTemp2.delete();
} else {
//每次高度可自定义
height += 10;
splidImg(imgPath, height);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 图片剪裁
* @param x 距离左上角的x轴距离
* @param y 距离左上角的y轴距离
* @param width 宽度
* @param height 高度
* @param sourcePath 图片源
* @param descpath 目标位置
*/
public static void imageCut(int x, int y, int width, int height, String sourcePath, String descpath) {
FileInputStream is = null;
ImageInputStream iis = null;
try {
is = new FileInputStream(sourcePath);
String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") + 1);
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(fileSuffix);
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
java.awt.Rectangle rect = new java.awt.Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, fileSuffix, new File(descpath));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is = null;
}
if (iis != null) {
try {
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
iis = null;
}
}
}
/**
* 判断是否为纯色
* @param imgPath 图片源
* @param percent 纯色百分比,即大于此百分比为同一种颜色则判定为纯色,范围[0-1]
* @return
* @throws IOException
*/
public static boolean isSimpleColorImg(BufferedImage src,int x, int y, int width, int height, float percent) throws IOException{
int count=0,pixTemp=0,pixel=0;
for(int i=x;i<width;i++){
for(int j=y;j<height;j++){
pixel=src.getRGB(i, j);
if(pixel==pixTemp) //如果上一个像素点和这个像素点颜色一样的话,就判定为同一种颜色
count++;
else
count=0;
if((float)count/((height-y)*(width-x))>=percent) //如果连续相同的像素点大于设定的百分比的话,就判定为是纯色的图片
return true;
pixTemp=pixel;
}
}
return false;
}