Java实现给图片局部打马赛克

2023-11-12

因为项目需要用到给图片打码的功能,本身实现也不是很复杂,就没有借鉴其他类库,参照网上的一些资料实现了一个简单的给图片局部打码的功能。也可以给一个图片的多个局部进行打码。

ImageArea.java

package com.test;

/**
 * 图片区域类
 * @author hty
 *
 */
public class ImageArea {
	int x;	//指定区域左上角横坐标
	int y;	//指定区域左上角纵坐标
	int width;	//指定区域宽度
	int height;	//指定区域高度
	public ImageArea(int x, int y, int width, int height) {
		super();
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + height;
		result = prime * result + width;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ImageArea other = (ImageArea) obj;
		if (height != other.height)
			return false;
		if (width != other.width)
			return false;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	
}

ImageUtil.java

package com.test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * 图片处理类
 * @author hty
 *
 */
public class ImageUtil {
	/**
	 * 给图片指定位置打马赛克
	 * @param filePath 图片位置
	 * @param targetPath 打码后的图片保存位置,若为空则保存路径默认为原图片路径
	 * @param x 图片要打码区域左上角的横坐标
	 * @param y 图片要打码区域左上角的纵坐标
	 * @param width 图片要打码区域的宽度
	 * @param height 图片要打码区域的高度
	 * @param mosaicSize 马赛克尺寸,即每个矩形的长宽
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings("static-access")
	public static boolean mosaic(String filePath, String targetPath, 
			int x, int y, int width, int height, int mosaicSize) throws IOException {
		//1. 初始化图像处理各变量
		if (!filePath.endsWith(".png") && !filePath.endsWith(".jpg") &&
				!filePath.endsWith(".gif")) {
			System.err.println("ImageUtil>>>文件名非法,不是正确的图片文件名");
			return false;
		}
		int index = filePath.lastIndexOf(".");
		String suffix = filePath.substring(index + 1);
		if (targetPath != null && !targetPath.isBlank() && !targetPath.endsWith(suffix)) {
			System.err.println("ImageUtil>>>目标文件后缀应与源文件后缀一致");
			return false;
		}
		File file = new File(filePath);
		if (!file.isFile()) {
			System.err.println("ImageUtil>>>" + filePath + "不是一个文件!");
			return false;
		}
		BufferedImage bi = ImageIO.read(file); // 读取该图片
		BufferedImage spinImage = new BufferedImage(bi.getWidth(),
				bi.getHeight(), bi.TYPE_INT_RGB);
		if (bi.getWidth() < mosaicSize || bi.getHeight() < mosaicSize || mosaicSize <= 0) { // 马赛克格尺寸太大或太小
			System.err.println("马赛克尺寸设置不正确");
			return false;
		}

		//2. 设置各方向绘制的马赛克块个数
		int xcount = 0; // 方向绘制个数
		int ycount = 0; // y方向绘制个数
		if (width % mosaicSize == 0) {
			xcount = width / mosaicSize;
		} else {
			xcount = width / mosaicSize + 1;
		}
		if (height % mosaicSize == 0) {
			ycount = height / mosaicSize;
		} else {
			ycount = height / mosaicSize + 1;
		}

		//3. 绘制马赛克(绘制矩形并填充颜色)
		Graphics gs = spinImage.getGraphics();
		gs.drawImage(bi, 0, 0, null);
		int xTmp = x;
		int yTmp = y;
		for (int i = 0; i < xcount; i++) {
			for (int j = 0; j < ycount; j++) {
				//马赛克矩形格大小
				int mwidth = mosaicSize;
				int mheight = mosaicSize;
				if(i == xcount - 1){   //横向最后一个比较特殊,可能不够一个size
					mwidth = width - xTmp;
				}
				if(j == ycount - 1){  //同理
					mheight = height - yTmp;
				}
				//矩形颜色取中心像素点RGB值
				int centerX = xTmp;
				int centerY = yTmp;
				if (mwidth % 2 == 0) {
					centerX += mwidth / 2;
				} else {
					centerX += (mwidth - 1) / 2;
				}
				if (mheight % 2 == 0) {
					centerY += mheight / 2;
				} else {
					centerY += (mheight - 1) / 2;
				}
				Color color = new Color(bi.getRGB(centerX, centerY));
				gs.setColor(color);
				gs.fillRect(xTmp, yTmp, mwidth, mheight);
				yTmp = yTmp + mosaicSize;// 计算下一个矩形的y坐标
			}
			yTmp = y;// 还原y坐标
			xTmp = xTmp + mosaicSize;// 计算x坐标
		}
		gs.dispose();
		if (targetPath == null || targetPath.isBlank())
			targetPath = filePath;
		File sf = new File(targetPath);
		ImageIO.write(spinImage, suffix, sf); // 保存图片
		return true;
	}

	public static boolean mosaic(String filePath, String targetPath,
			ImageArea area, int mosaicSize) throws IOException {
		return  mosaic(filePath, targetPath, area.getX(), area.getY(),
				area.getWidth(), area.getHeight(), mosaicSize);
	}

	/**
	 * 给图片多个指定位置打马赛克
	 * @param filePath 图片位置
	 * @param targetPath 打码后的图片保存位置,若为空则保存路径默认为原图片路径
	 * @param areaList 图片区域对象数组
	 * @param mosaicSize 马赛克尺寸,即每个矩形的长宽
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("static-access")
	public static boolean mosaic(String filePath, String targetPath,
			List<ImageArea> areaList, int mosaicSize) throws IOException  {
		//1. 初始化图像处理各变量
		if (!filePath.endsWith(".png") && !filePath.endsWith(".jpg") &&
				!filePath.endsWith(".gif")) {
			System.err.println("ImageUtil>>>文件名非法,不是正确的图片文件名");
			return false;
		}
		int index = filePath.lastIndexOf(".");
		String suffix = filePath.substring(index + 1);
		if (targetPath != null && !targetPath.isBlank() && !targetPath.endsWith(suffix)) {
			System.err.println("ImageUtil>>>目标文件后缀应与源文件后缀一致");
			return false;
		}
		File file = new File(filePath);
		if (!file.isFile()) {
			System.err.println("ImageUtil>>>" + filePath + "不是一个文件!");
			return false;
		}
		BufferedImage bi = ImageIO.read(file); // 读取该图片
		BufferedImage spinImage = new BufferedImage(bi.getWidth(),
				bi.getHeight(), bi.TYPE_INT_RGB);
		if (bi.getWidth() < mosaicSize || bi.getHeight() < mosaicSize || mosaicSize <= 0) { // 马赛克格尺寸太大或太小
			System.err.println("马赛克尺寸设置不正确");
			return false;
		}

		Graphics gs = spinImage.getGraphics();
		gs.drawImage(bi, 0, 0, null);
		//对每一个局部区域分别绘制马赛克
		for (ImageArea imageArea : areaList) {
			int x = imageArea.getX();
			int y = imageArea.getY();
			int width = imageArea.getWidth();
			int height = imageArea.getHeight();
			//2. 设置各方向绘制的马赛克块个数
			int xcount = 0; // 方向绘制个数
			int ycount = 0; // y方向绘制个数
			if (width % mosaicSize == 0) {
				xcount = width / mosaicSize;
			} else {
				xcount = width / mosaicSize + 1;
			}
			if (height % mosaicSize == 0) {
				ycount = height / mosaicSize;
			} else {
				ycount = height / mosaicSize + 1;
			}

			//3. 绘制马赛克(绘制矩形并填充颜色)
			int xTmp = x;
			int yTmp = y;
			for (int i = 0; i < xcount; i++) {
				for (int j = 0; j < ycount; j++) {
					//马赛克矩形格大小
					int mwidth = mosaicSize;
					int mheight = mosaicSize;
					if(i == xcount - 1){   //横向最后一个比较特殊,可能不够一个size
						mwidth = width - xTmp;
					}
					if(j == ycount - 1){  //同理
						mheight = height - yTmp;
					}
					//矩形颜色取中心像素点RGB值
					int centerX = xTmp;
					int centerY = yTmp;
					if (mwidth % 2 == 0) {
						centerX += mwidth / 2;
					} else {
						centerX += (mwidth - 1) / 2;
					}
					if (mheight % 2 == 0) {
						centerY += mheight / 2;
					} else {
						centerY += (mheight - 1) / 2;
					}
					Color color = new Color(bi.getRGB(centerX, centerY));
					gs.setColor(color);
					gs.fillRect(xTmp, yTmp, mwidth, mheight);
					yTmp = yTmp + mosaicSize;// 计算下一个矩形的y坐标
				}
				yTmp = y;// 还原y坐标
				xTmp = xTmp + mosaicSize;// 计算x坐标
			}

		}
		gs.dispose();
		if (targetPath == null || targetPath.isBlank())
			targetPath = filePath;
		File sf = new File(targetPath);
		ImageIO.write(spinImage, suffix, sf); // 保存图片
		return true;
	}
}

Main.java

package com.test;

import java.io.IOException;

public class Main {

	public static void main(String[] args) {
		try {
			ImageUtil.mosaic("C:\\Users\\Lenovo\\Pictures\\测试.jpg",
					"C:\\Users\\Lenovo\\Pictures\\sb.jpg", new ImageArea(300, 100, 700, 600), 40);
		} catch (IOException e) {
			System.err.println("出错了!!!!");
			e.printStackTrace();
		}
	}

}

运行效果:
打码前:
在这里插入图片描述

打码后:
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java实现给图片局部打马赛克 的相关文章

随机推荐

  • GNS3 配置GRE

    1 简述 GRE Generic Routing Encapsulation GRE是一种最传统的隧道协议 其根本功能就是要实现隧道功能 以实现异地网络之间可以通过内部私网相互访问 以上图为例 假设IP地址为10 1 1 1的XP1想访问I
  • 基于Zigbee的SHT10温湿度数据采集系统(已实现控制12个终端节点)——Zigbee协调器主要代码解析

    之前实现了基于Zigbee的SHT10温湿度数据采集系统 这里来重新复盘一些主要的知识和代码 写在前面 1 功能介绍 使用Zigbee终端节点采集环境的温度和湿度数据 然后将数据无线发送的Zigbee协调器 最后在电脑端显示获得到的数据 2
  • Ubuntu初学思维导图(后继续补充)

    关于虚拟机 Ubuntu的命令内容简要 1 创建用户 sudo adduser user01 创建用户时 同步创建对应组 同步创建家目录 sudo useradd user02 仅创建用户 单独设置完密码后 才能登陆 2 修改用户密码 su
  • http请求头部(header)详解

    当我们在浏览器中访问一个网站时 我们的浏览器实际上会向该网站发送一个 HTTP 请求 而 HTTP 请求头部 Header 则是一组包含请求信息的键值对 用来描述 HTTP 请求的各种属性和特征 以下是 HTTP 请求头部的一些常见属性和含
  • linux重启服务的脚本命令

    最近做网站测试 每次测试完成都要重启服务 为此写了一个简单的shell脚本 linux服务重启shell脚本示例 2014年12月18日 linux服务重启脚本 如何实现linux服务的定时重启 可以借助shell脚本来完成 ps命令捕获进
  • 方差分析在特征筛选中的应用

    方差分析在特征筛选中的应用 方差分析 Analysis of Variance 简称ANOVA 是一种常见的统计分析方法 它可以用于比较两个或多个组之间的均值差异 在机器学习中 我们可以应用方差分析来进行特征筛选 从而得到对模型有显著影响的
  • 高光谱图像端元提取——vertex component analysis(VCA/python)

    在高光谱图像中 VCA是一种常用的端元提取方法 算法来源 Vertex Component Analysis A Fast Algorithm to Unmix Hyperspectral Data submited to IEEE Tra
  • 伺服电机堵转检测

    一 电流数据的分析 电机工作时的电流如下图 电机正常工作时 电机电流具有两个状态 正常旋转和堵转 正常旋转时 电流在控制算法的作用下 一开始会有很快的上升 过程中电流受到控制算法的作用 没有平稳阶段 堵转时 电机结束了控制算法 所以堵转时电
  • 常见信息安全加密算法及Python库源码实例

    1 常见的信息安全加密算法 1 1 对称加密算法 AES Advanced Encryption Standard 对称密钥加密算法 被广泛使用且安全可靠 DES Data Encryption Standard 对称密钥加密算法 已被AE
  • 系统服务器性能巡检报告,pc服务器巡检报告

    pc服务器巡检报告 内容精选 换一换 简要介绍iPerf是一种命令行工具 它通过测量服务器可以处理的最大网络吞吐量来测试网络速度 因此在遇到网络速度问题时特别有用 通过该工具可以确定哪台服务器存在性能问题 语言 C C 一句话概述 网络性能
  • 当HBase遇上MapReduce

    第1关 HBase的MapReduce快速入门 本关任务 编写一个HBase的MapReduce配置程序 package com processdata import java io IOException import java util
  • K8S 剖析API对象类型

    K8S API对象类型 一 metav1 TypeMeta 对象的类型元数据信息 1 1 类型成员 定义了资源类型和api版本 type TypeMeta struct Kind string json kind omitempty pro
  • 数据库读写分离,主从同步实现方法

    前言 众所周知 随着用户量的增多 数据库操作往往会成为一个系统的瓶颈所在 而且一般的系统 读 的压力远远大于 写 因此我们可以通过实现数据库的读写分离来提高系统的性能 实现思路 通过设置主从数据库实现读写分离 主数据库负责 写操作 从数据库
  • regionprops函数用法详解

    转自 http apps hi baidu com share detail 24010679 Regionprops 用途是get the properties of region 即用来度量图像区域属性的函数 语法 STATS regi
  • latex公式记录(待完善)

    记录使用latex过程中遇到的公式 符号等 1 空格 quad 2 大于等于 geq 小于等于 leq 大于 textgreater 小于 textless 3 括号 大括号 a 绝对值 lvert a rvert 内积尖括号 langle
  • 【PCIe 5.0 - 8】PCIe 事务层详解4 - First/Last DW Byte Enables规则

    First Last DW Byte Enables规则 Byte Enables包含在Memory I O 和Configuration Requests中 本节定义了相应的规则 Byte Enables出现在Request header
  • IDE /字符串 /字符编码与文本文件(如cpp源代码文件)

    文章目录 概述 文本编辑器如何识别文件的编码格式 优先推测使用了UTF 8编码 字符编码的BOM字节序标记 重分析各文本编辑器下的测试效果 Qt Creator的文本编辑器 系统记事本 VS的文本编辑器 Notepad 编译器与代码文件的字
  • Python实现十进制小数转IEEE754单精度浮点数转换

    def ConvertFixedIntegerToComplement fixedInterger 浮点数整数部分转换成补码 整数全部为正 return bin fixedInterger 2 def ConvertFixedDecimal
  • VUE自定义指令directive和inserted

    VUE自定义指令directive和inserted 1 什么事自定义指令 1 2使用自定义指令的方式 2 设置自定义组件 2 1 设置全局指令 2 2 设置私有指令 2 3 钩子函数 3 案例 1 什么事自定义指令 指令 Directiv
  • Java实现给图片局部打马赛克

    因为项目需要用到给图片打码的功能 本身实现也不是很复杂 就没有借鉴其他类库 参照网上的一些资料实现了一个简单的给图片局部打码的功能 也可以给一个图片的多个局部进行打码 ImageArea java package com test 图片区域