Base64编码和图片转化
Base64编码和图片转化
package com.example;import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;import java.util.Base64;public class ImageBase64Tool {public static void main(String[] args) {String suffix = "jpg";//图片文件的后缀名。可以是jpg、gif,jpeg等System.out.println("#######################sun.misc.BASE64Decoder#######################");String imageFileName = "\\D:\\image\\test\\bg.jpg";String imageBinary = ImageBase64Tool.getImageBinary(imageFileName, suffix);System.out.println(imageBinary);String newFileName = "\\D:\\image\\test\\bg34.jpg";ImageBase64Tool.base64StringToImage(imageBinary, newFileName, suffix);System.out.println("###############################java Base64.Encoder###########################");String imageFileName1 = "\\D:\\image\\test\\404.jpeg";String imageBinary1 = ImageBase64Tool.generateBase64(imageFileName1);System.out.println(imageBinary1);String newFileName1 = "\\D:\\image\\test\\403.jpeg";ImageBase64Tool.base64StringToImage1(imageBinary1, newFileName1, suffix);}/*###########################sun.misc.BASE64Decoder #######################*//*** @param imageFileName 要生成二进制字节的图片所在位置* @param suffix 图片后缀*/public static String getImageBinary(String imageFileName, String suffix) {BASE64Encoder encoder = new BASE64Encoder();File imageFile = new File(imageFileName);try {BufferedImage bufferedImage = ImageIO.read(imageFile);ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(bufferedImage, suffix, baos);byte[] bytes = baos.toByteArray();String binaryString = encoder.encodeBuffer(bytes).trim();baos.close();return binaryString;} catch (IOException e) {e.printStackTrace();}return null;}/*** @param base64String 生成的base64的字符串* @param newImageFilePath 生成文件路径及文件* @param suffix 文件后缀*/public static void base64StringToImage(String base64String, String newImageFilePath, String suffix) {try {BASE64Decoder decoder = new BASE64Decoder();byte[] bytes = decoder.decodeBuffer(base64String);ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);File imageFile = new File(newImageFilePath);ImageIO.write(bufferedImage, suffix, imageFile);} catch (IOException e) {e.printStackTrace();}}/*#############################java Base64.Encoder #################################*//*** 自动将图片生成base64* @param imageFileName 需要生成字符串的图片路径*/public static String generateBase64(String imageFileName) {Base64.Encoder utilEncoder = Base64.getEncoder();byte[] imageBytes = null;FileInputStream fileInputStream = null; //文件输入流try {fileInputStream = new FileInputStream(imageFileName);try {imageBytes = new byte[fileInputStream.available()];fileInputStream.read(imageBytes);return utilEncoder.encodeToString(imageBytes);} catch (IOException e) {e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();}finally {if (null != fileInputStream){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}return null;}/*** 将base64的字符串转化为图片* @param base64String 生成的图片的base64的字符串* @param imageFileName 新的图片的位置* @param suffix 图片后缀*/public static void base64StringToImage1(String base64String, String imageFileName, String suffix) {try {Base64.Decoder utilDecoder = Base64.getDecoder();byte[] bytes = utilDecoder.decode(base64String);ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);File imageFile = new File(imageFileName);ImageIO.write(bufferedImage, suffix, imageFile);} catch (IOException e) {e.printStackTrace();}}
}
Base64编码和图片转化相关推荐
- BASE64编码的图片在网页中直接用
BASE64位转码有两种: 一种是图片转为Base64编码,这种编码是直接可以在页面通过<img src='base64编码'/>的方式显示 Base64 在CSS中的使用 .demoIm ...
- BASE64编码的图片在网页中的显示问题
BASE64位转码有两种: 一种是图片转为Base64编码,这种编码是直接可以在页面通过<img src='base64编码'/>的方式显示 Base64 在CSS中的使用 .demoIm ...
- Base64编码与图片互转
很多时候,第三方提供的图片地址为base64加密之后的,但是我们需要转换使用方便. package com.shucha.deveiface.biz.test;import java.io.FileI ...
- javascript将base64编码的图片数据转换为file并提交
2019独角兽企业重金招聘Python工程师标准>>> 直接提交base64编码图片数据,过大的话后台会出现转发错误问题. 一个不错的解决方式就是将base64编码的图片数据转换为B ...
- Android开发 - ImageView加载Base64编码的图片
在我们开发应用的过程中,并不是所有情况下都请求图片的URL或者加载本地图片,有时我们需要加载Base64编码的图片.这种情况出现在服务端需要动态生成的图片,比如: 二维码 图形验证码 - 这些应用场景 ...
- python如何将网络上的图片url和base64编码的图片保存在本地
1.python base64编码的图片保存到本地 import os import base64 sss ="""/9j/4AAQSkZJRgABAQEASABIAAD ...
- 在线还原base64编码的图片
base64编码还原图片 浏览器直接输入: data:image/png;base64,图像base64编码 该方法对图片大小有一定限制,编码太长则会显示不全 在线工具: https://tools. ...
- python中if brthon环境安装包_Python实现base64编码的图片保存到本地功能示例
本文实例讲述了Python实现base64编码的图片保存到本地功能.分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #!python3 import os import ...
- 浏览器是直接加载二进制图片更快还是加载base64编码的图片更快?
问题对人有帮助,内容完整,我也想知道答案0问题没有实际价值,缺少关键内容,没有改进余地 浏览器是直接加载二进制图片更快还是加载base64编码的图片更快?哪个效率更高? 例如: <img src ...
最新文章
- flannel源码分析--main
- 如何将表中的数据导出到电子表格中
- 一张图解释几个监督学习的定义
- ios 直播点赞_微信新版本更新:为视频号直播虚拟礼物,还提供连麦、美颜等功能...
- picACG本地缓存目录_手机上本地存储的哪些文件、文件夹不能删?
- Lecture 19 Shortest Paths III
- stm32高级定时器 基础知识
- 想Get热搜同款?GitHub开源神器让父亲重返18岁!
- JavaScript中语句与函数的执行辨析
- @property 各个属性作用【使用时最需注意strong/weak类型】【补充部分内存知识】...
- iPhone 13 系列不再齐「芯」,苹果为什么要造三款不同的 A15 处理器?
- C语言图书信息管理系统简单,线性表简单实现图书信息管理系统(C语言版)
- Python之 if-elif-else
- Oracle sql给一列赋值,简单的Oracle变量SQL赋值
- vscode——配置终端集成bash和cmd
- windows7安装openssh
- QQ for linux(ubuntu) 下载安装教程
- 配置管理工具SVN的使用
- 汉字拼音互相转换工具类
- 51nod 1521 一维战舰