- 浏览: 188043 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
refusedlazy:
refusedlazy 写道refusedlazy 写道ref ...
MySQL server has gone away问题 -
refusedlazy:
refusedlazy 写道refusedlazy 写道ref ...
MySQL server has gone away问题 -
refusedlazy:
refusedlazy 写道refusedlazy 写道ref ...
MySQL server has gone away问题 -
refusedlazy:
refusedlazy 写道refusedlazy 写道ref ...
MySQL server has gone away问题 -
refusedlazy:
refusedlazy 写道refusedlazy 写道ref ...
MySQL server has gone away问题
参考网上的资料 修正了里面的问题 增加了补白的功能 重构了代码
getLength 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好 不知道有没有更好地方法~
大并发且图片比较大时,有没有对性能做个测试!?
cool~
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * @author Eric Xu * */ public final class ImageUtils { /** * 图片水印 * @param pressImg 水印图片 * @param targetImg 目标图片 * @param x 修正值 默认在中间 * @param y 修正值 默认在中间 * @param alpha 透明度 */ public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) { try { File img = new File(targetImg); Image src = ImageIO.read(img); int wideth = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(src, 0, 0, wideth, height, null); //水印文件 Image src_biao = ImageIO.read(new File(pressImg)); int wideth_biao = src_biao.getWidth(null); int height_biao = src_biao.getHeight(null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null); //水印文件结束 g.dispose(); ImageIO.write((BufferedImage) image, "jpg", img); } catch (Exception e) { e.printStackTrace(); } } /** * 文字水印 * @param pressText 水印文字 * @param targetImg 目标图片 * @param fontName 字体名称 * @param fontStyle 字体样式 * @param color 字体颜色 * @param fontSize 字体大小 * @param x 修正值 * @param y 修正值 * @param alpha 透明度 */ public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) { try { File img = new File(targetImg); Image src = ImageIO.read(img); int width = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(src, 0, 0, width, height, null); g.setColor(color); g.setFont(new Font(fontName, fontStyle, fontSize)); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y); g.dispose(); ImageIO.write((BufferedImage) image, "jpg", img); } catch (Exception e) { e.printStackTrace(); } } /** * 缩放 * @param filePath 图片路径 * @param height 高度 * @param width 宽度 * @param bb 比例不对时是否需要补白 */ public static void resize(String filePath, int height, int width, boolean bb) { try { double ratio = 0.0; //缩放比例 File f = new File(filePath); BufferedImage bi = ImageIO.read(f); Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH); //计算比例 if ((bi.getHeight() > height) || (bi.getWidth() > width)) { if (bi.getHeight() > bi.getWidth()) { ratio = (new Integer(height)).doubleValue() / bi.getHeight(); } else { ratio = (new Integer(width)).doubleValue() / bi.getWidth(); } AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null); itemp = op.filter(bi, null); } if (bb) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); if (width == itemp.getWidth(null)) g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null); else g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null); g.dispose(); itemp = image; } ImageIO.write((BufferedImage) itemp, "jpg", f); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { pressImage("G:\\imgtest\\sy.jpg", "G:\\imgtest\\test1.jpg", 0, 0, 0.5f); pressText("我是文字水印", "G:\\imgtest\\test1.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.3f); resize("G:\\imgtest\\test1.jpg", 500, 500, true); } public static int getLength(String text) { int length = 0; for (int i = 0; i < text.length(); i++) { if (new String(text.charAt(i) + "").getBytes().length > 1) { length += 2; } else { length += 1; } } return length / 2; } }
getLength 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好 不知道有没有更好地方法~
评论
11 楼
yangpeihai
2009-12-22
谢谢lz。不过调用resize方法后,图片的质量就变差了,期待楼主3.0版本
10 楼
chenlixun
2009-12-02
ericxu131 写道
参考网上的资料 修正了里面的问题 增加了补白的功能 重构了代码
getLength 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好 不知道有没有更好地方法~
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * @author Eric Xu * */ public final class ImageUtils { /** * 图片水印 * @param pressImg 水印图片 * @param targetImg 目标图片 * @param x 修正值 默认在中间 * @param y 修正值 默认在中间 * @param alpha 透明度 */ public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) { try { File img = new File(targetImg); Image src = ImageIO.read(img); int wideth = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(src, 0, 0, wideth, height, null); //水印文件 Image src_biao = ImageIO.read(new File(pressImg)); int wideth_biao = src_biao.getWidth(null); int height_biao = src_biao.getHeight(null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null); //水印文件结束 g.dispose(); ImageIO.write((BufferedImage) image, "jpg", img); } catch (Exception e) { e.printStackTrace(); } } /** * 文字水印 * @param pressText 水印文字 * @param targetImg 目标图片 * @param fontName 字体名称 * @param fontStyle 字体样式 * @param color 字体颜色 * @param fontSize 字体大小 * @param x 修正值 * @param y 修正值 * @param alpha 透明度 */ public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) { try { File img = new File(targetImg); Image src = ImageIO.read(img); int width = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(src, 0, 0, width, height, null); g.setColor(color); g.setFont(new Font(fontName, fontStyle, fontSize)); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y); g.dispose(); ImageIO.write((BufferedImage) image, "jpg", img); } catch (Exception e) { e.printStackTrace(); } } /** * 缩放 * @param filePath 图片路径 * @param height 高度 * @param width 宽度 * @param bb 比例不对时是否需要补白 */ public static void resize(String filePath, int height, int width, boolean bb) { try { double ratio = 0.0; //缩放比例 File f = new File(filePath); BufferedImage bi = ImageIO.read(f); Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH); //计算比例 if ((bi.getHeight() > height) || (bi.getWidth() > width)) { if (bi.getHeight() > bi.getWidth()) { ratio = (new Integer(height)).doubleValue() / bi.getHeight(); } else { ratio = (new Integer(width)).doubleValue() / bi.getWidth(); } AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null); itemp = op.filter(bi, null); } if (bb) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); if (width == itemp.getWidth(null)) g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null); else g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null); g.dispose(); itemp = image; } ImageIO.write((BufferedImage) itemp, "jpg", f); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { pressImage("G:\\imgtest\\sy.jpg", "G:\\imgtest\\test1.jpg", 0, 0, 0.5f); pressText("我是文字水印", "G:\\imgtest\\test1.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.3f); resize("G:\\imgtest\\test1.jpg", 500, 500, true); } public static int getLength(String text) { int length = 0; for (int i = 0; i < text.length(); i++) { if (new String(text.charAt(i) + "").getBytes().length > 1) { length += 2; } else { length += 1; } } return length / 2; } }
getLength 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好 不知道有没有更好地方法~
大并发且图片比较大时,有没有对性能做个测试!?
9 楼
ericxu131
2009-11-19
zelsa 写道
直接用ImageMagick处理吧,就没这么多问题了。
ImageMagick 进行翻转、缩放大小、旋转:
http://www.coffeeba.net/?p=289
ImageMagick 圆角和边框:
http://www.coffeeba.net/?p=271
ImageMagick 教程:
http://www.coffeeba.net/?p=210
再附上一段调用ImageMagick的方法:
ImageMagick 进行翻转、缩放大小、旋转:
http://www.coffeeba.net/?p=289
ImageMagick 圆角和边框:
http://www.coffeeba.net/?p=271
ImageMagick 教程:
http://www.coffeeba.net/?p=210
再附上一段调用ImageMagick的方法:
public static void main(String[] args){ ArrayList<String> command = new ArrayList<String>(); command.add("/usr/bin/convert"); command.add("/home/bigImage.jpg"); command.add("-resize"); command.add("120x80"); command.add("/home/smallImage.jpg"); exec(command); } public static boolean exec(ArrayList command) { Process proc; try { //System.out.println("Trying to execute command " + Arrays.asList(command)); proc = Runtime.getRuntime().exec((String[]) command.toArray(new String[1])); } catch (IOException e) { System.out.println("IOException while trying to execute " + command); return false; } //System.out.println("Got process object, waiting to return."); int exitStatus; while (true) { try { exitStatus = proc.waitFor(); break; } catch (java.lang.InterruptedException e) { System.out.println("Interrupted: Ignoring and waiting"); } } if (exitStatus != 0) { System.out.println("Error executing command: " + exitStatus); } return (exitStatus == 0); }
cool~
8 楼
zelsa
2009-11-18
直接用ImageMagick处理吧,就没这么多问题了。
ImageMagick 进行翻转、缩放大小、旋转:
http://www.coffeeba.net/?p=289
ImageMagick 圆角和边框:
http://www.coffeeba.net/?p=271
ImageMagick 教程:
http://www.coffeeba.net/?p=210
再附上一段调用ImageMagick的方法:
ImageMagick 进行翻转、缩放大小、旋转:
http://www.coffeeba.net/?p=289
ImageMagick 圆角和边框:
http://www.coffeeba.net/?p=271
ImageMagick 教程:
http://www.coffeeba.net/?p=210
再附上一段调用ImageMagick的方法:
public static void main(String[] args){ ArrayList<String> command = new ArrayList<String>(); command.add("/usr/bin/convert"); command.add("/home/bigImage.jpg"); command.add("-resize"); command.add("120x80"); command.add("/home/smallImage.jpg"); exec(command); } public static boolean exec(ArrayList command) { Process proc; try { //System.out.println("Trying to execute command " + Arrays.asList(command)); proc = Runtime.getRuntime().exec((String[]) command.toArray(new String[1])); } catch (IOException e) { System.out.println("IOException while trying to execute " + command); return false; } //System.out.println("Got process object, waiting to return."); int exitStatus; while (true) { try { exitStatus = proc.waitFor(); break; } catch (java.lang.InterruptedException e) { System.out.println("Interrupted: Ignoring and waiting"); } } if (exitStatus != 0) { System.out.println("Error executing command: " + exitStatus); } return (exitStatus == 0); }
7 楼
xiangyh
2009-11-18
多线程的情况会有内存泄露的问题吧!
6 楼
skyuck
2009-11-11
图片大了造成内存溢出
5 楼
fffddd
2009-07-19
处理后的图质量明显低于以前了。
4 楼
331008019
2009-07-10
很不错, 在网上看到很多相关的帖子都是一样的, 这个功能算是最齐全的了, 多谢!
3 楼
longhong
2009-03-09
如何在图片中加斜向文字
2 楼
insiku
2009-01-04
FontMetrics 不好
Font.getStringBounds(String str, FontRenderContext frc)
返回指定 FontRenderContext 中指定 String 的逻辑边界。
Graphics2D.getFontRenderContext()
Font.getStringBounds(String str, FontRenderContext frc)
返回指定 FontRenderContext 中指定 String 的逻辑边界。
Graphics2D.getFontRenderContext()
1 楼
guooscar
2009-01-04
FontMetrics
发表评论
-
Java微信api类库ExWechat发布Maven项目3步整合微信
2014-01-07 16:32 6505官方地址:https://github.com/ericxu1 ... -
apache代理配置
2009-01-08 11:51 1013<VirtualHost *:80> ... -
Derby使用小记
2009-01-06 12:01 1622最近用了把Derby 下载的版本是10.4.2.0 Derby ... -
maven仓库地址备份
2009-01-03 19:40 1633http://repo1.maven.org/maven2/ -
当jar并非maven项目产生时安装方法
2008-12-31 09:25 757安装urlrewrite-3.2.0.jar mvn ins ... -
eclipse 插件URL总汇不断补完中
2008-09-08 15:56 1094编辑properties http://propedit.so ... -
eclipse 3.3 jode jad java反编译插件安装方法
2008-08-05 10:22 2390自从eclipse升级到3.3以后jad插件就一直没有成功的安 ... -
反射+annotation模拟spring2.5的@Autowired标签访问private变量
2008-08-04 15:54 1469这段时间用了一下spring 2.5的@Autowired注释 ... -
超简单java定时器,定时任务 使用 TimerTask 和 Timer
2008-06-16 13:12 2957博客搬家原文请看: http://www.drupaleye. ... -
一本好书Agile Java中文版
2008-05-08 09:30 2664本来想买一本TDD的书的,结果不小心定了这本,细细一看发现真是 ... -
spring里面事务的传播属性和事务隔离级别(复习)
2008-03-27 14:34 4279今天看了在论坛上看了 ... -
在Tomcat中部署Java Web应用程序(转)
2008-03-25 14:27 3451在Tomcat中部署Java Web应 ... -
Struts2配置文件、拦截器等详解(转)
2008-03-24 15:46 6853Struts2中有关struts-default.xml,st ... -
学习了一段时间ROR后反思java
2008-03-24 11:19 1163在学习了一段时间ror后,被它的约定重于配置的思想深深的吸引住 ... -
一些常用jar包的作用补全中
2008-03-21 11:03 4276commons-digester Digester基 ... -
删除了删除了删除了删除了删除了删除了删除了删除了删除了删除了
2008-03-20 15:48 545删除了删除了删除了删除了删除了删除了删除了删除了删除了删除了删 ... -
JSTL-EL表达式
2008-03-19 10:54 7577JSTL-EL表达式 一个EL表达式包含变量和操作符。任何存 ...
相关推荐
主要介绍了Java图片处理 (文字水印、图片水印、缩放、补白)代码实例,本文直接给出实现代码,需要的朋友可以参考下
不用多说,程序写得很清楚了,自己能看得懂的
mode 4 : 只缩放,不裁剪,保留全部图片信息,此时的参数只是限制了生成的图片的最大宽高,不产生补白。 mode 5 : 生成的图比例严格按照需要的比例,宽和高不超过给定的参数。 服务器环境要求 PHP 5.2 ...
煤矿井下图像型早期火灾探测研究王媛彬,马宪民(西安科技大学电控学院,陕西西安?710054)摘要:针对煤矿井下传统火灾探测方法的不足,提出了一种基于图像型的火灾探测方法,阐述了对所获取的红外图像进行预处理、特征提取...
巧用补白艺术促进深度学习——小学英语语篇教学中补白教学策略的运用探究.pdf
边框的里面可以有一层边内... 边内补白分为上边内补白(top),下边内补白(bottom),左边内补白(left),右边内补白(right). 边内补白只有width一种属性. 相关教程:CSS教程:margin属性定义边外补白 padding -- 定义边内补
支持对各种图片的预处理(支持图片的合并,拼接,格式转换,加补白,去除背景色等操作)。 支持批量图片生成PDF,可把图片追加到PDF, 对图片的合并,可以支持拼版操作, 所有功能,封装在20K的dll中,可以嵌入你的...
成语故事,因“补白”而精彩
成语故事,因“补白”而精彩.rar
让学生在"补白"中深度学习.pdf
本文实例讲述了PHP全功能无变形图片裁剪操作类与用法。分享给大家供大家参考,具体如下:...不裁剪,只缩放,不足的不补白,产生的图片为实际有效像素大小,例如有个图片是600X600 现在要生成 120X100 那么缩放后的实际
边框的外面可以有一层边外补白(margin),边外补白可以把块级元素分开.边外补白定义了围绕某种元素(elements)的空白. 可以查看盒模式,了解边外补白和边内补白. 边外补白分为上边外补白(top),下边外补白(bottom),左边外...
解决缩略图按比例缩放并自动补白问题 修改后台添加新闻出现错误继续执行的BUG luocms v2.0.101201 更新信息: 1.增加后台超级管理员无法删除,避免操作失误造成后台系统无法登录。 2.替换原有默认模板,...
最近在做网页拖拽验证码的开源项目,需要在服务端生成图片对应的可移动的色块,但是网上的资源都是做缩略图,对整个图片进行缩放的,所以自己动手,完成了对图片进行裁剪小块的工具 <?php namespace App\Libs; /...
XML应用开发(软件品牌)-1期 4.9 课堂实践-使用CSS容器属性显示XML文档-边距属性margin和补白属性padding.doc 学习资料 复习资料 教学资源
XML应用开发(软件品牌)-1期 4.9 案例分析-使用CSS容器属性显示XML文档-边距属性margin和补白属性padding.doc 学习资料 复习资料 教学资源
为研究微波和常规加热时煤储层内温度场的演化规律及储层瓦斯解吸量的变化规律,采用理论分析和数值模拟的试验方法,构建了以岩石SEM图像为几何特征,瓦斯和煤基质块体为传热介质的共轭传热模型,对比了常规加热和微波...