JAVA对图像取模
就是图片变成点阵
原图
取模效果图
代码如下:
public static void main(String[] args) throws IOException {try {// 读取图像文件BufferedImage image = ImageIO.read(new File("C:/Users/xiaol/Desktop/img/0.jpg"));// 定义阈值(取模的临界值)int threshold = 128;// 遍历图像的每个像素,并根据像素值进行取模操作for (int y = 0; y < image.getHeight(); y++) {for (int x = 0; x < image.getWidth(); x++) {// 获取当前像素的颜色Color color = new Color(image.getRGB(x, y));// 计算灰度值int gray = (color.getRed() + color.getGreen() + color.getBlue()) / 3;// 根据灰度值进行二值化处理int newColor = (gray < threshold) ? 0 : 255;// 设置新的像素值Color newPixel = new Color(newColor, newColor, newColor);image.setRGB(x, y, newPixel.getRGB());}}// 保存处理后的图像File output = new File("C:/Users/xiaol/Desktop/img/output_image.jpg");ImageIO.write(image, "jpg", output);System.out.println("图片取模完成,已保存为output_image.jpg");} catch (IOException e) {e.printStackTrace();}}