⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfimgcreateutil.java

📁 可以实现在图片上面打印文字
💻 JAVA
字号:
package com.cn.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class PDFIMGCreateUtil {
	public static void main(String[] a) {
		// 参数分别是,原文件路径;输出路径;打印文字;字色;量化表[也就是压缩率,0.0为高压缩,图片不清晰,1.0为最低压缩,图片清晰]; 字体;
		// 字大小; 字型[0为普通字型,1为粗字型,2为斜体字型,3为粗斜体字型,4为粗下划线字型,5为斜下划线字型,6为粗斜下划线字型,7为下划线字型];字距;坐标x;坐标y)
//		for(int i=0;i<10;i=i+2){
			PDFIMGCreateUtil.fontMark("d:\\"+0+".jpg", "d:\\"+1+".jpg", "abcd你好",
				new Color(255, 0, 100), 1, "宋体", 96, 6, 1000, 10, 10); 
//		}
		// System.out.println(d.createMark("e8.jpg","e81.jpg","",null,
		// 1,"",16));
	}

	public static boolean fontMark(String filePath, String filePath1,
			String markContent, Color markContentColor, float qualNum,
			String fontType, int fontSize, int shape, int distance, float x, float y) {
//		System.out.println(markContentColor.toString()); 
		ImageIcon imgIcon = new ImageIcon(filePath);
		Image theImg = imgIcon.getImage();
		// Image可以获得 输入图片的信息

		int width = theImg.getWidth(null);
		int height = theImg.getHeight(null);

		// 800 800 为画出图片的大小
		BufferedImage bimage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 2d 画笔
		Graphics2D g = bimage.createGraphics();
		g.setColor(markContentColor);
		g.setBackground(Color.white);

		// 画出图片-----------------------------------
		g.drawImage(theImg, 0, 0, null);
		// 画出图片-----------------------------------

		// --------对要显示的文字进行处理--------------
		AttributedString ats = new AttributedString(markContent);
		// 三个参数分别为,字体,字型(Font.PLAIN为普通样式,Font.BOLD为粗体,Font.ITALIC为斜体,Font.BOLD|Font.ITALIC为粗斜体),字大小
		Font f = null;

		// 添加字距
		ats.addAttribute(TextAttribute.KERNING,TextAttribute.KERNING_ON+25);
		// 0为普通字型,1为粗字型,2为斜体字型,3为粗斜体字型,4为粗下划线字型,5为斜下划线字型,6为粗斜下划线字型,7为下划线字型
		switch (shape) {
		case 0:
			f = new Font(fontType, Font.PLAIN, fontSize);
			break;
		case 1:
			f = new Font(fontType, Font.BOLD, fontSize);
			break;
		case 2:
			f = new Font(fontType, Font.ITALIC, fontSize);
			break;
		case 3:
			f = new Font(fontType, Font.BOLD | Font.ITALIC, fontSize);
			break;
		case 4:
			f = new Font(fontType, Font.BOLD, fontSize);
			ats.addAttribute(TextAttribute.UNDERLINE,
					TextAttribute.UNDERLINE_ON, 0, markContent.length());
			break;
		case 5:
			f = new Font(fontType, Font.ITALIC, fontSize);
			ats.addAttribute(TextAttribute.UNDERLINE,
					TextAttribute.UNDERLINE_LOW_TWO_PIXEL, 0, markContent
							.length());
			break;
		case 6:
			f = new Font(fontType, Font.BOLD | Font.ITALIC, fontSize);
			ats.addAttribute(TextAttribute.UNDERLINE,
					TextAttribute.UNDERLINE_ON, 0, markContent.length());
			break;
		case 7:
			f = new Font(fontType, Font.PLAIN, fontSize);
			ats.addAttribute(TextAttribute.UNDERLINE,
					TextAttribute.UNDERLINE_ON, 0, markContent.length());
		default:
			f = new Font(fontType, Font.PLAIN, fontSize);
			break;
		}

		ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());

		AttributedCharacterIterator iter = ats.getIterator();
		// ----------------------

		g.drawString(iter, x, y+fontSize);
		// 添加水印的文字和设置水印文字出现的内容 ----位置(坐标)
		g.dispose();// 画笔结束
		try {
			// 输出 文件 到指定的路径
			FileOutputStream out = new FileOutputStream(filePath1);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);

			param.setQuality(qualNum, true);
			encoder.encode(bimage, param);
			out.close();
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -