图片处理(水印,Base64转图片)

Base64格式转图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42


public static boolean Base64ToImage(String imgStr) throws WxException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String string = sdf.format(date);
//图片存放路径
String filePath = "D:" + File.separator + string;
File file = new File(filePath);
//判断文件是否存在
if (!file.exists()) {
file.mkdir();
}
//图片名称
Long lg = System.currentTimeMillis();
String imageName = lg + ".jpg";
//图片存放路径
String imagePath = filePath + File.separator + imageName;
if (imgStr == null) {
throw new WxException("图片base64编码数据为空");
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(imagePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
throw new WxException("图片base64格式转换图片失败。");
}
}


图片增加图片水印处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78


public static String watermark(File image) throws Exception {
// String logoFileName = "logo_"+image.getOriginalFilename();
OutputStream os = null;

try {
Image image2 = ImageIO.read(image);
int width = image2.getWidth(null);
int height = image2.getHeight(null);

// 1、创建缓存图片对象
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 2、 创建JAVA绘图工具对象
Graphics2D graphics2D = bufferedImage.createGraphics();

// 3、使用绘图工具对象,将原图绘制到缓存图片对象
graphics2D.drawImage(image2, 0, 0, width, height, null);

// 4、使用绘图工具,将水印(文字/图片)绘制到缓存图片对象Logo为水印图片

File logo = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "static/shuiyin.png");

// File logo = new File(logoPath);
Image imageLogo = ImageIO.read(logo);
int width1 = imageLogo.getWidth(null);
int height1 = imageLogo.getHeight(null);
////设置水印 ALPHA大小
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3F));
graphics2D.rotate(Math.toRadians(30), bufferedImage.getHeight() / 2, bufferedImage.getHeight() / 2);//图层以中心为基点,转30度的弧度。
int x = -width / 2;
int y = -height / 2;
while (x < width * 1.5) {
y = -height / 2;
while (y < height * 1.5) {
graphics2D.drawImage(imageLogo, x, y, null);
//MARGIN_ALL 边缘参数
y += height1 + 50;
}
x += width1 + 50;
}
graphics2D.dispose();
// 5、 创建图像编码工具类 指定地址
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String string = sdf.format(date);

// realUploadPath + File.separator + image.getName();
String des = "D:" + File.separator + "workCatalog" + File.separator + string + File.separator;
File file = new File(des);
if (!file.exists()) {
file.mkdirs();
}
String watermarkPaht = "D:" + File.separator + "workCatalog" + File.separator + string + File.separator + image.getName();
//C:\Users\Tanghaiwen\AppData\Local\Temp\tomcat-docbase.4840919478909796944.8083\images\logo_1.jpg
os = new FileOutputStream(watermarkPaht);
JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(os);
// 6、使用图片编码工具类,输出缓存图像到模板图片文件
jpegImageEncoder.encode(bufferedImage);
return des;
} catch (Exception e) {
e.printStackTrace();
throw new WxException("图片水印处理失败");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
throw new WxException("图片水印处理失败");
}
}
}

}


图片增加文字水印处理

注:和图片文印一样,文字水印未经测试(若有问题就用上面的走一遍即可)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77


public String watermark(File image) {
OutputStream os = null;

try {
Image image2 = ImageIO.read(image);
int width = image2.getWidth(null);
int height = image2.getHeight(null);

// 1、创建缓存图片对象
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 2、 创建JAVA绘图工具对象
Graphics2D graphics2D = bufferedImage.createGraphics();

// 3、使用绘图工具对象,将原图绘制到缓存图片对象
graphics2D.drawImage(image2, 0, 0, width, height, null);

// 4、使用绘图工具,将水印(文字/图片)绘制到缓存图片对象
graphics2D.setFont(new Font("微软雅黑", Font.BOLD, 30));
graphics2D.setColor(Color.RED);

//S1-获取文字水印的宽、高
int width1 = 30 * getTextLength("水印文字");
int height1 = 30;

int widthDiff = width - width1;
int heightDiff = height - height1;

int x = 10;
int y = 10;
if (x > widthDiff) {
x = widthDiff;
}
if (y > heightDiff) {
y = heightDiff;
}
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3F));//设置水印
graphics2D.drawString("水印文字", x, y + 30);//x:横坐标,y:纵坐标
graphics2D.dispose();

// 5、 创建图像编码工具类
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String string = sdf.format(date);

// realUploadPath + File.separator + image.getName();
String des = "D:" + File.separator + "workCatalog" + File.separator + string + File.separator;
File file = new File(des);
if (!file.exists()) {
file.mkdirs();
}
String watermarkPaht = "D:" + File.separator + "workCatalog" + File.separator + string + File.separator + image.getName();
os = new FileOutputStream(des);

JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(os);

// 6、使用图片编码工具类,输出缓存图像到模板图片文件
jpegImageEncoder.encode(bufferedImage);
return des;
} catch (Exception e) {
e.printStackTrace();
throw new WxException("图片水印处理失败");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
throw new WxException("图片水印处理失败");
}
}
}
}