图片处理工具

根据原图片,生成指定宽高的图片、

导入依赖

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.16</version>
</dependency>

代码实现

package cn.csp.practice;

import net.coobird.thumbnailator.Thumbnails;

import java.io.IOException;

public class ImageSize {

    /**
     * 根据原图片,生成指定宽高的图片
     * srcPath 原图片路径
     * destPath 新生成图片路径
     * width 宽
     * height 高
     * forceSize 是否强制使用指定宽高,false则会保持原图片宽高比例
     */
    public static boolean resizeImage(String srcPath,String destPath,int width,int height,boolean forceSize) throws IOException {
        if (forceSize){
            Thumbnails.of(srcPath).forceSize(width,height).toFile(destPath);
        }else {
            Thumbnails.of(srcPath).width(width).height(height).toFile(destPath);
        }
        return true;

    }

    public static void main(String[] args) throws IOException {
        String path = "D:\\媒体.png";
        String destPathTrue = "D:\\forceSize_true.png";
        String destPathFalse = "D:\\forceSize_false.png";
        boolean forceSizeTrue = true;
        boolean forceSizeFalse = false;
        boolean imageTrue = ImageSize.resizeImage(path, destPathTrue, 200, 200, forceSizeTrue);
        boolean imageFalse = ImageSize.resizeImage(path, destPathFalse, 200, 200, forceSizeFalse);
    }
}

根据原图片,在指定位置画框

代码实现

package cn.csp.practice;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageDraw {
    public static void main(String[] args) throws IOException {
        String path = "D:\\媒体.png";
        String destPath = "D:\\test.png";
        InputStream in = new FileInputStream(path);
        BufferedImage image = ImageIO.read(new File(path));
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.red);
        graphics.drawRect(100,100,100,100);
        FileOutputStream outputStream = new FileOutputStream(destPath);
        ImageIO.write(image,"png",outputStream);
    }
}

根据原图片,在指定位置添加文字

代码实现

package cn.csp.practice;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

public class ImageFont {

    // 添加水印
    public void addFont(String srcPath,String destPath,String content,Color color,Font font){
        try {
            // 读取原图信息
            File srcFile = new File(srcPath);
            Image srcImage = ImageIO.read(srcFile);
            int srcImageWidth = srcImage.getWidth(null);
            int srcImageHeight = srcImage.getHeight(null);
            // 加水印
            BufferedImage bufferedImage = new BufferedImage(srcImageWidth, srcImageHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = bufferedImage.createGraphics();
            graphics.drawImage(srcImage,0,0,srcImageWidth,srcImageHeight,null);
            graphics.setColor(color);
            graphics.setFont(font);
            // 设置水印的坐标
            int x = srcImageWidth - 2 * getFontContentLength(content, graphics);
            int y = srcImageHeight - 2 * getFontContentLength(content, graphics);
            graphics.drawString(content,x,y);
            graphics.dispose();
            // 输出图片
            FileOutputStream outputStream = new FileOutputStream(destPath);
            ImageIO.write(bufferedImage,"jpg",outputStream);
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //指定位置添加文字
    public void addFontLocal(String srcPath,String destPath,String content,Color color,Font font,int x,int y,int width,int height){
        try {
            // 读取原图信息
            File srcFile = new File(srcPath);
            Image srcImage = ImageIO.read(srcFile);
            int srcImageWidth = srcImage.getWidth(null);
            int srcImageHeight = srcImage.getHeight(null);
            // 加水印
            BufferedImage bufferedImage = new BufferedImage(srcImageWidth, srcImageHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = bufferedImage.createGraphics();
            graphics.drawImage(srcImage,0,0,srcImageWidth,srcImageHeight,null);
            graphics.setColor(color);
            graphics.setFont(font);
            graphics.drawString(content,x,y);
            graphics.drawRect(x,y,width,height);
            graphics.dispose();
            // 输出图片
            FileOutputStream outputStream = new FileOutputStream(destPath);
            ImageIO.write(bufferedImage,"jpg",outputStream);
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public int getFontContentLength(String content,Graphics2D graphics2D){
        return graphics2D.getFontMetrics(graphics2D.getFont()).charsWidth(content.toCharArray(),0,content.length());
    }

    public static void main(String[] args) {
        Font font = new Font("微软雅黑", Font.PLAIN, 15);
        String src = "D:\\流媒体.png";
        String dest = "D:\\流媒体_test.png";
        String destLocal = "D:\\流媒体_local.png";
        String content = "测试内容";
        Color color = new Color(255, 0, 0, 128);
        new ImageFont().addFont(src,dest,content,color,font);
        new ImageFont().addFontLocal(src,destLocal,content,Color.RED,font,200,200,200,200);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值