/**
* @Description: 将base64编码字符串转换为图片
* @param imgStr
* base64编码字符串
* @param path
* 图片路径-具体到文件
* @return
*/
public static boolean generateImage(String imgStr, String path) {
if (StringUtils.isBlank(imgStr)) {
return false;
}
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(path);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
} /**
* @Description: 根据图片地址转换为base64编码字符串
* @return
*/
public static String getImageStr(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}

Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类。这个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。但是在Eclipse和MyEclipse中直接使用,却找不到该类。解决方法如下:

1.右键项目--》Build Path --》Configure Build Path

选择Libraries,点击JRE System Library,选择 Access rules,如果之前没有定义规则,会显示No rules defined

2. Access rules,点击Edit --》Add,然后点击Ok

3.在Resolution下拉列表框中选择Accessible,Rule Pattern 选择**,依次点击ok

/**
* @Description:图片拼接 (注意:必须两张图片长宽一致哦)
* @param files
* 要拼接的文件列表
* @param type
* 1横向拼接, 2 纵向拼接
*/
public static boolean mergeImage(String[] files, int type, String targetFile) {
int len = files.length;
if (len < 1) {
throw new RuntimeException("图片数量小于1");
}
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
src[i] = new File(files[i]);
images[i] = ImageIO.read(src[i]);
} catch (Exception e) {
throw new RuntimeException(e);
}
int width = images[i].getWidth();
int height = images[i].getHeight();
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}
int newHeight = 0;
int newWidth = 0;
for (int i = 0; i < images.length; i++) {
// 横向
if (type == 1) {
newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
newWidth += images[i].getWidth();
} else if (type == 2) {// 纵向
newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
newHeight += images[i].getHeight();
}
}
if (type == 1 && newWidth < 1) {
return false;
}
if (type == 2 && newHeight < 1) {
return false;
} // 生成新图片
try {
BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
int width_i = 0;
for (int i = 0; i < images.length; i++) {
if (type == 1) {
ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
images[i].getWidth());
width_i += images[i].getWidth();
} else if (type == 2) {
ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
height_i += images[i].getHeight();
}
}
// 输出想要的图片
ImageIO.write(ImageNew, targetFile.split("\\.")[1], new File(targetFile));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* @param srcImgPath
* 源图片路径
* @param tarImgPath
* 保存的图片路径
* @param waterMarkContent
* 水印内容
* @param markContentColor
* 水印颜色
* @param font
* 水印字体
*/
public boolean addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent, Color markContentColor,
Font font, int degree) {
try {
// 读取原图片信息
File srcImgFile = new File(srcImgPath);// 得到文件
Image srcImg = ImageIO.read(srcImgFile);// 文件转化为图片
int srcImgWidth = srcImg.getWidth(null);// 获取图片的宽
int srcImgHeight = srcImg.getHeight(null);// 获取图片的高
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
g.setColor(markContentColor); // 根据图片的背景设置水印颜色
g.setFont(font); // 设置字体
// 设置水印的坐标
int x = srcImgWidth - 2 * getWatermarkLength(waterMarkContent, g);
int y = srcImgHeight - 2 * getWatermarkLength(waterMarkContent, g);
g.rotate(Math.toRadians(degree), (double) srcImgWidth / 2, (double) srcImgHeight / 2);
float high = (float) (1648 / 3.7);
for (int i = 0; i < 9; i++) {
if (i % 2 == 0) {
g.drawString(waterMarkContent, 0.0f - 500, i * high); // 画出水印
} else {
g.drawString(waterMarkContent, 0.0f, i * high); // 画出水印
}
}
g.dispose();
// 输出图片
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, "jpg", outImgStream);
System.out.println("添加水印完成");
outImgStream.flush();
outImgStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
} public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
} public static void main(String[] args) {
Font font = new Font("微软雅黑", Font.BOLD, 150); // 水印字体
String srcImgPath = "d:/demo.jpg"; // 源图片地址
String tarImgPath = "d:/test1.jpg"; // 待存储的地址
String waterMarkContent = "本照片仅供行邮商品报关使用 本照片仅供行邮商品报关使用"; // 水印内容
Color color = new Color(0, 0, 0, 50); // 水印图片色彩以及透明度
addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color, font, 330);
}

base64转图片、图片转base64、图片拼接、加水印(水印角度可设置)的更多相关文章

  1. 浅析用Base64编码的图片优化网页加载速度

    想必大家都知道网页加载的过程,从开始请求,到加载页面,开始解析和显示网页,遇到图片就再次向服务器发送请求,加载图片.如果图片很多的话,就会产生大量的http请求,从而影响页面的加载速度.所以现在有一种 ...

  2. Android开发 - ImageView加载Base64编码的图片

    在我们开发应用的过程中,并不是所有情况下都请求图片的URL或者加载本地图片,有时我们需要加载Base64编码的图片.这种情况出现在服务端需要动态生成的图片,比如: 二维码 图形验证码 ... 这些应用 ...

  3. 图片处理之 Base64

    网页上的图片资源如果采用 http 形式的 url 的话都会额外发送一次请求,网页发送的 http 请求次数越多,会造成页面加载速度越慢.而采用Base64格式的编码,将图片转化为字符串后,图片文件会 ...

  4. BASE64编码的图片在网页中的显示问题的解决

    BASE64位转码有两种: 一种是图片转为Base64编码,这种编码是直接可以在页面通过<img src='base64编码'/>的方式显示 Base64 在CSS中的使用 .demoIm ...

  5. LODOP直接用base64码输出图片

    Lodop中的ADD_PRINT_IMAGE,也可以直接输出base64码图片,不用加img标签,如果加了img标签,会被当做超文本对待,受浏览器引擎解析的影响. 什么时候使用base64码直接输出比 ...

  6. base64转换成图片

    前端代码JS: 前端图片为canvsa绘图转base64格式 function putTextInfo() { var canvasImg = painting.canvas.toDataURL('i ...

  7. EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象

    EF+LINQ事物处理   在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...

  8. Base64图片编码原理,base64图片工具介绍,图片在线转换Base64

    Base64图片编码原理,base64图片工具介绍,图片在线转换Base64 DataURI 允许在HTML文档中嵌入小文件,可以使用 img 标签或 CSS 嵌入转换后的 Base64 编码,减少  ...

  9. webpack打包小图片时进行Base64转码

    关于base64 优点: base64就是一串字符串码表示的图片,在加载页面和js时一块加载出来,减少了加载图片时的http请求.加载一张图片时会发起一次http请求,http请求每次建立都会需要一定 ...

随机推荐

  1. 使用websocketpp进行websocket通信

    websocketpp介绍 websocketpp是一个只有头文件的支持websocket协议的C++开源库,支持websocket客户端和服务器功能,网络传输模块基于boost::asio 提供 s ...

  2. 【algo&ds】2.线性表

    1.线性表 线性表(英语:Linear List)是由n(n≥0)个数据元素(结点)a[0],a[1],a[2]-,a[n-1]组成的有限序列. 其中: 数据元素的个数n定义为表的长度 = " ...

  3. 读《MySQL必知必会》我学到了什么?

    前言 最近在写项目的时候发现自己的SQL基本功有些薄弱,遂上知乎查询MYSQL关键字,期望得到某些高赞答案的指点,于是乎发现了 https://www.zhihu.com/question/34840 ...

  4. count的一些用法

    count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL  count(1)包括了所有列,用1代表代码行,在统计结果的时候,不会忽略列值为NULL  count(列名)只包 ...

  5. asp.net core 自定义 Policy 替换 AllowAnonymous 的行为

    asp.net core 自定义 Policy 替换 AllowAnonymous 的行为 Intro 最近对我们的服务进行了改造,原本内部服务在内部可以匿名调用,现在增加了限制,通过 identit ...

  6. nyoj 74-小学生算术(进位问题)

    74-小学生算术 内存限制:64MB 时间限制:3000ms 特判: No 通过数:23 提交数:53 难度:1 题目描述: 很多小学生在学习加法时,发现“进位”特别容易出错.你的任务是计算两个三位数 ...

  7. 力扣(LeetCode)买卖股票的最佳时机 个人题解

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  8. root权限后,不要忘了还有selinux

    下面的例子运行在中兴android 5.0手机上. 当我们使用root权限的python去创建socket监听端口8088时,selinux向kmsg输出了下面的记录 python-android5 ...

  9. leetcode-242 判断两个字符串是不是 Anagram ?

    题目描述 假设给定两个字符串 s 和 t, 让我们写出一个方法来判断这两个字符串是否是字母异位词? 字母异位词就是,两个字符串中含有字母的个数和数量都一样,比如: Example 1: Input: ...

  10. 建筑行业的新起之秀---BIM

       近年来,BIM在国家在建筑行业的推进下逐渐走近人们的视线,而且BIM技术是作为建筑领域的一项新技术行业发展的越来越好,在很多的建筑场景都用到了BIM建模.施工.运维以及BIM+GIS等以BIM为 ...