1 一个截图,图片中有部分是空白区,想要把空白区域给删除裁剪
解决这个问题,我们需要知道什么样的是空白。像素一样,颜色是白色属于空白区。
BufferedImage.getRGB(int x, int y) 可以拿到图片每个坐标的像素点的颜色,可以判断这个而得到符合算法的一个区域,裁剪这个区域的图片即可。
/**
* 图片去掉下空白
* @return
*/
public static String deletBlank(String imageBase64){
// 转图片文件流,进行操作
try {
BufferedImage bi = Base64Util.getImageInput(imageBase64);
// 根据像素判断图片下边空白开始坐标级大小
int height=bi.getHeight();
int width=bi.getWidth();
int x=0,y=0,x_=0,y_=0;
int pixTemp=0,pixel=0;
// 直接计算相同像素结束坐标
for(int j=0;j<height;j++){
for(int i=0;i<width;i++){
pixel=bi.getRGB(i, j);
if(pixel==pixTemp){
x_=i;y_=j;//相同像素结束的xy坐标
}
else{
x=i;y=j;// 相同像素开始的xy坐标
}
pixTemp=pixel;
}
}
if (y_-y<=20){
//空白高度大于40才进行裁剪
return imageBase64;
}
BufferedImage subimage = bi.getSubimage(0, 0, width, y+20);
ByteArrayOutputStream ot = new ByteArrayOutputStream();
ImageIO.write(subimage, "png",ot);
// byte 转base64
return Base64Util.encodeToString(ot.toByteArray());
} catch (Exception e) {
LogUtils.error(LOG,"图片去下边空白失败,返回原数据",e);
return imageBase64;
}
}
BufferedImage还可以创建Graphics2D,对图片进行一些复杂的操作。这个后面具体分析。下面连接是对Graphics2D的一些介绍
https://blog.csdn.net/mmma2501/article/details/80144315
2 对图片进行旋转
解决这个问题,使用Graphics2D,示例代码如下
package com.huitongjy.generator.web.test.util;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* created by htjy_ks on 2019-07-26
*/
public class ImageUtil {
/**
* 对图片进行旋转
*
* @param src 被旋转图片
* @param angel 旋转角度
* @return 旋转后的图片
*/
public static BufferedImage Rotate(Image src, int angel) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
// 计算旋转后图片的尺寸
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
src_width, src_height)), angel);
BufferedImage res = null;
res = new BufferedImage(rect_des.width, rect_des.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
// 进行转换
g2.translate((rect_des.width - src_width) / 2,
(rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, null, null);
return res;
}
/**
* 计算旋转后的图片
*
* @param src 被旋转的图片
* @param angel 旋转角度
* @return 旋转后的图片
*/
public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
// 如果旋转的角度大于90度做相应的转换
if (angel >= 90) {
if (angel / 90 % 2 == 1) {
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new Rectangle(new Dimension(des_width, des_height));
}
}
3 改变图片后缀的格式
一般可以直接修改文件名,格式就跟着边了,这做一个png转jpg的方式
public static void convertPngToJpg(File file,String newFilePath){
try {
BufferedImage bufferedImage = ImageIO.read(file);
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),bufferedImage.getHeight(),BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, null);
ImageIO.write(ImageUtil.Rotate(newBufferedImage,180),"jpg",new File(newFilePath));
} 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) {
// String sourcePath = "/Users/htjy_ks/Documents/picture/444.jpg";
// convertPngToJpg(new File(sourcePath_),sourcePath);
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();
Rectangle rect = new 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;
}
}
}
上面这个方法,不知道为啥不支持png格式,所以后来先把图片转jpg的,然后在处理。当然后面找到了更简单的方式。
https://blog.csdn.net/project_shuai/article/details/98495370
后续有关图片的问题处理会继续更新...