当前位置: 首页 > news >正文

图片马赛克处理(Java)

1.需求
  • 给图片的指定区域打码
  • 给整张图片打码
  • 马赛克方格取色支持中心点取色和随机取色
  • 马赛克支持灰度处理
2.源码
package com.visy.utils;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Random;/*** @author visy.wang* @date 2024/9/19 9:56*/
public class ImageUtil {/*** 给图片指定区域打马赛克* @param x 打码区域左上角的横坐标* @param y 打码区域左上角的纵坐标* @param width 打码区域的宽度* @param height 打码区域的高度* @param size 马赛克格子尺寸,即每个正方形小方格的边长*/public static void mosaic(InputStream source, OutputStream target, int x, int y, int width, int height, int size) throws IOException {//读取该图片BufferedImage image = ImageIO.read(source);int imgWidth = image.getWidth(), imgHeight = image.getHeight();System.out.println("原图片尺寸:"+imgWidth+"*"+imgHeight);if(size<=0 || width==0 || height==0){//不打马赛克,直接返回原图ImageIO.write(image, "jpg", target);return;}//马赛克区域边界值处理width = width<0||width>imgWidth ? imgWidth : width;height = height<0||height>imgHeight ? imgHeight : height;//起点坐标<0处理x = Math.max(x, 0);y = Math.max(y, 0);//马赛克块大小 不能大于图片宽度和高度,超过时取宽高中小的那一个if (size > imgWidth || size > imgHeight) {size = Math.min(imgWidth, imgHeight);}//创建一张画布(和原图同尺寸,颜色类型选择RGB)BufferedImage canvas = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);//获得画布的画笔Graphics gs = canvas.getGraphics();//先将原图片画到画布上gs.drawImage(image, 0, 0, null);//计算横向和纵向绘制马赛克方格的个数int xCount = width/size + (width%size==0 ? 0 : 1); //横绘绘制个数int yCount = height/size + (height%size==0 ? 0 : 1); //纵向绘制个数//遍历指定区域的所有方格并填充int $x = x;//方格左上角x坐标for (int i = 0; i < xCount; i++) {int $y = y;//方格左上角y坐标//方格的宽度,横向最后一个方格的宽需单独处理int $width = i==xCount-1 ? (x+width-$x) : size;for (int j = 0; j < yCount; j++) {//方格的高度,纵向最后一个方格的高需单独处理int $height = j==yCount-1 ? (y+height-$y) : size;//颜色取方格中心像素点RGB值//int rgb = getCenterRgb($x, $y, $width, $height, image);//颜色取方格内随机像素点RGB值int rgb = getRandomRgb($x, $y, $width, $height, image);//设置颜色(灰度处理)//Color color = new Color(toGray(rgb));Color color = new Color(rgb);//设置颜色gs.setColor(color);//填充方格gs.fillRect($x, $y, $width, $height);//方格加边框(用于测试)//gs.setColor(Color.RED);//gs.drawRect($x, $y, $width, $height);$y += size;//计算下一个方格的左上角y坐标}$x += size;//计算下一行方格的左上角x坐标}gs.dispose(); //释放资源ImageIO.write(canvas, "jpg", target); // 保存图片}/*** 给整张图片打马赛克* @param source 原图输入流* @param target 打码后的图片输出流* @param size 马赛克格子尺寸,即每个正方形小方格的边长*/public static void mosaicAll(InputStream source, OutputStream target, int size) throws IOException {mosaic(source, target, 0, 0, -1, -1, size);}/*** 获取马赛克小方格中心点的颜色* @param x 小方格左上角横坐标* @param y 小方格左上角纵坐标* @param width 小方格的宽度* @param height 小方格的高度* @param image 原图* @return 颜色RGB值*/private static int getCenterRgb(int x, int y, int width, int height, BufferedImage image){//计算当前方格中心点位置int xCenterIndex = x + (width%2==0 ? width : width-1) / 2;int yCenterIndex = y + (height%2==0 ? height : height-1) / 2;//颜色取中心像素点RGB值return image.getRGB(xCenterIndex, yCenterIndex);}/*** 在马赛克小方格区域内随机取一个像素点的颜色* @param x 小方格左上角横坐标* @param y 小方格左上角纵坐标* @param width 小方格的宽度* @param height 小方格的高度* @param image 原图* @return 颜色RGB值*/private static int getRandomRgb(int x, int y, int width, int height, BufferedImage image){//在方格区域内随机取一个点的颜色Random random = new Random();int xIndex = x + random.nextInt(width);int yIndex = y + random.nextInt(height);return image.getRGB(xIndex, yIndex);}/*** 彩色转换成黑白* @param rgb 彩色RGB值* @return 黑白RGB值*/private static int toGray(int rgb){int r = (rgb >> 16) & 0xFF;int g = (rgb >> 8) & 0xFF;int b = rgb & 0xFF;// 计算灰度值int gray = (r + g + b) / 3;return (gray << 16) + (gray << 8) + gray;}/*** 创建目标(输出)文件* @param file 源文件* @return 目标文件*/private static File createTargetFile(File file){String name = file.getName();String newName = name.substring(0, name.lastIndexOf("."))+"_mosaic.jpg";return new File(file.getParent(), newName);}public static void mosaic(File file, int x, int y, int width, int height, int size) throws IOException {InputStream in = Files.newInputStream(file.toPath());OutputStream out = Files.newOutputStream(createTargetFile(file).toPath());mosaic(in, out, x, y, width, height, size);}public static void mosaicAll(File file, int size) throws IOException {InputStream in = Files.newInputStream(file.toPath());OutputStream out = Files.newOutputStream(createTargetFile(file).toPath());mosaicAll(in, out, size);}public static void main(String[] args) throws IOException {File f = new File("E:\\test\\imgs\\2d6aa7e497a059df30d635667b1ec998.jpeg");//mosaic(f, 20 , 560, 500, 150, 10);mosaic(f,370, 241, 370, 245, 15);System.out.println("处理完成");}
}
3.输入输出
  • 处理前

在这里插入图片描述

  • 处理后(中心点取色)

在这里插入图片描述

  • 处理后(灰度处理)

在这里插入图片描述

  • 处理后(随机取色)

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/443923.html

相关文章:

  • python+selenium实现自动联网认证,并实现断网重连
  • 基于机器学习的注意力缺陷/多动障碍 (ADHD)(python论文+代码)HYPERAKTIV
  • Spring Boot 集成 Redisson 实现消息队列
  • go语言Map详解
  • C++——已知数组a[6]={1,3,5,7,9};输入一个数值,要求按照现有排序规律将它放入数组当中。
  • 云计算第四阶段---CLOUD Day7---Day8
  • 深入解析ThingsBoard与ThingsKit物联网平台的差异
  • 五、CAN总线
  • Linux:终端(terminal)与终端管理器(agetty)
  • 钉钉与MySQL对接集成获取部门列表2.0打通EXECUTE语句
  • 微信小程序点赞动画特效实现
  • Day25笔记-普通文件读写with上下文二进制文件csv文件
  • MySQL安装教程
  • 【Windows】快速帮你解决如何找到 Windows 上的 .condarc 文件
  • 『正版软件』XYplorer 专业的 Windows 文件管理工具软件
  • “吉林一号”宽幅02B系列卫星
  • 我的AI工具箱Tauri版-FasterWhisper音频转文本
  • Java后端中的延迟队列实现:使用Redis与RabbitMQ的不同策略
  • Linux中使用cp命令的 -f 选项,但还是提醒覆盖的问题
  • 互联网技术的持续演进:从现在到未来
  • vscode安装ESLint与Vetur插件后自动修复代码不生效
  • 2848、与车相交的点
  • 基于k8s手动部署rabbitmq集群(Manually Deploying RabbitMQ Cluster Based on k8s)
  • mybatis 配置文件完成增删改查(四) :多条件 动态sql查询
  • 先楫HPM6750 Windows下VSCode开发环境配置
  • 【JavaScript】LeetCode:41-45
  • 数据结构(Day18)
  • error: ‘InsertAtTop‘ was not declared in this scope
  • MySQL缓冲池详解
  • 【我的 PWN 学习手札】tcache stash with fastbin double free —— tcache key 绕过