继续介绍图片处理的基本操作
旋转图像
参数有3种,顺时针/逆时针/自定义角度
图像处理代码:
public static BufferedImage rotationimage(BufferedImage bi, double theta) {
if (bi == null) {
return null;
}
int width = bi.getWidth();
int height = bi.getHeight();
int swidth;
int sheight;
int angle = 0;
BufferedImage result = null;
while (theta < 0) {
theta = theta + 360;
}
angle = (int) (theta) % 360;
if (angle == 90) {
result = rotationimage_RightAngle(bi, ImageTool.ROTATIONIMG_CCW);
} else if (angle == 180) {
result = rotationimage_RightAngle(bi, ImageTool.ROTATIONIMG_HORIZONTALTRUNING);
} else if (angle == 270) {
result = rotationimage_RightAngle(bi, ImageTool.ROTATIONIMG_CW);
} else if (angle == 0) {
result = bi;
} else {
swidth = (int) (Math.sqrt(width * width + height * height));
sheight = swidth;
int xCentre = (int) (width / 2);
int yCentre = (int) (height / 2);
double PiToAngle = Math.PI / 180;
result = new BufferedImage(swidth, sheight, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
double oldtheta = Math.atan2(j - yCentre, i - xCentre);
double r_data = Math.sqrt((j - yCentre) * (j - yCentre) + (i - xCentre) * (i - xCentre));
int newx = (int) (r_data * Math.cos(oldtheta + theta * PiToAngle)) + swidth / 2;
int newy = (int) (r_data * Math.sin(oldtheta + theta * PiToAngle)) + sheight / 2;
result.setRGB(newx, newy, bi.getRGB(i, j));
}
}
}
return result;
}
public static BufferedImage rotationimage_RightAngle(BufferedImage bi, int rightangle) {
if (bi == null) {
return null;
}
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage result = null;
switch (rightangle) {
case ROTATIONIMG_CW:
// 0,0 → h,0 w,h→0,w 0,h→0,0 w,0→h,w
result = new BufferedImage(height, width, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(height - 1 - j, i, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_MIRROR_X:
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(width - 1 - i, j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_MIRROR_Y:
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(i, height - 1 - j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_MIRROR_ORIGINAL:
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(width - 1 - i, height - 1 - j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_CCW:
// 0,0 → 0,w w,h→h,0 0,h→h,w w,0→0,0
result = new BufferedImage(height, width, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(j, width - 1 - i, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_HORIZONTALTRUNING:
// 0,0 → w,h w,h→0,0 0,h→w,o w,0→0,h
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(width - 1 - i, height - 1 - j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_RECOVER:
result = bi;
break;
default:
return null;
}
return result;
}
图像镜像处理
参数有3种 水平镜像/垂直镜像/原点镜像
和旋转中 直角旋转共用代码
镜像图像代码:
public static BufferedImage rotationimage_RightAngle(BufferedImage bi, int rightangle) {
if (bi == null) {
return null;
}
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage result = null;
switch (rightangle) {
case ROTATIONIMG_CW:
// 0,0 → h,0 w,h→0,w 0,h→0,0 w,0→h,w
result = new BufferedImage(height, width, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(height - 1 - j, i, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_MIRROR_X:
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(width - 1 - i, j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_MIRROR_Y:
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(i, height - 1 - j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_MIRROR_ORIGINAL:
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(width - 1 - i, height - 1 - j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_CCW:
// 0,0 → 0,w w,h→h,0 0,h→h,w w,0→0,0
result = new BufferedImage(height, width, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(j, width - 1 - i, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_HORIZONTALTRUNING:
// 0,0 → w,h w,h→0,0 0,h→w,o w,0→0,h
result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
result.setRGB(width - 1 - i, height - 1 - j, bi.getRGB(i, j));
}
}
break;
case ROTATIONIMG_RECOVER:
result = bi;
break;
default:
return null;
}
return result;
}
图像剪切
参数有 X方向 Y方向
代码如下
public static BufferedImage shearImage(BufferedImage bi, double shear_x, double shear_y) {
if (bi == null) {
return null;
}
int width = bi.getWidth(); // 得到源图宽
int height = bi.getHeight(); // 得到源图长
int newWidth=width;
int newHeight=height;
if(shear_x!=1){
newWidth=(int)(width/(1-shear_x));
}
if(shear_y!=1){
newHeight=(int)(height/(1-shear_y));
}
BufferedImage tag = new BufferedImage(newWidth*3, newHeight*3, BufferedImage.TYPE_INT_RGB);
//利用JAVA自带的功能
Graphics2D g2d = (Graphics2D) tag.getGraphics();
//添加变量
AffineTransform at =AffineTransform.getShearInstance(shear_x, shear_y);
g2d.setTransform(at);
g2d.drawImage(bi, 0, 0, null); // 绘制缩小后的图
g2d.dispose();
return tag;
}