1.读取图片
File f = new File(“c:\images\myimage.gif”); BufferedImage bi = ImageIO.read(f); |
2.写图片
BufferedImage im = new BufferedImage(0, 0, BufferedImage. TYPE_INT_RGB); ImageIO. write(im, "png" , new File( "d://img.png")); |
3.图片的信息
BufferedImage bufferedImage = ImageIO.read(new File("d:/zxing.png")); bufferedImage.getWidth(); //宽度 bufferedImage.getHeight(); //高度 bufferedImage.getRGB(23,56); //某个位置 |
4.图片合并
BufferedImage nestImage = ImageIO.read(new File("d:/zxing.png")); BufferedImage newImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = newImage.createGraphics(); // (a-b)/2 centergraphics.drawImage(nestImage, 100, 100, null); nestImage.flush(); graphics.dispose(); Graphics2D.drawImage(img, x, y, width, height, observer) Graphics2D.drawImage(img, x, y, width, height, bgcolor, observer) |
5.改变图片大小 BufferedImage.getScaledInstance(200, 200, Image.SCALE_SMOOTH) 获得比例大小的图片
//缩小后的大小为200X200 BufferedImage nestImage = ImageIO.read(new File("d:/zxing.png")); BufferedImage newImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB); newImage.getGraphics().drawImage(nestImage.getScaledInstance(200, 200, Image.SCALE_SMOOTH), 0, 0, null); |
6.带水印 以图片合并的方式来添加一个水印
BufferedImage image = ImageIO.read(new File("d:/zxing.png")); BufferedImage waterMark = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2d = waterMark.createGraphics(); graphics2d.drawImage(image, 0, 0,400,400, null); graphics2d.setColor(Color.GRAY); graphics2d.setFont(new Font(Font.SERIF, Font.BOLD, 40)); graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .8f)); graphics2d.drawString("worterMarker", 100, 100); image.flush(); graphics2d.dispose(); |