java图片操作--生成与原图对称的图片

时间:2021-06-16 17:48:29

java图片操作--生成与原图对称的图片

package com.pay.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import javax.imageio.ImageIO; import org.apache.commons.io.IOUtils; public class ImageChange { public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub String path="F:\\123.jpg";
String desc ="f:\\1234.jpg";
FileInputStream in =new FileInputStream(new File(path));
//读取图片
BufferedImage bufImage = ImageIO.read(in);
int height=bufImage.getHeight();
int width=bufImage.getWidth(); System.out.println(height);
System.out.println(width); // 读取出图片的所有像素
int[] rgbs = bufImage.getRGB(0, 0, width, height, null, 0, width); // 对图片的像素矩阵进行水平镜像
for (int row = 0; row < height; row++) {
for (int col = 0; col < width / 2; col++) {
int temp = rgbs[row * width + col];
rgbs[row * width + col] = rgbs[row * width + (width - 1 - col)];
rgbs[row * width + (width - 1 - col)] = temp;
}
}
// 把水平镜像后的像素矩阵设置回 bufImage
bufImage.setRGB(0, 0, width, height, rgbs, 0, width); // 把修改过的 bufImage 保存到本地
ImageIO.write(bufImage, "JPEG", new File(desc)); } }