本文主要是介绍JAVA实现图片打马赛克,高斯模糊处理图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JAVA实现图片打马赛克,高斯模糊处理图片
项目中有时需要对敏感图片进行打码或模糊处理,下面有两个方法可以参考
1.对图片固定坐标进行遮挡
public static final String BASE64_HEAD = "data:image/jpg;base64,";
/*** 图片打马赛克,返回base64* @param path* @return*/public static String imageMosaic(String path) {String mosaicPath = "";try {// 加载图像BufferedImage bufferedImage = getImageInfo(path);if (bufferedImage == null) {return mosaicPath;}// 创建与输入图像相同大小的新图像BufferedImage outputImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);// 创建画笔Graphics graphics = outputImage.getGraphics();// 将原始图像绘制到新图像中graphics.drawImage(bufferedImage, 0, 0, null);// 打码区域左上角x坐标int x = 50;// 打码区域左上角y坐标int y = 50;// 打码区域宽度int width = 90;// 打码区域高度int height = 90;graphics.setColor(Color.GRAY);graphics.fillRect(x, y, width, height);// 释放资源graphics.dispose();mosaicPath = encode(bufferedImageToBytes(outputImage));} catch (IOException e) {System.out.println("处理图像失败: " + e.getMessage());}return BASE64_HEAD + mosaicPath;}
1.1.BufferedImage bufferedImage = getImageInfo(path);中用到的方法
/*** 获取图片信息** @param imageUrl 图片地址* @return*/@SneakyThrowspublic static BufferedImage getImageInfo(String imageUrl) {InputStream inputStream = readFile(imageUrl);if (inputStream == null) {return null;}return ImageIO.read(inputStream);}
1.2.InputStream inputStream = readFile(imageUrl);用到的方法
/*** url转InputStream** @param fileUrl* @return InputStream*/public static InputStream readFile(String fileUrl) {if (BasicUtil.isNullOrEmpty(fileUrl)) {return null;}try {URL url = new URL(fileUrl);HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();urlCon.setConnectTimeout(6000);urlCon.setReadTimeout(6000);int code = urlCon.getResponseCode();if (code != HttpURLConnection.HTTP_OK) {throw new Exception("文件读取失败" + code);}return urlCon.getInputStream();} catch (Exception e) {e.printStackTrace();}return null;}
1.3.mosaicPath = encode(bufferedImageToBytes(outputImage));用到的方法
/*** 图片转字符串* @param image 图片Buffer* @return Base64编码*/public static String encode(byte[] image) {Base64.Encoder encoder = Base64.getEncoder();return replaceEnter(encoder.encodeToString(image));}/*** 字符替换* @param str 字符串* @return 替换后的字符串*/public static String replaceEnter(String str) {String reg = "[\n-\r]";Pattern p = Pattern.compile(reg);Matcher m = p.matcher(str);return m.replaceAll("");}
1.4.mosaicPath = encode(bufferedImageToBytes(outputImage));用到的方法
/*** BufferedImage 转 byte[]** @param image* @return* @throws IOException*/public static byte[] bufferedImageToBytes(BufferedImage image) throws IOException {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ImageIO.write(image, "jpg", outputStream);return outputStream.toByteArray();}
2.对图片进行模糊处理,里面用到的方法,在上面有写
//maven依赖
<dependency><groupId>com.jhlabs</groupId><artifactId>filters</artifactId><version>2.0.235-1</version>
</dependency>
//gradle依赖
implementation 'com.jhlabs:filters:2.0.235-1'
/*** 高斯模糊处理图片,返回base64* @param url* @return*/@SneakyThrowspublic static String fuzzyImage(String url) {BufferedImage bufferedImage = getImageInfo(url);if (bufferedImage == null) {return "";}GaussianFilter filter = new GaussianFilter();//数字越大越模糊filter.setRadius(10);BufferedImage dstImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_USHORT_565_RGB);filter.filter(bufferedImage, dstImage);return BASE64_HEAD + encode(bufferedImageToBytes(dstImage));}
public static void main(String[] args) {String path = "https://123456.jpg";System.out.println(fuzzyImage(path));}
欢迎交流互相学习
这篇关于JAVA实现图片打马赛克,高斯模糊处理图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!