用java处理图片(压缩成小尺寸;加文字和logo水印)

openkk 12年前

/***功能:压缩图片变成小尺寸***    *参数1:oImage:原图;*    *参数2:maxWidth:小尺寸宽度;*    *参数3:maxHeight:小尺寸长度;*    *参数4:newImageName:小尺寸图片存放的路径和新名字;*    *参数5:fileType:小尺寸图片类型(png,gif,jpg...)***/    private void compressImage(File oImage, int maxWidth, int maxHeight, String newImageName, String fileType) {          BufferedImage srcImage = ImageIO.read(oImage);;          int srcWidth = srcImage.getWidth(null);          int srcHeight = srcImage.getHeight(null);          if(srcWidth <= maxWidth && srcHeight <= maxHeight){              saveImage(srcImage);              return;          }          Image scaledImage = srcImage.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH);          double ratio = Math.min((double) maxWidth / srcWidth, (double) maxHeight / srcHeight);          AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);          scaledImage = op.filter(srcImage, null);          saveImage((BufferedImage)scaledImage, fileType, new FileOutputStream(newImageName));// 存盘      }      /***功能:图片加文字水印***    *参数1:oImage:原图;*    *参数2:newImageName:加文字水印图片存放的路径和新名字;*    *参数3:fileType:加文字水印图片类型(png,gif,jpg...);    *参数4:waterText:文字水印的内容****/    private boolean pressText(File oImage, String newImageName, String fileType, String waterText) throws IOException{          BufferedImage originalImage = ImageIO.read(oImage);          int originalWidth = originalImage.getWidth(null);          int originalHeight = originalImage.getHeight(null);          if (originalWidth < 50 || originalHeight < 50){              return false;          }          if(waterText==null || waterText.trim().equals("")){           return false;          }          BufferedImage newImage = new BufferedImage(originalWidth, originalHeight, BufferedImage.TYPE_INT_RGB);          Graphics2D g = newImage.createGraphics();          g.drawImage(originalImage, 0, 0, originalWidth, originalHeight, null);          g.setColor(Color.RED);          int fontSize = 13;          g.setFont(new Font("宋体", Font.PLAIN, fontSize));          g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));          int len = InitServlet.WATER_TEXT.length();          if(InitServlet.IS_WATER_CENTER==0){           g.drawString(InitServlet.WATER_TEXT, originalWidth-len*fontSize/2-6,originalHeight-6);          }else{           g.drawString(InitServlet.WATER_TEXT, (originalWidth-(len*fontSize))/2, (originalHeight-fontSize)/2);          }          g.dispose();   ImageIO.write(newImage, fileType, new FileOutputStream(newImageName));// 存盘   return true;      }        /***功能:图片加logo图片水印***    *参数1:oImage:原图;*    *参数2:newImageName:加logo图片水印存放的路径和新名字;*    *参数3:fileType:加logo图片水印图片类型(png,gif,jpg...);    *参数4:logoPath:logo水印图片的存放路径****/     private boolean pressImage(File oImage, String newImageName, String fileType, String logoPath) throws IOException{          File waterMarkImage = new File(logoPath);          if (!waterMarkImage.exists()) {           return false;          }          BufferedImage originalImage = ImageIO.read(oImage);          BufferedImage watermarkImage = ImageIO.read(waterMarkImage);          int originalWidth = originalImage.getWidth(null);          int originalHeight = originalImage.getHeight(null);          int watermarkWidth = watermarkImage.getWidth(null);          int watermarkHeight = watermarkImage.getHeight(null);          if (originalWidth <= watermarkWidth || originalHeight <= watermarkHeight || originalWidth < 50 || originalHeight < 50) {           return false;          }          BufferedImage newImage = new BufferedImage(originalWidth, originalHeight, BufferedImage.TYPE_INT_RGB);          Graphics2D g = newImage.createGraphics();          g.drawImage(originalImage, 0, 0, originalWidth, originalHeight, null);          g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));          if(InitServlet.IS_WATER_CENTER==0){           g.drawImage(watermarkImage, originalWidth-watermarkWidth-10, originalHeight-watermarkHeight-10, watermarkWidth, watermarkHeight, null);          }else{           g.drawImage(watermarkImage, (originalWidth-watermarkWidth)/2, (originalHeight-watermarkHeight)/2, watermarkWidth, watermarkHeight, null);          }          g.dispose();   ImageIO.write(newImage, fileType, new FileOutputStream(newImageName));// 存盘   return true;      }