用java写一个对传入的图片进行亮度调节、锐度调节、对比度调节、色度调节、打马赛克、添加指定贴图的程序
时间: 2023-06-16 09:05:47 浏览: 167
java编写的小软件处理图片
以下是一个用Java实现的图片处理程序,可以对传入的图片进行亮度调节、锐度调节、对比度调节、色度调节、打马赛克和添加指定贴图的操作:
```java
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageProcessor {
public static void main(String[] args) {
// 读取图片文件
BufferedImage image = null;
try {
image = ImageIO.read(new File("input.jpg"));
} catch (IOException e) {
e.printStackTrace();
return;
}
// 调整亮度
adjustBrightness(image, 50);
// 调整锐度
adjustSharpness(image, 0.5);
// 调整对比度
adjustContrast(image, 1.5);
// 调整色度
adjustHue(image, 0.5);
// 打马赛克
mosaic(image, 10);
// 添加贴图
addWatermark(image, "watermark.png");
// 保存处理后的图片
try {
ImageIO.write(image, "jpg", new File("output.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
// 调整亮度
public static void adjustBrightness(BufferedImage image, int brightness) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
double factor = 1.0 + brightness / 100.0;
for (int i = 0; i < pixels.length; i++) {
int alpha = (pixels[i] >> 24) & 0xff;
int red = (pixels[i] >> 16) & 0xff;
int green = (pixels[i] >> 8) & 0xff;
int blue = pixels[i] & 0xff;
red = (int) (red * factor);
green = (int) (green * factor);
blue = (int) (blue * factor);
red = Math.min(255, Math.max(0, red));
green = Math.min(255, Math.max(0, green));
blue = Math.min(255, Math.max(0, blue));
pixels[i] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
image.setRGB(0, 0, width, height, pixels, 0, width);
}
// 调整锐度
public static void adjustSharpness(BufferedImage image, double sharpness) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
int[] newPixels = new int[pixels.length];
int threshold = (int) (sharpness * 255);
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
int alpha = (pixels[y * width + x] >> 24) & 0xff;
int red = (pixels[y * width + x] >> 16) & 0xff;
int green = (pixels[y * width + x] >> 8) & 0xff;
int blue = pixels[y * width + x] & 0xff;
int delta = 0;
delta += Math.abs((pixels[y * width + (x - 1)] >> 16) & 0xff - red);
delta += Math.abs((pixels[y * width + (x + 1)] >> 16) & 0xff - red);
delta += Math.abs((pixels[(y - 1) * width + x] >> 16) & 0xff - red);
delta += Math.abs((pixels[(y + 1) * width + x] >> 16) & 0xff - red);
if (delta > threshold) {
newPixels[y * width + x] = (alpha << 24) | (0 << 16) | (0 << 8) | 0;
} else {
newPixels[y * width + x] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
}
image.setRGB(0, 0, width, height, newPixels, 0, width);
}
// 调整对比度
public static void adjustContrast(BufferedImage image, double contrast) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
double factor = (259 * (contrast + 255)) / (255 * (259 - contrast));
for (int i = 0; i < pixels.length; i++) {
int alpha = (pixels[i] >> 24) & 0xff;
int red = (pixels[i] >> 16) & 0xff;
int green = (pixels[i] >> 8) & 0xff;
int blue = pixels[i] & 0xff;
red = (int) (factor * (red - 128) + 128);
green = (int) (factor * (green - 128) + 128);
blue = (int) (factor * (blue - 128) + 128);
red = Math.min(255, Math.max(0, red));
green = Math.min(255, Math.max(0, green));
blue = Math.min(255, Math.max(0, blue));
pixels[i] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
image.setRGB(0, 0, width, height, pixels, 0, width);
}
// 调整色度
public static void adjustHue(BufferedImage image, double hue) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
float[] hsv = new float[3];
for (int i = 0; i < pixels.length; i++) {
int alpha = (pixels[i] >> 24) & 0xff;
int red = (pixels[i] >> 16) & 0xff;
int green = (pixels[i] >> 8) & 0xff;
int blue = pixels[i] & 0xff;
Color.RGBtoHSB(red, green, blue, hsv);
hsv[0] += hue;
hsv[0] = hsv[0] < 0 ? hsv[0] + 1 : hsv[0] > 1 ? hsv[0] - 1 : hsv[0];
int rgb = Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]);
red = (rgb >> 16) & 0xff;
green = (rgb >> 8) & 0xff;
blue = rgb & 0xff;
pixels[i] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
image.setRGB(0, 0, width, height, pixels, 0, width);
}
// 打马赛克
public static void mosaic(BufferedImage image, int size) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
int[] newPixels = new int[pixels.length];
for (int y = 0; y < height; y += size) {
for (int x = 0; x < width; x += size) {
int red = 0, green = 0, blue = 0, count = 0;
for (int dy = 0; dy < size && y + dy < height; dy++) {
for (int dx = 0; dx < size && x + dx < width; dx++) {
int pixel = pixels[(y + dy) * width + (x + dx)];
red += (pixel >> 16) & 0xff;
green += (pixel >> 8) & 0xff;
blue += pixel & 0xff;
count++;
}
}
red /= count;
green /= count;
blue /= count;
for (int dy = 0; dy < size && y + dy < height; dy++) {
for (int dx = 0; dx < size && x + dx < width; dx++) {
newPixels[(y + dy) * width + (x + dx)] = (255 << 24) | (red << 16) | (green << 8) | blue;
}
}
}
}
image.setRGB(0, 0, width, height, newPixels, 0, width);
}
// 添加贴图
public static void addWatermark(BufferedImage image, String watermarkFile) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage watermark = null;
try {
watermark = ImageIO.read(new File(watermarkFile));
} catch (IOException e) {
e.printStackTrace();
return;
}
Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.drawImage(watermark, width - watermark.getWidth() - 10, height - watermark.getHeight() - 10, null);
graphics.dispose();
}
}
```
其中,`adjustBrightness`、`adjustContrast`和`adjustHue`方法使用了颜色空间转换的方法来实现亮度、对比度和色度的调节,`adjustSharpness`方法使用了边缘检测的方法来实现锐度的调节,`mosaic`方法使用了打马赛克的方法来实现图片的马赛克效果,`addWatermark`方法用来在图片上添加指定的贴图。
阅读全文