当前位置 : 主页 > 编程语言 > java >

java处理扩大与缩小图片

来源:互联网 收集:自由互联 发布时间:2022-07-22
java处理扩大与缩小图片 ​​图片扩大与缩小​​ ​​图片分层​​ ​​图片灰度处理​​ ​​图片变绿色负片​​ ​​图片红化​​ ​​图片锐化​​ java处理扩大与缩小图片 图片


java处理扩大与缩小图片

  • ​​图片扩大与缩小​​
  • ​​图片分层​​
  • ​​图片灰度处理​​
  • ​​图片变绿色负片​​
  • ​​图片红化​​
  • ​​图片锐化​​


java处理扩大与缩小图片

图片扩大与缩小

在​​java.desktop.javax.imageio​​包下有操作图片相关的类。

不过,这些ImageIO只支持一些常见的图片类型:​​jpg,png​​等。

​​Java SE ImageIO docs​​

使用起来也不难:

@Test
public void testUp1() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("test.png");
BufferedImage bufferedImage = ImageIO.read(classPathResource.getInputStream());
BufferedImage image = new BufferedImage(2000,1100 , bufferedImage.getType());
Graphics2D graphics = image.createGraphics();
graphics.drawImage(bufferedImage, 0,0,2000,1100,null);
graphics.dispose();
OutputStream outputStream = new FileOutputStream("out.jpg");
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}

我准备的图片是一张1200*800的图片,jpg格式。

java处理扩大与缩小图片_Java操作图片

java处理扩大与缩小图片_ImageIO处理图片_02

接着,我们转为2000*1100的图片,格式不变。

java处理扩大与缩小图片_Java操作图片_03

这是放大。

如果是缩小,就修改目标长度和宽度。比如800*400

java处理扩大与缩小图片_java自定义图片大小_04

想想,还能做其他的操作吗?

图片分层

比如双层?

@Test
public void testUp1() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("test.jpg");
BufferedImage bufferedImage = ImageIO.read(classPathResource.getInputStream());
BufferedImage image = new BufferedImage(800,800 , bufferedImage.getType());
Graphics2D graphics = image.createGraphics();
graphics.drawImage(bufferedImage, 0,0,800,400,null);
graphics.drawImage(bufferedImage, 0,400,800,400,null);
graphics.dispose();
OutputStream outputStream = new FileOutputStream("out.jpg");
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}

java处理扩大与缩小图片_Java一键处理图片_05

图片灰度处理

在或者,图片变黑白?

依然是那张美女图。

@Test
public void testUp() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("test.jpg");
BufferedImage bufferedImage = ImageIO.read(classPathResource.getInputStream());
ColorSpace space = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(space, null);
BufferedImage image = op.filter(bufferedImage, null);
OutputStream outputStream = new FileOutputStream("out.jpg");
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}

java处理扩大与缩小图片_Java操作图片_06

图片变绿色负片

说实话,我不太了解图像相关的知识,所以,如果名字不对,请包涵。

@Test
public void testUp() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("test.jpg");
BufferedImage bufferedImage = ImageIO.read(classPathResource.getInputStream());
ColorSpace space = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);
ColorConvertOp op = new ColorConvertOp(space, null);
BufferedImage image = op.filter(bufferedImage, null);
OutputStream outputStream = new FileOutputStream("out.jpg");
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}

java处理扩大与缩小图片_Java修改图片的颜色_07

图片红化

@Test
public void testUp() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("test.jpg");
BufferedImage bufferedImage = ImageIO.read(classPathResource.getInputStream());
ColorSpace space = ColorSpace.getInstance(ColorSpace.CS_PYCC);
ColorConvertOp op = new ColorConvertOp(space, null);
BufferedImage image = op.filter(bufferedImage, null);
OutputStream outputStream = new FileOutputStream("out.jpg");
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}

java处理扩大与缩小图片_java自定义图片大小_08

图片锐化

@Test
public void testUp() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("test.jpg");
BufferedImage bufferedImage = ImageIO.read(classPathResource.getInputStream());
ColorSpace space = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
ColorConvertOp op = new ColorConvertOp(space, null);
BufferedImage image = op.filter(bufferedImage, null);
OutputStream outputStream = new FileOutputStream("out.jpg");
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}

java处理扩大与缩小图片_Java操作图片_09


上一篇:Java基础--动态代理是基于什么原理?
下一篇:没有了
网友评论