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

本文介绍了一种图片处理方法,包括如何压缩图片至指定尺寸并保持画质,以及如何为图片添加文字和logo水印。该方法适用于多种图片格式,并且能够灵活调整水印的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  /***功能:压缩图片变成小尺寸***
  *参数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));// 存盘
    }

    private void saveImage(BufferedImage bi, String savePath, String fileType) throws IOException {
        FileOutputStream out = new FileOutputStream(savePath);
        ImageIO.write(bi, fileType, out);// 存盘
        out.flush();
        out.close();
    }

  /***功能:图片加文字水印***
  *参数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;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值