1.讀取圖片spa
File f = new File(「c:\images\myimage.gif」);server BufferedImage bi = ImageIO.read(f);圖片 |
2.寫圖片ci
BufferedImage im = new BufferedImage(0, 0, BufferedImage. TYPE_INT_RGB);get ImageIO. write(im, "png" , new File( "d://img.png"));it |
3.圖片的信息table
BufferedImage bufferedImage = ImageIO.read(new File("d:/zxing.png"));gc bufferedImage.getWidth(); //寬度im bufferedImage.getHeight(); //高度img 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 center graphics.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(); |