java实现在一张大图片上添加小图及文字
短信预约 -IT技能 免费直播动态提醒
在一张大图上添加小图及文字
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:\\Users\\langz\\Desktop\\ffff\\big.jpg";
String smallImg = "C:\\Users\\langz\\Desktop\\ffff\\small.jpg";
String content = "好久不见,你还好吗";
String outPath = "C:\\Users\\langz\\Desktop\\ffff\\" + System.currentTimeMillis() + ".jpg";
try {
bigImgAddSmallImgAndText(bigImg, smallImg, 500, 500, null, 200, 200, outPath);
} catch (IOException e) {
e.printStackTrace();
}
}
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;
public class TextMarkProcessor {
public static void main(String[] args) {
new TextMarkProcessor().testTextMark();
}
public void testTextMark() {
File class="lazy" data-srcImgFile = new File("D:/test/desktop.png");
String logoText = "[ 天使的翅膀 ]";
File outputRotateImageFile = new File("D:/test/desktop_text_mark.jpg");
createWaterMarkByText(class="lazy" data-srcImgFile, logoText, outputRotateImageFile, 0);
}
public void createWaterMarkByText(File class="lazy" data-srcImgFile, String logoText,
File outputImageFile, double degree) {
OutputStream os = null;
try {
Image class="lazy" data-srcImg = ImageIO.read(class="lazy" data-srcImgFile);
BufferedImage buffImg = new BufferedImage(class="lazy" data-srcImg.getWidth(null),
class="lazy" data-srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = buffImg.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics.drawImage(class="lazy" data-srcImg.getScaledInstance(class="lazy" data-srcImg.getWidth(null),
class="lazy" data-srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
if (degree>0) {
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.class="lazy" data-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;
public class PictureMarkProcessor {
public static void main(String[] args) {
new PictureMarkProcessor().testPictureMark();
}
public void testPictureMark() {
File class="lazy" data-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(class="lazy" data-srcImageFile, logoImageFile, outputRoateImageFile, 0);
}
public void createWaterMarkByIcon(File class="lazy" data-srcImageFile, File logoImageFile,
File outputImageFile, double degree) {
OutputStream os = null;
try {
Image class="lazy" data-srcImg = ImageIO.read(class="lazy" data-srcImageFile);
BufferedImage buffImg = new BufferedImage(class="lazy" data-srcImg.getWidth(null),
class="lazy" data-srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = buffImg.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics.drawImage(class="lazy" data-srcImg.getScaledInstance(class="lazy" data-srcImg.getWidth(null),
class="lazy" data-srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));
Image logoImg = logoImgIcon.getImage();
//旋转
if (degree>0) {
graphics.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getWidth() / 2);
}
float alpha = 0.8f; // 透明度
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.class="lazy" data-src_ATOP, alpha));
//水印 的位置
graphics.drawImage(logoImg, buffImg.getWidth()/3, buffImg.getHeight()/2, null);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.class="lazy" data-src_OVER));
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();
}
}
}
}
实现效果如下:
原图:
加文字水印:
加图片水印:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341