java 操作图片

最近项目里有需求是根据模板图片生成证件图片,使用后记录demo

package com.scdzyc.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

import javax.imageio.ImageIO;

public class CardPrintImage {

    private Font font = new Font("仿宋", Font.BOLD, 21); // 添加字体的属性设置

    // 导入本地图片到缓冲区
    public BufferedImage loadImageLocal(String imgName) throws IOException {
        return loadImageLocal(new File(imgName));
    }

    //导入本地图片到缓冲区
    public BufferedImage loadImageLocal(File file) throws IOException {
        return ImageIO.read(file);
    }

    //导入网络图片到缓冲区
    public BufferedImage loadImageUrl(String imgName) throws IOException {
        URL url = new URL(imgName);
        return ImageIO.read(url);
    }

    //生成新图片到本地
    public void writeImageLocal(String newImage, BufferedImage img, String formatName) throws IOException {
        if (newImage != null && img != null) {
            File outputfile = new File(newImage);
            ImageIO.write(img, formatName, outputfile);
        }
    }

    /**
     * 设定文字的字体等
     */
    public void setFont(String fontStyle, int fontSize) {
        this.font = new Font(fontStyle, Font.PLAIN, fontSize);
    }

    /**
     * 修改图片,返回修改后的图片缓冲区(只输出一行文本)
   * 参数 :x、y 位置点
*/ public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y, Font font) { try { int w = img.getWidth(); int h = img.getHeight(); Graphics2D g = img.createGraphics(); /** * 设置Graphics2D上下文的背景颜色。背景色用于清除区域。当为组件构造Graphics2D时,背景颜色将继承自该组件。 * 在Graphics2D上下文中设置背景颜色只会影响后续的clearRect调用,而不会影响组件的背景颜色。要更改组件的背景,请使用组件的适当方法 */ g.setBackground(Color.WHITE); if (content != null) { // 将Graphics2D上下文的原点转换为当前坐标系统中的点(x, y)。 // 修改Graphics2D上下文,使其新的原点对应于Graphics2D上下文以前的坐标系中的点(x, y)。 // 在这个图形上下文的后续渲染操作中使用的所有坐标都是相对于这个新的原点的 // 坐标原点设置 //g.translate(w / 2, h / 2); g.translate(0, 0); // g.rotate(8 * Math.PI / 180); if(content instanceof BufferedImage) { BufferedImage head = (BufferedImage) content; g.drawImage(head, x, y, head.getWidth(),head.getHeight(),null); }else{ g.setColor(new Color(0, 0, 0));//设置字体颜色 g.setFont(font); if (font == null){ g.setFont(this.font); } g.drawString(content.toString(), x, y); } } g.dispose(); } catch (Exception e) { System.out.println(e.getMessage()); } return img; } // 修改图片,返回修改后的图片缓冲区(只输出一行文本) public BufferedImage modifyImageYe(BufferedImage img, String context, int x, int y) { int w = img.getWidth(); int h = img.getHeight(); Graphics2D g = img.createGraphics(); g.setBackground(Color.WHITE); g.setColor(Color.blue);//设置字体颜色 if (this.font != null) { g.setFont(this.font); } g.drawString(context, x, y); g.dispose(); return img; } //图片合并 public BufferedImage modifyImagetogeter(BufferedImage source, BufferedImage target, int x, int y) { int w = target.getWidth(); int h = target.getHeight(); Graphics2D g = source.createGraphics(); g.drawImage(target, x, y, w, h, null); g.dispose(); return source; } /** * 图片大小调整 * @param b * @param width * @param height * @return */ public BufferedImage resizeImages(BufferedImage b, int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics2D graphics = image.createGraphics(); graphics.setBackground(Color.BLACK); graphics.drawImage(b, 0, 0, width, height,null); graphics.dispose(); return image; } public static void main(String[] args) throws IOException { CardPrintImage tt = new CardPrintImage(); Font font1 = new Font("宋体", Font.BOLD, 21); Font font2 = new Font("宋体", Font.BOLD, 18); Font font3 = new Font("宋体", Font.BOLD, 19); // BufferedImage d = tt.loadImageLocal("E:\\Images\\hh_front.png");//hh_front.png BufferedImage d = tt.loadImageUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596605351645&di=aa1a159d1a01c5f9d062c7ef05e6d6c8&imgtype=0&src=http%3A%2F%2Fpicture.ik123.com%2Fuploads%2Fallimg%2F180330%2F4-1P330160644.jpg"); tt.modifyImage(d, "齐彬彬", 275, 138,font1); tt.modifyImage(d, "男", 477, 138,font1); tt.modifyImage(d, "620104197706030033115", 312, 194,font2); tt.modifyImage(d, "620100169878787897897879", 285, 250,font2); tt.modifyImage(d, "2020年10月", 313, 308,font3); tt.modifyImage(d, "三年", 490, 308,font3);
     // 加载头像图片 BufferedImage headImage
= tt.loadImageLocal("E:\\Images\\head.jpg"); headImage = tt.resizeImages(headImage,118,148); BufferedImage bufferedImage = tt.modifyImage(d, headImage, 60, 148, null); tt.writeImageLocal("E:\\Images\\hh_front-out.png", d,"png"); System.out.println("success"); } }

 

posted on 2020-08-06 17:17  未知不知  阅读(319)  评论(0)    收藏  举报