本文主要是介绍图片添加水印及打印文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给图片添加水印,打印文件。
java相关操作:
1. 图片操作;(参考:http://uule.iteye.com/blog/2278169)
2. 打印机操作。(参考:http://tiansoft.iteye.com/blog/1120161,http://blog.csdn.net/alex__0805/article/details/47293805)
相关代码:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.OrientationRequested;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.ParseException;/*** Created by LH on 2017/2/24.*/
public class PicStrMaker {public static void main(String[] args) throws ParseException {PicStrMaker picStrMaker = new PicStrMaker();picStrMaker.doTemplate1("E:\\back.jpg", "E:\\2.jpg", "大家好:", "frcoder");picStrMaker.printImage("E:\\2.jpg", false);}/*** 样板1** @param inImgPath 底片路径* @param outImgPath 输出路径* @param title 题目* @param signature 署名*/public void doTemplate1(String inImgPath, String outImgPath, String title, String signature) {try {System.out.print("Drawing string in image......" + outImgPath);openPic(inImgPath);drawString(title, 50, 130, new GraphicsProperty());drawString(signature, 800, 550, new GraphicsProperty());savePic(outImgPath);System.out.println(" --> Done.");} catch (IOException e) {e.printStackTrace();}}/*** 画字实现*/BufferedImage buffImg;public void openPic(String inImgPath) throws IOException {// 获取主图片的路径InputStream is = new FileInputStream(inImgPath);// 通过JPEG图象流创建JPEG数据流解码器JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);// 解码当前JPEG数据流,返回BufferedImage对象buffImg = jpegDecoder.decodeAsBufferedImage();is.close();}public void drawString(String content, int x, int y, GraphicsProperty gp) {// 得到画笔对象Graphics g = buffImg.getGraphics();// 设置颜色g.setColor(gp.color);// 设置字体g.setFont(new Font(gp.fontName, gp.fontStyle, gp.fontSize));// 设置内容和位置g.drawString(content, x, y);g.dispose();}public void savePic(String outImgPath) throws IOException {// 设置保存图片OutputStream os = new FileOutputStream(outImgPath);// 创建编码器,用于编码内存中的图象数据。JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);en.encode(buffImg);os.close();}class GraphicsProperty {Color color;String fontName;int fontStyle;int fontSize;public GraphicsProperty() {this(Color.black, "楷体", Font.BOLD, 50);}public GraphicsProperty(Color color, String fontName, int fontStyle, int fontSize) {this.color = color;this.fontName = fontName;this.fontStyle = fontStyle;this.fontSize = fontSize;}}/*** 打印实现*/public void printImage(String fileName, boolean dialogFlag) {try {// 字节流获取图片信息FileInputStream fin = new FileInputStream(fileName);DocFlavor dof = null;// 根据用户选择不同的图片格式获得不同的打印设备if (fileName.endsWith(".gif")) {// gifdof = DocFlavor.INPUT_STREAM.GIF;} else if (fileName.endsWith(".jpg")) {// jpgdof = DocFlavor.INPUT_STREAM.JPEG;} else if (fileName.endsWith(".png")) {// pngdof = DocFlavor.INPUT_STREAM.PNG;}// 设置打印内容Doc doc = new SimpleDoc(fin, dof, null);// 获得默认打印设备,字节流方式,图片格式PrintService ps = PrintServiceLookup.lookupDefaultPrintService();// 如果没有获取打印机if (ps == null) return;// 终止程序System.out.print("Printing image (" + ps + ")......" + fileName);// 获得打印属性PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();// 每一次默认打印一页pras.add(new Copies(1));// 设置打印方向(横向)pras.add(OrientationRequested.LANDSCAPE);if (dialogFlag) {// 出现打印对话框// 出现设置对话框PrintService service = ServiceUI.printDialog(null, 50, 50, new PrintService[]{ps}, ps, dof, pras);if (service != null) {// 获得打印工作DocPrintJob job = service.createPrintJob();// 开始打印job.print(doc, pras);System.out.println(" --> Done.");} else {System.out.println(" --> Cancel.");}} else {// 不出现打印对话框// 获得打印工作DocPrintJob job = ps.createPrintJob();// 开始打印job.print(doc, pras);System.out.println(" --> Done.");}fin.close();} catch (IOException ie) {// 捕获io异常ie.printStackTrace();} catch (PrintException pe) {// 捕获打印异常pe.printStackTrace();}}
}
这篇关于图片添加水印及打印文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!