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;
-
-
- public class ImageUtil {
-
-
-
- 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;
-
-
- x = Math.max(x, 0);
- y = Math.max(y, 0);
-
-
- if (size > imgWidth || size > imgHeight) {
- size = Math.min(imgWidth, imgHeight);
- }
-
-
- 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;
- for (int i = 0; i < xCount; i++) {
- int $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;
-
-
-
- int rgb = getRandomRgb($x, $y, $width, $height, image);
-
-
- Color color = new Color(rgb);
-
- gs.setColor(color);
-
- gs.fillRect($x, $y, $width, $height);
-
-
-
-
-
- $y += size;
- }
- $x += size;
- }
- gs.dispose();
- ImageIO.write(canvas, "jpg", target);
- }
-
-
-
- public static void mosaicAll(
- InputStream source, OutputStream target, int size
- ) throws IOException {
- mosaic(source, target, 0, 0, -1, -1, size);
- }
-
-
-
- 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;
-
- return image.getRGB(xCenterIndex, yCenterIndex);
- }
-
-
-
- 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);
- }
-
-
-
- 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;
- }
-
-
-
- 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,370, 241, 370, 245, 15);
- System.out.println("处理完成");
- }
- }
-
3.输入输出