package org.jeecg.modules.lampblack.RaLampblack.util;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
/**
14
15 * 该类实现了图片的合并功能,可以选择水平合并或者垂直合并。
16 * 当然此例只是针对两个图片的合并,如果想要实现多个图片的合并,只需要自己实现方法 BufferedImage
17 * mergeImage(BufferedImage[] imgs, boolean isHorizontal)即可;
18 * 而且这个方法更加具有通用性,但是时间原因不实现了,方法和两张图片实现是一样的
19 */
public class ceshi {
/**
24 * @param fileUrl
25 * 文件绝对路径或相对路径
26 * @return 读取到的缓存图像
27 * @throws IOException
28 * 路径错误或者不存在该文件时抛出IO异常
29 */
public static BufferedImage getBufferedImage(String fileUrl)
throws IOException {
File f = new File(fileUrl);
return ImageIO.read(f);
}
/**
38 * 远程图片转BufferedImage
39 * @param destUrl 远程图片地址
40 * @return
41 */
public static BufferedImage getBufferedImageDestUrl(String destUrl) {
HttpURLConnection conn = null;
BufferedImage image = null;
try {
URL url = new URL(destUrl);
conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == 200) {
image = ImageIO.read(conn.getInputStream());
return image;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return image;
}
/**
* 获取文本长度。汉字为1:1,英文和数字为2:1
*/
private static int getTextLength(String text) {
int length = text.length();
for (int i = 0; i < text.length(); i++) {
String sparam = String.valueOf(text.charAt(i));
if (sparam.getBytes().length > 1) {
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
return length;
}
// 水印透明度
private static float alpha = 0.2f;
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText 水印内容
* @param srcImg 源图片
* @param targerPath 生成水印文件路径
* @param degree 倾斜度
*/
public static void imageByText(String logoText, Image srcImg, String targerPath, Integer degree) {
InputStream is = null;
OutputStream os = null;
try {
// 源图片
int heightParam = srcImg.getHeight(null);// 原图高度
int widthParam = srcImg.getWidth(null);// 原图宽度
// 水印文字大小
int fontSize = 120;
// 水印文字字体
Font font = new Font("微软雅黑", Font.BOLD, heightParam/50);
// 水印文字颜色
Color color = Color.gray;
// 水印之间的间隔
int xmove = widthParam/6;
// 水印之间的间隔
int ymove = heightParam/6;
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 (null != degree) {
graphics.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}
// 设置水印文字颜色
graphics.setColor(color);
// 设置水印文字Font
graphics.setFont(font);
// 设置水印文字透明度
//graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int xparam = -widthParam / 2;
int yparam = -heightParam / 2;
int markWidth = fontSize * getTextLength(logoText);// 字体长度
int markHeight = fontSize;// 字体高度
// 循环添加水印
while (xparam < widthParam * 1) {
yparam = -heightParam / 2;
graphics.drawString(logoText, xparam, yparam);
yparam += markHeight + ymove;
}
// 释放资源
graphics.dispose();
// 生成图片
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
System.out.println("添加水印文字成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
61 * 输出图片
62 *
63 * @param buffImg
64 * 图像拼接叠加之后的BufferedImage对象
65 * @param savePath
66 * 图像拼接叠加之后的保存路径
67 */
public static void generateSaveFile(BufferedImage buffImg, String savePath) {
int temp = savePath.lastIndexOf(".") + 1;
try {
File outFile = new File(savePath);
if(!outFile.exists()){
outFile.createNewFile();
}
ImageIO.write(buffImg, savePath.substring(temp), outFile);
System.out.println("ImageIO write...");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
83 *
84 * @Title: 构造图片
85 * @Description: 生成水印并返回java.awt.image.BufferedImage
86 * @param buffImg
87 * 源文件(BufferedImage)
88 * @param waterFile
89 * 水印文件(BufferedImage)
90 * @param x
91 * 距离右下角的X偏移量
92 * @param y
93 * 距离右下角的Y偏移量
94 * @param alpha
95 * 透明度, 选择值从0.0~1.0: 完全透明~完全不透明
96 * @return BufferedImage
97 * @throws IOException
98 */
public static BufferedImage overlyingImage(BufferedImage buffImg, BufferedImage waterImg, int x, int y, float alpha) throws IOException {
// 创建Graphics2D对象,用在底图对象上绘图
Graphics2D g2d = buffImg.createGraphics();
int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
int waterImgHeight = waterImg.getHeight();// 获取层图的高度
// 在图形和图像中实现混合和透明效果
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 绘制
g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
g2d.dispose();// 释放图形上下文使用的系统资源
return buffImg;
}
/**
115 * 待合并的两张图必须满足这样的前提,如果水平方向合并,则高度必须相等;如果是垂直方向合并,宽度必须相等。
116 * mergeImage方法不做判断,自己判断。
117 *
118 * @param img1
119 * 待合并的第一张图
120 * @param img2
121 * 带合并的第二张图
122 * @param isHorizontal
123 * 为true时表示水平方向合并,为false时表示垂直方向合并
124 * @return 返回合并后的BufferedImage对象
125 * @throws IOException
126 */
public static BufferedImage mergeImage(BufferedImage img1,
BufferedImage img2, boolean isHorizontal) throws IOException {
int w1 = img1.getWidth();
int h1 = img1.getHeight();
int w2 = img2.getWidth();
int h2 = img2.getHeight();
// 从图片中读取RGB
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
int[] ImageArrayTwo = new int[w2 * h2];
ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);
// 生成新图片
BufferedImage DestImage = null;
if (isHorizontal) { // 水平方向合并
DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
} else { // 垂直方向合并
DestImage = new BufferedImage(w1, h1 + h2, BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 设置下半部分的RGB
}
return DestImage;
}
/**
156 * Java 测试图片叠加方法
157 */
public static void overlyingImageTest() {
String sourceFilePath = "E:/weixin/1.jpg";
String waterFilePath = "E:/weixin/1.png";
String saveFilePath = "E:/weixin/4.png";
try {
BufferedImage bufferImage1 = getBufferedImage(sourceFilePath);
BufferedImage bufferImage2 = getBufferedImage(waterFilePath);
// 构建叠加层
BufferedImage buffImg = overlyingImage(bufferImage1, bufferImage2, 0, 0, 1.0f);
// 输出水印图片
generateSaveFile(buffImg, saveFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
179 * Java 测试图片合并方法
180 */
public static void imageMargeTest() {
// 读取待合并的文件
BufferedImage bi1 = null;
BufferedImage bi2 = null;
// 调用mergeImage方法获得合并后的图像
BufferedImage destImg = null;
System.out.println("下面是垂直合并的情况:");
String saveFilePath = "D://test//new1.jpg";
String divingPath = "D://test//new2.jpg";
String margeImagePath = "D://test//margeNew.jpg";
try {
bi1 = getBufferedImage(saveFilePath);
bi2 = getBufferedImage(divingPath);
// 调用mergeImage方法获得合并后的图像
destImg = mergeImage(bi1, bi2, false);
} catch (IOException e) {
e.printStackTrace();
}
// 保存图像
generateSaveFile(destImg, margeImagePath);
System.out.println("垂直合并完毕!");
}
/**
179 * Java 测试图片合并方法
180 */
public static void imageMargeTest1() {
try {
BufferedImage small1 = ImageIO.read(new File("E:/weixin/r.jpg"));
imageByText("001",small1,"E:/weixin/r1.jpg",0);
} catch (IOException e) {
e.printStackTrace();
}
// 保存图像
System.out.println("垂直合并完毕!");
}
public static void main(String[] args) {
// 测试图片的叠加
//overlyingImageTest();
try {
BufferedImage big = ImageIO.read(new File("E:/weixin/3.png"));
BufferedImage small = ImageIO.read(new File("E:/weixin/1.png"));
Graphics2D g = big.createGraphics();
int x = 105;
int y = 106 ;
g.drawImage(small, x, y, 331, 331, null);
g.dispose();
ImageIO.write(big, "jpg", new File("E:/weixin/r.jpg"));
imageMargeTest1();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("-------------------成功-------------------");
}
// 测试图片的垂直合并
// imageMargeTest();
}
用java实现图片合并与折叠
最新推荐文章于 2024-04-07 11:33:04 发布