java 图像操作_java 操作图片

packagecom.scdzyc.utils;import java.awt.*;importjava.awt.image.BufferedImage;import java.io.*;importjava.net.URL;importjavax.imageio.ImageIO;public classCardPrintImage {private Font font = new Font("仿宋", Font.BOLD, 21); //添加字体的属性设置//导入本地图片到缓冲区

public BufferedImage loadImageLocal(String imgName) throwsIOException {return loadImageLocal(newFile(imgName));

}//导入本地图片到缓冲区

public BufferedImage loadImageLocal(File file) throwsIOException {returnImageIO.read(file);

}//导入网络图片到缓冲区

public BufferedImage loadImageUrl(String imgName) throwsIOException {

URL url= newURL(imgName);returnImageIO.read(url);

}//生成新图片到本地

public void writeImageLocal(String newImage, BufferedImage img, String formatName) throwsIOException {if (newImage != null && img != null) {

File outputfile= newFile(newImage);

ImageIO.write(img, formatName, outputfile);

}

}/*** 设定文字的字体等*/

public void setFont(String fontStyle, intfontSize) {this.font = newFont(fontStyle, Font.PLAIN, fontSize);

}/*** 修改图片,返回修改后的图片缓冲区(只输出一行文本)

* 参数 :x、y 位置点*/

public BufferedImage modifyImage(BufferedImage img, Object content, int x, inty, 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 instanceofBufferedImage) {

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());

}returnimg;

}//修改图片,返回修改后的图片缓冲区(只输出一行文本)

public BufferedImage modifyImageYe(BufferedImage img, String context, int x, inty) {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();returnimg;

}//图片合并

public BufferedImage modifyImagetogeter(BufferedImage source, BufferedImage target, int x, inty) {int w =target.getWidth();int h =target.getHeight();

Graphics2D g=source.createGraphics();

g.drawImage(target, x, y, w, h,null);

g.dispose();returnsource;

}/*** 图片大小调整

*@paramb

*@paramwidth

*@paramheight

*@return

*/

public BufferedImage resizeImages(BufferedImage b, int width, intheight) {

BufferedImage image= newBufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

Graphics2D graphics=image.createGraphics();

graphics.setBackground(Color.BLACK);

graphics.drawImage(b,0, 0, width, height,null);

graphics.dispose();returnimage;

}public static void main(String[] args) throwsIOException {

CardPrintImage tt= newCardPrintImage();

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值