java 在图片上写字,两个图片合并的实现方法
实例如下:
packagewriteimg;
importjavax.imageio.ImageIO;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics2D;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.net.URL;
publicclasspic{
privateFontfont=newFont("华文彩云",Font.PLAIN,40);//添加字体的属性设置
privateGraphics2Dg=null;
privateintfontsize=0;
privateintx=0;
privateinty=0;
/**
*导入本地图片到缓冲区
*/
publicBufferedImageloadImageLocal(StringimgName){
try{
returnImageIO.read(newFile(imgName));
}catch(IOExceptione){
System.out.println(e.getMessage());
}
returnnull;
}
/**
*导入网络图片到缓冲区
*/
publicBufferedImageloadImageUrl(StringimgName){
try{
URLurl=newURL(imgName);
returnImageIO.read(url);
}catch(IOExceptione){
System.out.println(e.getMessage());
}
returnnull;
}
/**
*生成新图片到本地
*/
publicvoidwriteImageLocal(StringnewImage,BufferedImageimg){
if(newImage!=null&&img!=null){
try{
Fileoutputfile=newFile(newImage);
ImageIO.write(img,"jpg",outputfile);
}catch(IOExceptione){
System.out.println(e.getMessage());
}
}
}
/**
*设定文字的字体等
*/
publicvoidsetFont(StringfontStyle,intfontSize){
this.fontsize=fontSize;
this.font=newFont(fontStyle,Font.PLAIN,fontSize);
}
/**
*修改图片,返回修改后的图片缓冲区(只输出一行文本)
*/
publicBufferedImagemodifyImage(BufferedImageimg,Objectcontent,intx,
inty){
try{
intw=img.getWidth();
inth=img.getHeight();
g=img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.orange);//设置字体颜色
if(this.font!=null)
g.setFont(this.font);
//验证输出位置的纵坐标和横坐标
if(x>=h||y>=w){
this.x=h-this.fontsize+2;
this.y=w;
}else{
this.x=x;
this.y=y;
}
if(content!=null){
g.drawString(content.toString(),this.x,this.y);
}
g.dispose();
}catch(Exceptione){
System.out.println(e.getMessage());
}
returnimg;
}
/**
*修改图片,返回修改后的图片缓冲区(输出多个文本段)xory:true表示将内容在一行中输出;false表示将内容多行输出
*/
publicBufferedImagemodifyImage(BufferedImageimg,Object[]contentArr,
intx,inty,booleanxory){
try{
intw=img.getWidth();
inth=img.getHeight();
g=img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.RED);
if(this.font!=null)
g.setFont(this.font);
//验证输出位置的纵坐标和横坐标
if(x>=h||y>=w){
this.x=h-this.fontsize+2;
this.y=w;
}else{
this.x=x;
this.y=y;
}
if(contentArr!=null){
intarrlen=contentArr.length;
if(xory){
for(inti=0;i<arrlen;i++){
g.drawString(contentArr[i].toString(),this.x,this.y);
this.x+=contentArr[i].toString().length()
*this.fontsize/2+5;//重新计算文本输出位置
}
}else{
for(inti=0;i<arrlen;i++){
g.drawString(contentArr[i].toString(),this.x,this.y);
this.y+=this.fontsize+2;//重新计算文本输出位置
}
}
}
g.dispose();
}catch(Exceptione){
System.out.println(e.getMessage());
}
returnimg;
}
/**
*修改图片,返回修改后的图片缓冲区(只输出一行文本)
*
*时间:2007-10-8
*
*@paramimg
*@return
*/
publicBufferedImagemodifyImageYe(BufferedImageimg){
try{
intw=img.getWidth();
inth=img.getHeight();
g=img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.blue);//设置字体颜色
if(this.font!=null)
g.setFont(this.font);
g.drawString("reyo.cn",w-85,h-5);
g.dispose();
}catch(Exceptione){
System.out.println(e.getMessage());
}
returnimg;
}
publicBufferedImagemodifyImagetogeter(BufferedImageb,BufferedImaged){
try{
intw=b.getWidth();
inth=b.getHeight();
g=d.createGraphics();
g.drawImage(b,100,10,w,h,null);
g.dispose();
}catch(Exceptione){
System.out.println(e.getMessage());
}
returnd;
}
publicstaticvoidmain(String[]args){
pictt=newpic();
BufferedImaged=tt.loadImageLocal("D:\\11.jpg");
//BufferedImageb=tt
//.loadImageLocal("E:\\文件(word,excel,pdf,ppt.txt)\\zte-logo.png");
tt.writeImageLocal("D:\\cc.jpg",tt.modifyImage(d,"西昌苹果",90,90)
//往图片上写文件
);
//tt.writeImageLocal("D:\\cc.jpg",tt.modifyImagetogeter(b,d));
//将多张图片合在一起
System.out.println("success");
}
}
以上就是小编为大家带来的java在图片上写字,两个图片合并的实现方法全部内容了,希望大家多多支持毛票票~