java写的水印字和水印图
- package image;
- import java.io.IOException;
- import java.awt.Graphics;
- import java.awt.*;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.awt.image.ImageProducer;
- import java.io.File;
- import java.io.FileOutputStream;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- import com.sun.jimi.core.Jimi;
- import com.sun.jimi.core.JimiException;
- import com.sun.jimi.core.JimiWriter;
- import com.sun.jimi.core.options.JPGOptions;
- /**
- *
- * @author myjava_024
- * java做水印,字体和图片,调节透明度
- */
- public class ImageOperation {
- /**
- * 将任何格式的图片转换成JPG图片 source原图片路径 dest 转换后图片的路径
- */
- public void toReduce(String source, String dest)
- {
- try {
- JPGOptions options = new JPGOptions();
- options.setQuality(100);
- // 图片格式在jimi的解码格式范围内
- ImageProducer image = Jimi.getImageProducer(source);
- // 图片编码
- JimiWriter writer = Jimi.createJimiWriter(dest);
- writer.setSource(image);
- // 对输出文件的属性进行相关的设置,每种格式的属性都不一样
- writer.setOptions(options);
- writer.putImage(dest);
- } catch (JimiException je) {
- System.err.println("Error: " + je);
- je.printStackTrace();
- }
- }
- /**
- * 实现对图片的抽取缩略并控制生成图的大小
- * oldfile原图片路径 newfile缩略后的 图片路径 wideths 缩略后图片的宽 heights缩略后图片的高
- */
- public void changDimension(String oldfile, String newfile, int wideths,
- int heights) throws JimiException, IOException
- {
- String temp = newfile;
- File inputfile = null;
- this.toReduce(oldfile, temp);
- // 读入文件
- inputfile = new File(temp);
- // 构造Image对象
- Image src = ImageIO.read(inputfile);
- // double wideth = (double) src.getWidth(null); // 得到源图宽
- // double height = (double) src.getHeight(null); // 得到源图长
- int iWideth = wideths;
- int iHeight = heights;
- BufferedImage tag = null;
- try {
- tag = new BufferedImage(iWideth, iHeight,
- BufferedImage.TYPE_INT_RGB);
- // 绘制缩小后的图
- tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null);
- JimiWriter writer = Jimi.createJimiWriter(newfile);
- writer.setSource(tag);
- writer.putImage(newfile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 给图片添加文字 oldfile newfile就不用多说了 text 就是给图片加的文字
- */
- public void Imagefont(String oldfile, String newfile, String text)
- {
- Image img;
- String filename = oldfile.substring(oldfile.lastIndexOf(".") + 1);
- String temp = newfile;
- try {
- if (!filename.equals("jpg")) {
- this.toReduce(oldfile, newfile);
- img = ImageIO.read(new File(temp));
- } else {
- img = ImageIO.read(new File(oldfile));
- }
- int width = img.getWidth(null);
- int height = img.getHeight(null);
- BufferedImage image = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = image.createGraphics();
- g.drawImage(img, 0, 0, width, height, null);
- g.setColor(Color.red);
- g.setFont(new Font("Courier", Font.HANGING_BASELINE, 40));
- g.drawString(text, width - 360, height - 72);
- g.dispose();
- FileOutputStream os = new FileOutputStream(temp);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
- encoder.encode(image);
- } catch (Exception e) {
- e.getMessage();
- }
- }
- /**
- * 给图片添加水印,并控制水印图片的位置及透明度
- * oldfile newfile我也不说了 syoldfile 要添加的水印图片的路径 t 水印图品添加的位置 to 水印图片的透明度
- */
- public void Imagese(String oldfile, String newfile, String syoldfile,
- int t, float to)
- {
- //文件类型
- String filename = oldfile.substring(oldfile.lastIndexOf(".") + 1);
- System.out.println(filename);
- Image img;
- String temp = newfile;
- float alpha = to;
- try {
- if (!filename.equals("jpg")) {
- toReduce(oldfile, newfile);
- img = ImageIO.read(new File(temp));
- System.out.println("其他类型图片创建生成水印的图片");
- } else {
- img = ImageIO.read(new File(oldfile));
- }
- int width = img.getWidth(null);
- int height = img.getHeight(null);
- int ws = width / 3;
- int hs = height / 3;
- BufferedImage image = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics2D g = image.createGraphics();
- g.drawImage(img, 0, 0, width, height, null);
- Image logo = ImageIO.read(new File(syoldfile));
- int lw = logo.getWidth(null);
- int lh = logo.getHeight(null);
- //设置图片透明度
- g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
- alpha));
- g.drawImage(logo, null, null);
- switch (t) {
- case 1:
- g.drawImage(logo, 0, 0, lw, lh, null);
- System.out.println("布局一");
- break;
- case 2:
- g.drawImage(logo, ws, 0, lw, lh, null);
- System.out.println("布局二");
- break;
- case 3:
- g.drawImage(logo, 2 * ws, 0, lw, lh, null);
- System.out.println("布局三");
- break;
- case 4:
- g.drawImage(logo, 0, hs, lw, lh, null);
- System.out.println("布局四");
- break;
- case 5:
- g.drawImage(logo, ws, hs, lw, lh, null);
- System.out.println("布局五");
- break;
- case 6:
- g.drawImage(logo, 2 * ws, hs, lw, lh, null);
- System.out.println("布局六");
- break;
- case 7:
- g.drawImage(logo, 0, 2 * hs, lw, lh, null);
- System.out.println("布局七");
- break;
- case 8:
- g.drawImage(logo, ws, 2 * hs, lw, lh, null);
- System.out.println("布局八");
- break;
- case 9:
- g.drawImage(logo, 2 * ws, 2 * hs, lw, lh, null);
- System.out.println("布局九");
- break;
- default:
- break;
- }
- g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
- g.dispose();
- //生成图片
- FileOutputStream oss = new FileOutputStream(temp);
- JPEGImageEncoder encoders = JPEGCodec.createJPEGEncoder(oss);
- encoders.encode(image);
- } catch (Exception e) {
- e.getMessage();
- e.printStackTrace();
- }
- }
- public static void main(String [] args)
- {
- ImageOperation test = new ImageOperation();
- //alpha表示透明度,alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
- float alpha = 0.2f;
- //生成水印图
- test.Imagese("d:/001.svg", "d:/003.svg", "d:/002.svg", 1, alpha);
- /*test.Imagese("d:/001.jpg", "d:/aa/001.jpg", "d:/002.jpg", 1, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/002.jpg", "d:/002.jpg", 2, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/003.jpg", "d:/002.jpg", 3, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/004.jpg", "d:/002.jpg", 4, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/005.jpg", "d:/002.jpg", 5, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/006.jpg", "d:/002.jpg", 6, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/007.jpg", "d:/002.jpg", 7, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/008.jpg", "d:/002.jpg", 8, alpha);
- test.Imagese("d:/001.jpg", "d:/aa/009.jpg", "d:/002.jpg", 9, alpha); */
- }
- }
这里面需要使用个JimiProClasses.jar这个包,我把包传资源上了