先贴上源码,再调用测试看效果,整理了3天。如有更好想法或不同见解,欢迎@我(struggle_jb@163.com).

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;/*** @author E-mail:chengjiangbo@lvmama.com* @version 2015-2-27 下午2:10:52** MyImage2*/
public class MyImage2 {private String srcFile;private String destFile;private int width;private int height;private Image img;public String getSrcFile() {return srcFile;}public void setSrcFile(String srcFile) {this.srcFile = srcFile;}public String getDestFile() {return destFile;}public void setDestFile(String destFile) {this.destFile = destFile;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public Image getImg() {return img;}public void setImg(Image img) {this.img = img;}/*** 构造函数* @param fileName 操作的文件名* @param newFileName 生成的新文件名* @throws IOException*/public MyImage2(String fileName,String newFileName) throws IOException {this.srcFile = fileName;this.destFile = newFileName;img = ImageIO.read(new File(fileName));width = img.getWidth(null);//得到源宽度height = img.getHeight(null);//得到源高度}/*** 强制压缩/放大图片到固定的大小* @param newWidth* @param newHeight* @throws IOException*/public void resize(int newWidth,int newHeight) throws IOException{Image img = Toolkit.getDefaultToolkit().getImage(srcFile);BufferedImage bi_scale = toBufferedImage(img,newWidth,newHeight);//第一种通过文件流和JPEGImageEncoder近JPEg编码输出FileOutputStream newImageOPS = new FileOutputStream(destFile);//输出文件流/** JPEGImageEncoder 将图像缓冲数据编码为 JPEG 数据流。该接口的用户应在 Raster* 或 BufferedImage 中提供图像数据,在 JPEGEncodeParams 对象中设置必要的参数,* 并成功地打开 OutputStream(编码 JPEG 流的目的流)。JPEGImageEncoder 接口可* 将图像数据编码为互换的缩略 JPEG 数据流,该数据流将写入提供给编码器的 OutputStream 中。注意:com.sun.image.codec.jpeg 包中的类并不属于核心 Java API。它们属于 Sun 发布的JDK 和 JRE 产品的组成部分。虽然其它获得许可方可能选择发布这些类,但开发人员不能寄希望于从非 Sun 实现的软件中得到它们。我们期望相同的功能最终可以在核心 API 或标准扩展中得到。*/com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(newImageOPS);encoder.encode(bi_scale);//近JPEG编码newImageOPS.close();//第二种通过图片流写入//ImageIO.write(bi_scale, "JPEG", new File(destFile));}/*** 生成图片带红的处理方法* @param image* @param width* @param height* @return*/public static BufferedImage toBufferedImage(Image image,int width,int height) {if (image instanceof BufferedImage) {return (BufferedImage)image;}// This code ensures that all the pixels in the image are loadedimage = new ImageIcon(image).getImage();// Determine if the image has transparent pixels; for this method's// implementation, see e661 Determining If an Image Has Transparent Pixels//boolean hasAlpha = hasAlpha(image);// Create a buffered image with a format that's compatible with the screenBufferedImage bimage = null;GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();try {// Determine the type of transparency of the new buffered imageint transparency = Transparency.OPAQUE;/* if (hasAlpha) {transparency = Transparency.BITMASK;}*/// Create the buffered imageGraphicsDevice gs = ge.getDefaultScreenDevice();GraphicsConfiguration gc = gs.getDefaultConfiguration();bimage = gc.createCompatibleImage(width, height, transparency);} catch (HeadlessException e) {// The system does not have a screen}if (bimage == null) {// Create a buffered image using the default color modelint type = BufferedImage.TYPE_INT_RGB;//int type = BufferedImage.TYPE_3BYTE_BGR;//by wang/*if (hasAlpha) {type = BufferedImage.TYPE_INT_ARGB;}*/bimage = new BufferedImage(width, height, type);}// Copy image to buffered imageGraphics g = bimage.createGraphics();// Paint the image onto the buffered imageg.drawImage(image, 0, 0,width,height, null);绘制缩小的图g.dispose();return bimage;}/*** 按照固定的比例缩放图片* @param t* @throws IOException*/public void resize(double t) throws IOException{int w = (int)(width*t);int h = (int)(height*t);resize(w,h);}/*** 已宽度为基准,等比例缩放图片* @param newWidth* @throws IOException*/public void resizeByWidth(int newWidth) throws IOException{int h = (int)(height*(new Double(newWidth)/width));resize(newWidth,h);}/*** 以高度为基准,等比例缩放图片* @param newHeight* @throws IOException */public void resizeByHeight(int newHeight) throws IOException{int w = (int)(width*(new Double(newHeight)/height));resize(w,newHeight);}/*** 生成规格* @throws IOException */public void resizeFix(int newWidth,int newHeight) throws IOException{if(width>height || (width/height>newWidth/newHeight)){resizeByWidth(newWidth);}else{resizeByHeight(newHeight);}}/** * 添加图片水印 * @param targetImg 目标图片路径,如:C://myPictrue//1.jpg * @param waterImg  水印图片路径,如:C://myPictrue//logo.png * @param x 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间 * @param y 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间 * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @throws Exception */public static void pressImage(String targetImg,String waterImg,int x,int y,float alpha) throws Exception{try {File file = new File(targetImg);Image image = ImageIO.read(file);int width = image.getWidth(null);int height = image.getHeight(null);BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bufferedImage.createGraphics();g.drawImage(image, 0, 0, null);Image waterImage = ImageIO.read(new File(waterImg));//水印文件int width_wi=waterImage.getWidth(null);int height_wi=waterImage.getHeight(null);if(width<=width_wi || height<=height_wi){throw new Exception("原图的宽、高必须大于水印图的宽、高");}AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha);int widthDiff = width-width_wi;int heightDiff = height-height_wi;if(x<0){x = widthDiff/2;}else if(x>widthDiff){x = widthDiff;}if(y<0){y = heightDiff/2;}else if(y>heightDiff){y = heightDiff;}g.drawImage(waterImage, x, y, width_wi,height_wi,null);//水印文件结束g.dispose();ImageIO.write(bufferedImage, "JPEG", file);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 添加文字水印 * @param targetImg 目标图片路径,如:C://myPictrue//1.jpg * @param pressText 水印文字, 如:中国证券网 * @param fontName 字体名称,    如:宋体 * @param fontStyle 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC) * @param fontSize 字体大小,单位为像素 * @param color 字体颜色 * @param x 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间 * @param y 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间 * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) */public static void pressText(String targetImg,String pressText,String fontName,int fontStyle,int fontSize,Color color,int x,int y,float alpha){try {File file = new File(targetImg);Image image = ImageIO.read(file);int width = image.getWidth(null);int height = image.getHeight(null);BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bufferedImage.createGraphics();g.drawImage(image,0,0, width, height, null);g.setFont(new Font(fontName, fontStyle, fontSize));g.setColor(color);g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));int width_wi = fontSize*getTextLength(pressText);int height_wi = fontSize;int widthDiff = width-width_wi;int heightDiff = height-height_wi;if(x<0){x = widthDiff/2;}else if(x>widthDiff){x=widthDiff;}if(y<0){y = heightDiff/2;}else if(y>heightDiff){y = heightDiff;}g.drawString(pressText, x, y+height_wi);//水印文件g.dispose();ImageIO.write(bufferedImage, "JPEG", file);} catch (IOException e) {e.printStackTrace();}}/*** 计算文字像素长度* @param text* @return*/private static int getTextLength(String text){int textLength = text.length();int length = textLength;for (int i = 0; i < textLength; i++) {int wordLength = String.valueOf(text.charAt(i)).getBytes().length;if(wordLength > 1){length+=(wordLength-1);}}return length%2==0 ? length/2:length/2+1;}/*** 旋转任意度数的方法* @param targetImg* @param degree* @param bgcolor* @throws IOException*/public static void rotateImage(String targetImg, int degree, Color bgcolor) throws IOException {  File file = new File(targetImg);BufferedImage sourceImage = ImageIO.read(file);int iw = sourceImage.getWidth();//原始图象的宽度   int ih = sourceImage.getHeight();//原始图象的高度  int w = 0;  int h = 0;  int x = 0;  int y = 0;  degree = degree % 360;  if (degree < 0)  degree = 360 + degree;//将角度转换到0-360度之间  double ang = Math.toRadians(degree);//将角度转为弧度  /** *确定旋转后的图象的高度和宽度 */  if (degree == 180 || degree == 0 || degree == 360) {w = iw;  h = ih;  } else if (degree == 90 || degree == 270) {w = ih;  h = iw;  } else {int d = iw + ih;  w = (int) (d * Math.abs(Math.cos(ang)));  h = (int) (d * Math.abs(Math.sin(ang)));  }  x = (w / 2) - (iw / 2);//确定原点坐标  y = (h / 2) - (ih / 2);  BufferedImage rotatedImage = new BufferedImage(w, h, sourceImage.getType());  Graphics2D gs = (Graphics2D)rotatedImage.getGraphics();  if(bgcolor==null){rotatedImage  = gs.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);  }else{gs.setColor(bgcolor);  gs.fillRect(0, 0, w, h);//以给定颜色绘制旋转后图片的背景  }  //有两种旋转使用方式,第一使用AffineTransformOp,第二使用Graphics2D/* * AffineTransform at = new AffineTransform();  at.rotate(ang, w / 2, h / 2);//旋转图象  at.translate(x, y);  AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);  op.filter(sourceImage, rotatedImage);  sourceImage = rotatedImage;  ImageIO.write(sourceImage, "PNG", file);//这里的格式化请使用PNG格式,否则旋转后会出现红眼效果
*/      BufferedImage bufferedImage = new BufferedImage(w, h, sourceImage.getType());Graphics2D g = bufferedImage.createGraphics();if(bgcolor==null){g.setColor(Color.WHITE);}else{g.setColor(bgcolor);  }g.fillRect(0, 0, w, h);//以给定颜色绘制旋转后图片的背景  g.rotate(Math.toRadians(degree), w/2, h/2);g.translate(x, y);g.drawImage(sourceImage, 0, 0, null);g.dispose();ImageIO.write(bufferedImage, "JPEG", file);//这里的JPEG也可以是PNG}/*** @param args*/public static void main(String[] args) {/*try {//生成缩略图MyImage2 image2 = new MyImage2("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_300_100.jpg");image2.resizeFix(300, 100);//这里缩略图的比例在方法里会计算,防止失真。} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}*//*try {//加图片水印MyImage2.pressImage("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_300_100.jpg","C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.png",300, 300, 0.5f);} catch (Exception e) {e.printStackTrace();}*///加文字水印//MyImage2.pressText("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_300_100.jpg", "江波之印", "宋体", Font.BOLD, 20, Color.WHITE,10, 10, 1f);//旋转任意度数包括直角(90,180,270,360)try {MyImage2.rotateImage("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_300_100.jpg", 90,null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}    }

1、生成缩略图测试:

原图:

缩略图:

2、加水印测试

加图片水印:

加文字水印:

3、旋转任意度数包括直角(90,180,270,360)测试

参考技术链接:

http://hxg1026.iteye.com/blog/667500;

http://blog.csdn.net/mengfei86/article/details/6821553;

http://blog.csdn.net/heliang7/article/details/7309394;

http://www.oschina.net/question/1092_23668;(解决生成的缩略图蒙一层红的bug);

http://www.cnblogs.com/waya/archive/2008/11/04/1326402.html;

java对图片的各种操作(压缩、加水印(文字或图片)、旋转)相关推荐

  1. html5滚动字幕添加图片,抖音视频怎么加动态文字 竖版图片加视频及滚动字幕的方法...

    点击上方链接下载安装好需要的工具,在下载的压缩包里有安装提示给到大家,记得要看哟!那么小编在这里也简单提一下,安装时两个文件都要以管理员身份运行,安装前先关闭360等安全软件以免有文件被误删.打开界面 ...

  2. 咖啡汪日志——JAVA导出pdf文件加水印 文字+图片、文字

    咖啡汪日志--JAVA导出pdf文件加水印 文字和图片.文字 hello,又大家见面了! 作为一只不是在戏精就是在戏精路上的哈士奇,今天要展示给大家的就是如何快捷地给pdf文件增加各种水印.嗷呜呜,前 ...

  3. JAVA实现图片加水印及Base64图片字符串加水印

    JAVA实现图片加水印及Base64图片字符串加水印 可将根据不同需求选择场景一,场景二来实现家水印操作,个人感觉比我上一遍文章<base64字符串加水印 >实在 场景一: 涉及上送过来的 ...

  4. think php 缩放图片,thinkphp图片裁剪、缩放、加水印方法

    thinkphp图片裁剪.缩放.加水印方法如下: /** * 图像的裁剪.缩放.加水印 * @param string $path 路径 * @param int $width 裁剪的宽度/限制的高度 ...

  5. AS3给图片加水印文字

    <需要flash player 10+支持> 通过FileReference获取选中的图片,调用它的load方法,将图片存入到内存中.使用Loader(flash.display.Load ...

  6. PDF转换图片,图片的切割,图片转换PDF以及PDF加水印等记录贴

    PDF转变为图片: 把图片进行切割: 把图片转变回PDF: 为PDF加水印文字: 为PDF加水印图片. 1,PDF转变为图片 /*** @author dalin*将PDF格式的文件转换成png文件* ...

  7. php加水印功能,PHP图片加水印功能

    本篇文章主要介绍PHP图片加水印功能,感兴趣的朋友参考下,希望对大家有所帮助. 实例代码如下:<?php /** * 功能:给一张图片加上水印效果 * $i 要加水印效果的图片 * $t 水印文 ...

  8. 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化...

    <?php /*[LocoySpider] (C)2005-2010 Lewell Inc.火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加水印 ...

  9. Java实现Excel和word转pdf加水印,复制及可用

    Java实现Excel和word转pdf加水印,复制及可用 时隔大半年,之前朋友,今天突然找我说,他们有个需求要做这东西.想起来之前自己照着官方文档写,也走了不少坑.今天就索性写个,也为了方便以后使用 ...

  10. html图片加文字批量处理,图片批量加水印工具,图片批量添加文字|图片同时添加文字或图片水印...

    一般在网上下载的图片都会自动带有相应网站的文字或是图片水印,虽然可能在使用图片素材的时候,图片上的水印会在一定程度上影响美观,但是水印是对于版权或者是原创的一个保护,图片水印不仅是可以保护别人的原创图 ...

最新文章

  1. vue-cli3 第三版安装搭建项目
  2. Android Studio报错解决:droid.tools.idea.welcome.install.WizardException: SDK tools directory is missing
  3. Objective C 链式调用 1
  4. Python学习札记(十七) 高级特性3 列表生成式
  5. 神经网络中的优化算法总结
  6. 让UpdatePanel支持文件上传(4):数据传输与解析机制
  7. 微服务间保持事务一致性
  8. 演讲(1)--演讲小故事
  9. linux的mysql如何删除用户_linux mysql增加用户,删除用户,以及用户权限
  10. mysql固定某列获取不连续的值_SQL-怎么把一列不规律的值,取出其中连续段的首尾数字?...
  11. vhg电路是什么意思_值得珍藏的经典模拟电路
  12. 将代码提交到码云步骤
  13. python 爬取加密视频,爬虫:解决视频遇到m3u8加密
  14. 我的世界javamod怎么装_我的世界MOD安装方法图文教程_我的世界MOD怎么安装_牛游戏网...
  15. 计算百分比的分析函数
  16. 原来ChatGPT可以充当这么多角色
  17. LiveData实践
  18. 基于SSM实现的艺术品鉴定管理系统+App
  19. Linux I2C 设备注册
  20. c# leetcode1079. 活字印刷(回溯算法)

热门文章

  1. Jackson 泛型序列化
  2. 30个物联网产业动向 芯片商也来抢占IoT
  3. 「微服务网关实战三」详细理解 SCG 路由、断言与过滤器
  4. AAA:认证、审计、授权
  5. 使用GTK+和Glade快速开发Linux图形界面
  6. Nodejs监控Apple召回计划邮件提醒
  7. 一位HRD真实讲述,从大公司到小公司的生存策略
  8. Unity中点击物体后让其消失(注意:要使用hit.collider,而不是this,要时刻注意你要哪一个消失)
  9. python绘制线型图
  10. 从“小时购”看京东即时零售的新野望