1. 程式人生 > 實用技巧 >java 操作圖片

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"); } }