资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
java实现在张图上添加图及字实现在张图上添加图及字录录在张图上添加图及字实现给图添加印1. 添加字印2. 添加图印在张图上添加图及字在张图上添加图及字import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;import com.sun.image.codec.jpeg.JPEGImageEncoder;import javax.swing.*;import java.awt.*;import java.awt.image.BufferedImage;import java.io.*;import java.text.ParseException;public class ImgUtil public static void main(String args) throws ParseException String bigImg = C:UserslangzDesktopffffbig.jpg; String smallImg = C:UserslangzDesktopffffsmall.jpg; String content = 好久不见,你还好吗; String outPath = C:UserslangzDesktopffff + System.currentTimeMillis() + .jpg; try bigImgAddSmallImgAndText(bigImg, smallImg, 500, 500, null, 200, 200, outPath); catch (IOException e) e.printStackTrace(); /* * 在张图张添加图和字 * param bigImgPath 图的路径 * param smallImgPath 图的路径 * param sx 图在图上x抽位置 * param sy 图在图上y抽位置 * param content 字内容 * param cx 字在图上y抽位置 * param cy 字在图上y抽位置 * param outPathWithFileName 结果输出路径 */ public static void bigImgAddSmallImgAndText(String bigImgPath , String smallImgPath, int sx, int sy , String content, int cx, int cy , String outPathWithFileName) throws IOException /主图的路径 InputStream is = new FileInputStream(bigImgPath); /通过JPEG图象流创建JPEG数据流解码器 JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is); /解码当前JPEG数据流,返回BufferedImage对象 BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage(); /得到画笔对象 Graphics g = buffImg.getGraphics(); /图的路径 ImageIcon imgIcon = new ImageIcon(smallImgPath); /得到Image对象。 Image img = imgIcon.getImage(); /将图绘到图上,5,300 .表你的图在图上的位置。 g.drawImage(img, sx, sy, null); /设置颜。 g.setColor(Color.WHITE); /最后个参数来设置字体的 if (content != null) Font f = new Font(宋体, Font.PLAIN, 25); Color mycolor = Color.red;/new Color(0, 0, 255); g.setColor(mycolor); g.setFont(f); g.drawString(content, cx, cy); /表这段字在图上的位置(cx,cy) .第个是你设置的内容。 g.dispose(); OutputStream os = new FileOutputStream(outPathWithFileName); /创键编码器,于编码内存中的图象数据。 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); en.encode(buffImg); is.close(); os.close(); 实现给图添加印实现给图添加印某些应场景下需要对图加上印防盗,例如微博户图。Java中实现添加印需要到BufferedImage、Graphics2D 和ImageIO类。1. 添加字印添加字印import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import javax.imageio.ImageIO;/* * 添加字印 * * author Ricky Fung */public class TextMarkProcessor /* * param args */ public static void main(String args) new TextMarkProcessor().testTextMark(); public void testTextMark() File srcImgFile = new File(D:/test/desktop.png); String logoText = 天使的翅膀 ; File outputRotateImageFile = new File(D:/test/desktop_text_mark.jpg); createWaterMarkByText(srcImgFile, logoText, outputRotateImageFile, 0); public void createWaterMarkByText(File srcImgFile, String logoText, File outputImageFile, double degree) OutputStream os = null; try Image srcImg = ImageIO.read(srcImgFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = buffImg.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); if (degree0) graphics.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); graphics.setColor(Color.RED); graphics.setFont(new Font(宋体, Font.BOLD, 36); float alpha = 0.8f; graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha); graphics.drawString(logoText, buffImg.getWidth()/3, buffImg.getHeight()/2); graphics.dispose(); os = new FileOutputStream(outputImageFile); / 成图 ImageIO.write(buffImg, JPG, os); catch (Exception e) e.printStackTrace(); finally try if (null != os) os.close(); catch (Exception e) e.printStackTrace(); 2. 添加图印添加图印import java.awt.AlphaComposite;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.swing.ImageIcon;/* * 添加图印 * * author Ricky Fung */public class PictureMarkProcessor /* * param args */ public static void main(String args) new PictureMarkProcessor().testPictureMark(); public void testPictureMark() File srcImageFile = new File(D:/test/desktop.png); File logoImageFile = new File(D:/test/tools.png); File outputRoateImageFile = new File(D:/test/desktop_pic_mark.jpg); createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile, 0); public void createWaterMarkByIcon(File srcImageFile, File logoImageFile, File outputImageFile, double degree) OutputStream os = null; try Image srcImg = ImageIO.read(srcImageFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), B
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号