package com.alarmtech.basic.utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
/**
* @author haomingyang
* @version 1.0
* @description: TODO
* @date 2021/10/28 17:30
*/
public class PrintImage {
// 添加字体的属性设置
private Font font = new Font("黑体", Font.PLAIN, 25);
private Graphics2D g = null;
private int fontsize = 0;
/**
* 导入本地图片到缓冲区
*/
public BufferedImage loadImageLocal(String imgName) {
try {
return ImageIO.read(new File(imgName));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}
/**
* 导入网络图片到缓冲区
*/
public BufferedImage loadImageUrl(String imgName) {
try {
URL url = new URL(imgName);
return ImageIO.read(url);
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}
/**
* 生成新图片到本地
*/
public void writeImageLocal(String newImage, BufferedImage img) {
if (newImage != null && img != null) {
try {
File outputfile = new File(newImage);
ImageIO.write(img, "jpg", outputfile);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 设定文字的字体等
*/
public void setFont(Font font) {
this.font = font;
}
/**
* 修改图片,返回修改后的图片缓冲区(只输出一行文本)
*/
public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y,Color color) {
try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.BLUE);
g.setColor(color);//设置字体颜色
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g.setStroke(new BasicStroke(3));
if (this.font != null) {
g.setFont(this.font);
}
if (content != null) {
g.translate(w / 2, h / 2);
//g.rotate(8 * Math.PI / 180);
g.drawString(content.toString(), x, y);
}
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return img;
}
/**
* 修改图片,返回修改后的图片缓冲区(只输出一行文本)
*
* 时间:2007-10-8
*
* @param img
* @return
*/
public BufferedImage modifyImageYe(BufferedImage img) {
try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.blue);//设置字体颜色
if (this.font != null) {
g.setFont(this.font);
}
g.drawString("www.hi.baidu.com?xia_mingjian", w - 85, h - 5);
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return img;
}
public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {
try {
int w = b.getWidth();
int h = b.getHeight();
g = d.createGraphics();
g.drawImage(b, 10, 10, w, h, null);
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return d;
}
/**
* 图片写入另外的图片中
* @param b 主图
* @param d 需要写入的图片
* @param x 写入X坐标位置
* @param y 写入Y坐标位置
* @return
*/
public BufferedImage modifyImages(BufferedImage b, BufferedImage d,int x,int y){
try {
int width = b.getWidth();
int height = b.getHeight();
Image image=b.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage bufferedImage2=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D g = bufferedImage2.createGraphics();
g.drawImage(image, 0, 0,null);
g.drawImage(d, x, y, 50, 50, null);
g.dispose();
return bufferedImage2;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return b;
}
public static void main(String[] args) throws IOException {
PrintImage tt = new PrintImage();
BufferedImage d = tt.loadImageLocal("e://certificate.png");
BufferedImage nImage = tt.loadImageUrl("http://sc.adminbuy.cn/uploads/allimg/210326/1-210326132K9-50.png");
d=tt.modifyImages(d,nImage,10,10);
tt.modifyImage(d, "dataMap.get(certificateTemplateParams.getVarName())", 50, 50,new Color(0,0,0));
tt.writeImageLocal("e://certificate2.png", d);
}
/***
* 插入描边的字体
* @param img
* @param content
* @param w
* @param h
* @return
*/
public BufferedImage modifyShapImg(BufferedImage img, String content, int w, int h) {
// int w = img.getWidth();
// int h = img.getHeight();
g = img.createGraphics();
//Font f = new Font("Courier New", Font.BOLD, 140);
GlyphVector v = font.createGlyphVector(g.getFontMetrics(font).getFontRenderContext(), content);
Shape shape = v.getOutline();
if (content != null) {
g.translate(w, h);
//g.rotate(8 * Math.PI / 180);
//g.drawString(content.toString(), x, y);
}
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g.setColor(new Color(0,90,160));
g.fill(shape);
g.setColor(new Color(248,248,255));
g.setStroke(new BasicStroke(2));
g.draw(shape);
return img;
}
}
java 操作编辑图片
最新推荐文章于 2024-05-21 17:15:57 发布