java图片处理工具类

功能列表:

  • 图片压缩(支持MultipartFile)
  • 叠加图片
  • 添加文本–可根据textWidth参数自动换行,可设置行间距、字间距
  • 添加水印图片(全屏铺满)
  • 添加水印图片、可设置水印图片旋转角度
  • 添加水印文字(自动换行)、可设置水印文字的旋转角度
  • 获取文本所占长度像素
  • 图片截取
  • 图片高质量缩放
  • 图片png格式缩放
  • 图片克隆副本
  • 读取图片对象
  • 图片输出

完整源码:点击下载

部分源码:
   /**
	 * 图片添加水印图片(全屏铺满)
	 * @param buffImg 源图片
	 * @param waterImgSrc 水印图片路径
	 * @return
	 */
	public static BufferedImage imgWaterFulll(BufferedImage buffImg, String waterImgSrc){
		//获取图片副本
		BufferedImage bufferedImage = imgClone(buffImg);

		//获取Graphics2D绘图
		Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
		graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		graphics2D.setColor(Color.black);

		//添加水印照片
		ImageIcon waterIcon = new ImageIcon(waterImgSrc);
		Image water = waterIcon.getImage();
		int waterHeight = water.getHeight(null);
		int waterWidth = water.getWidth(null);
		int height = (int)Math.ceil(bufferedImage.getHeight() / waterHeight);
		int width = (int)Math.ceil(bufferedImage.getWidth() / waterWidth);
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				int posX = waterWidth * j;
				int posY = waterHeight * i;
				//循环绘制水印
				graphics2D.drawImage(water, posX, posY, null);
			}
		}
		graphics2D.dispose();
		return bufferedImage;
	}
   /**
     * 图片添加文本-根据textWidth参数可自动换行
     * @param buffImg
     * @param font 字体
     * @param text 文字
     * @param x 左边距
     * @param y 右边距
     * @param lineWidth 行宽
     * @param zizeSpaceWidth 字间距宽度
     * @param lineSpaceHeight 行间距高度
     * @return
     * @throws Exception
     */
    public static BufferedImage imgText(BufferedImage buffImg, Font font, String text, float x, float y, float lineWidth, float zizeSpaceWidth, float lineSpaceHeight) throws Exception{

        //复制一个副本
        BufferedImage bufferedImage = imgClone(buffImg);
        //获取Graphics2D绘图
        Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
        //消除java.awt.Font字体的锯齿
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.setColor(Color.black);
        graphics2D.setFont(font);

        //获取文字在图片中所能占的前进宽度
        //int m = graphics2D.getFontMetrics(font).stringWidth(text);

        //计算填充字段内容所占行数
        //int liness = (int) Math.ceil(m / (lineWidth * multiple));

        //绘制文本信息,每次循环绘制一行文本
        int textLength = text.length();
        int textStart = 0; //行文本坐标开始
        int textEnd = 0; //行文本坐标结束
        for(int currLine = 0; textEnd <= textLength; currLine ++){
            textEnd ++;
            // 行文本
            String lineText = "";
            while (textEnd <= textLength){
                lineText = text.substring(textStart, textEnd);
                // 计算文本+字间距所占宽度
                float lineTextWidth = graphics2D.getFontMetrics(font).stringWidth(lineText) + zizeSpaceWidth * (lineText.length() - 1);
                if(lineTextWidth > lineWidth){
                    // 第一个字比行宽还大
                    if(textEnd == 1){
                        throw new Exception("lineWidth参数值太小了,第一个字比行宽还大");
                    }
                    try{
                        lineText = text.substring(textStart, textEnd - 1);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                }else{
                    textEnd ++;
                }
            }
            // 开始绘制文本
            System.out.println(lineText);
            float yLine = y + (font.getSize() * currLine) + (lineSpaceHeight * currLine);
            // 如果有行间距,每次只绘制一个字
            if(0 != zizeSpaceWidth){
                StringBuffer currentText = new StringBuffer();
                Integer currentTextWidth = 0;
                for (int i = 0; i< lineText.length(); i ++) {
                    String t = String.valueOf(lineText.charAt(i));
                    currentText.append(t);
                    graphics2D.drawString(t, x + currentTextWidth + (zizeSpaceWidth * i), yLine);
                    currentTextWidth = graphics2D.getFontMetrics(font).stringWidth(currentText.toString());
                }
            }else{
                graphics2D.drawString(lineText, x, yLine);
            }
            textStart = textEnd - 1;
        }
        graphics2D.dispose();
        return bufferedImage;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值