base64编码操作图片
package com.trsmedia.service; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.util.Base64.Encoder;
import java.util.Base64;
import java.util.Base64.Decoder; public class Image_base64_test {
public static void main(String[] args) {
//这一步很重要很重要很重要,因为base64的数据会有data:base64img,
String str = "iVBORw0KGgoAAAANSUhEUgAAAXcAAAGQCAMAAABiX9D2AAAAAXNSR0IB2cksfwAAAchQTFRFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAsLcCbwAAAJh0Uk5TACBfj69/P3D0/8um9jBQ6P2K0u3a2E/U/t4Qvu7P+3fIbvUf7PdYV/gtsOr5a3X8gdw6aYxsw+A7PPLNVObpv4nF1VN+qM68ve+R25vx5/rwh8LHLo2hwNaLlLlg65yj0K6VkvOX3Z/EhmLi2Y7TkK22iIDB0cxH5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GSbF5od2uyAznR6EYSgOiQE83/2t2YLHE52OO3uykAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8fk/Tmi9wqi3qsEAAAAASUVORK5CYII=";
generateImage(str,"F:\\tg\\tupian.png"); }
/**
* @param imgStr base64编码字符串
* @param path 图片路径-具体到文件
*/
public static boolean generateImage(String imgStr, String path) {
if (imgStr == null)
return false;
Decoder decoder = Base64.getDecoder();
try {
// 解密
byte[] b = decoder.decode(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) { System.out.println(e);
return false;
}
}
//图片转化成base64字符串
public static String GetImageStr()
{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "F:\\tupian\\a.jpg";//待处理的图片
// 地址也有写成"F:/deskBG/86619-107.jpg"形式的
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(data);//返回Base64编码过的字节数组字符串
} }
base64编码操作图片的更多相关文章
- 浅析用Base64编码的图片优化网页加载速度
想必大家都知道网页加载的过程,从开始请求,到加载页面,开始解析和显示网页,遇到图片就再次向服务器发送请求,加载图片.如果图片很多的话,就会产生大量的http请求,从而影响页面的加载速度.所以现在有一种 ...
- Java Base64编码与图片互转
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- Base64编码与图片互转
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- BASE64编码的图片在网页中的显示问题的解决
BASE64位转码有两种: 一种是图片转为Base64编码,这种编码是直接可以在页面通过<img src='base64编码'/>的方式显示 Base64 在CSS中的使用 .demoIm ...
- Android开发 - ImageView加载Base64编码的图片
在我们开发应用的过程中,并不是所有情况下都请求图片的URL或者加载本地图片,有时我们需要加载Base64编码的图片.这种情况出现在服务端需要动态生成的图片,比如: 二维码 图形验证码 ... 这些应用 ...
- 把一个base64编码的图片绘制到canvas (canvas的图片在转成dataurl)
把一个base64编码的图片绘制到canvas 需要引入jquery. <canvas id="myCanvas" width="800" height= ...
- 问题:C#将base64转换成二进制图片;结果:c# Base64编码和图片的互相转换代码
c# Base64编码和图片的互相转换代码 Base64编码在Web方面有很多应用,譬如在URL.电子邮件方面.网上有很多相关的资源用于提供Base64编码和其他编码的转换,.Net Framewor ...
- 将base64编码转换图片
/// <summary> /// 二进制Base64编码转图片 /// </summary> /// <param name="bytes"> ...
- base64编码转图片
protected void Button1_Click(object sender, EventArgs e) { //strImg为base64编码的图片字符串 string strImg = & ...
随机推荐
- DB2创建库 数据恢复
例:数据库:PRODB2用户 :DB2ADMIN/DB2ADMIN备份库路径:D:/bank 一.恢复数据库1.启动数据库运行->db2cmd->db2Db2=>start db m ...
- <每日一题> Day6:HDU递推专题完结
原题链接 这是我自己Clone的专题,A,B题解昨天发过了 C:参考代码: /* 很容易我们可以手推出n = 1, 2, 3时的情况,我们假设前n - 1 列已经放好,方法有dp[n - 1]种,第n ...
- 6、 逻辑回归(Logistic Regression)
6.1 分类问题 在分类问题中,你要预测的变量
- Hystrix (容错,回退,降级,缓存)
Hystrix熔断机制就像家里的保险丝一样,若同时使用高功率的电器,就会烧坏电路,这时候保险丝自动断开就有效的保护了电路.而我们程序中也同样是这样.例如若此时数据库压力太大速度很慢,此时还有不断的请求 ...
- EF添加关联的提示问题:映射从第 260 行开始的片段时有问题:
一,EF添加关联的提示问题 严重性 代码 说明 项目 文件 行 禁止显示状态错误 错误 3004: 映射从第 260 行开始的片段时有问题:没有为 设置 T_xx_xxRelation 中的属性 T_ ...
- 实现单选框点击label标记中的文字也能选中
实例: <label for="man"> <input type="radio" value="男" name=&quo ...
- SQLServer死锁查询
--查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys ...
- 按字节读取txt文件缓存区大小设置多少比较好?
读取 txt 文件常规写法有逐行读取和按照字节缓存读取,那么按照字节缓存读取时,设置缓存区多大比较好呢?百度了一下,没发现有说这个问题的,自测了一把,以事实说话. 常规读取方法如下: // 字节流读取 ...
- [USACO]奶牛会展(背包)
[USACO]奶牛会展 题目背景 奶牛想证明它们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N 头奶牛进行 了面试,确定了每头奶牛的智商和情商. 题目描述 贝西有权选择让哪些奶牛参加展览. ...
- maven 常用插件 拷贝依赖 拷贝jar包 查看属性 环境变量
1 maven编译后希望将生产的jar包拷贝到指定目录 在pom中配置maven插件 maven-antrun-plugin <build > <plugins> <pl ...