java处理扩大与缩小图片 图片扩大与缩小 图片分层 图片灰度处理 图片变绿色负片 图片红化 图片锐化 java处理扩大与缩小图片 图片
java处理扩大与缩小图片
- 图片扩大与缩小
- 图片分层
- 图片灰度处理
- 图片变绿色负片
- 图片红化
- 图片锐化
java处理扩大与缩小图片
图片扩大与缩小
在java.desktop.javax.imageio包下有操作图片相关的类。
不过,这些ImageIO只支持一些常见的图片类型:jpg,png等。
Java SE ImageIO docs
使用起来也不难:
@Testpublic 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格式。
接着,我们转为2000*1100的图片,格式不变。
这是放大。
如果是缩小,就修改目标长度和宽度。比如800*400
想想,还能做其他的操作吗?
图片分层
比如双层?
@Testpublic 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();
}
图片灰度处理
在或者,图片变黑白?
依然是那张美女图。
@Testpublic 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();
}
图片变绿色负片
说实话,我不太了解图像相关的知识,所以,如果名字不对,请包涵。
@Testpublic 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();
}
图片红化
@Testpublic 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();
}
图片锐化
@Testpublic 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();
}