展开全部
这个是要一个图片中嵌套另外一张图片
你可以62616964757a686964616fe59b9ee7ad9431333431336163试试下面这段代码importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics2D;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
.URL;
importjavax.imageio.ImageIO;
/**
*把两张图片合并
*@authorlizhiyong
*@version$Id:Pic.java,v0.1
-6-3下午3:21:231111Exp$
*/
publicclassPic{
privateFontfont=newFont("宋体",Font.PLAIN,12);//添加字体的属性设置
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
g.drawString(contentArr[i].toString(),this.x,this.y);
this.x+=contentArr[i].toString().length()*this.fontsize/2+5;//重新计算文本输出位置
}
}else{
for(inti=0;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;
}
/**
*修改图片,返回修改后的图片缓冲区(只输出一行文本)
*
*时间:-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("www.?xia_mingjian",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,20,w,h,null);
g.dispose();
}catch(Exceptione){
System.out.println(e.getMessage());
}
returnd;
}
publicstaticvoidmain(String[]args){
Pictt=newPic();
BufferedImaged=tt.loadImageLocal("\\ploanshare\\2\\11.jpg");
BufferedImageb=tt.loadImageLocal("\\ploanshare\\2\\22.png");
//往图片上写文件
//tt.writeImageLocal("E:\\ploanshare\\2\\22.jpg",tt.modifyImage(d,"000000",90,90));
tt.writeImageLocal("\\ploanshare\\2\\cc.jpg",tt.modifyImagetogeter(b,d));
//将多张图片合在一起
System.out.println("success");
}
}