实例如下:
package writeimg; import javax.imageio.imageio; import java.awt.color; import java.awt.font; import java.awt.graphics2d; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.net.url; public class pic { private font font = new font("华文彩云", font.plain, 40);// 添加字体的属性设置 private graphics2d g = null; private int fontsize = 0; private int x = 0; private int y = 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(string fontstyle, int fontsize) { this.fontsize = fontsize; this.font = new font(fontstyle, font.plain, fontsize); } /** * 修改图片,返回修改后的图片缓冲区(只输出一行文本) */ public bufferedimage modifyimage(bufferedimage img, object content, int x, int y) { try { int w = img.getwidth(); int h = 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 (exception e) { system.out.println(e.getmessage()); } return img; } /** * 修改图片,返回修改后的图片缓冲区(输出多个文本段) xory:true表示将内容在一行中输出;false表示将内容多行输出 */ public bufferedimage modifyimage(bufferedimage img, object[] contentarr, int x, int y, boolean xory) { try { int w = img.getwidth(); int h = 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) { int arrlen = contentarr.length; if (xory) { for (int i = 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 (int i = 0; i < arrlen; i++) { g.drawstring(contentarr[i].tostring(), this.x, this.y); this.y += this.fontsize + 2;// 重新计算文本输出位置 } } } 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("reyo.cn", 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, 100, 10, w, h, null); g.dispose(); } catch (exception e) { system.out.println(e.getmessage()); } return d; } public static void main(string[] args) { pic tt = new pic(); bufferedimage d = tt.loadimagelocal("d:\\11.jpg"); // bufferedimage b = 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 在图片上写字,两个图片合并的实现方法全部内容了,希望大家多多支持移动技术网~
如对本文有疑问, 点击进行留言回复!!
Java Class.forName()用法和newInstance()方法原理解析
网友评论