最近项目需要将图片以base64编码,这里记录下相关的一些东西。

需要导入两个类:sun.misc.BASE64Encoder  

        sun.misc.BASE64Decoder

下面是相关java代码:

  public class Imagebase64 {
    static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    static BASE64Decoder decoder = new sun.misc.BASE64Decoder();

public static void main(String[] args) {
        System.out.println(getImageBinary()); // image to base64
        base64StringToImage(getImageBinary()); // base64 to image
    }

static String getImageBinary() {
        File f = new File("d://in.jpg");
        try {
            BufferedImage bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", baos);
            byte[] bytes = baos.toByteArray();

return encoder.encodeBuffer(bytes).trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

static void base64StringToImage(String base64String) {
        try {
            byte[] bytes1 = decoder.decodeBuffer(base64String);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            BufferedImage bi1 = ImageIO.read(bais);
            File f1 = new File("d://out.jpg");
            ImageIO.write(bi1, "jpg", f1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

图片和base64互转的更多相关文章

  1. 图片 和 base64 互转

    图片转base64 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]; UIImage *img = ...

  2. php 图片与base64互转

    header('Content-type:text/html;charset=utf-8'); //读取图片文件,转换成base64编码格式 $image_file = '1.png'; $image ...

  3. Base64编码 图片与base64编码互转

    package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import jav ...

  4. PHP 图片base64 互转

    <?php /* http://tool.css-js.com/base64.html 透明图片 <img src="data:image/jpg;base64,iVBORw0K ...

  5. C#和Python 图片和base64的互转

    C#实例代码: /// <summary> /// 图片转base64 /// </summary> /// <param name="bmp"> ...

  6. java 图片base64互转

    public class ImgBase64 { public static void main(String[] args) //测试 { String strImg = GetImageStr() ...

  7. 用 opencv和numpy进行图片和字符串互转,并保存至 json

    用 opencv和numpy进行图片和字符串互转,并保存至 json 转至 https://zhuanlan.zhihu.com/p/27349847 受 用 base64 进行图片和字符串互转,并保 ...

  8. HTML5之图片转base64编码

    之前在群里看到很多小哥哥小姐姐讨论关于图片base64互转的方法,刚好我之前用到的一个方法给大家分享一下. <!Doctype html><html> <head> ...

  9. .net C# 图片转Base64 Base64转图片

    //图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...

随机推荐

  1. 转:Spring系列之beanFactory与ApplicationContext

    原文地址:Spring系列之beanFactory与ApplicationContext 一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC ...

  2. LeetCode 476 Number Complement 解题报告

    题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...

  3. elastic search范围查询

    queryBuilder.must(QueryBuilders.rangeQuery("pt_longitude").from(minLongitude).to(maxLongit ...

  4. Python开发【笔记】:asyncio 定时器

    asyncio 定时器 实现: import asyncio class Timer: def __init__(self, timeout, callback): self._timeout = t ...

  5. PyChram快捷键

  6. 洛谷P2852 牛奶模式Milk Patterns [USACO06DEC] 字符串

    正解:SA/二分+哈希 解题报告: 传送门! umm像这种子串的问题已经算是比较套路的了,,,?就后缀的公共前缀这样儿的嘛QwQ 所以可以先求个SA 然后现在考虑怎么判断一个长度为d的子串出现了k次? ...

  7. BTM学习小记

    BTM的原理跟LDA很像,下面是该模型的概率图: 由该图可以看出来,与LDA的区别在于确定主题分布和词分布后相应地取两个词(而LDA只取一个,即类比常见的骰子说法:先投掷K面的骰子得到主题z,再根据相 ...

  8. mysql 设置 innodb_print_all_deadlocks=ON, 保存死锁日志

    Introduced 5.6.2 Command-Line Format --innodb-print-all-deadlocks=# System Variable Name innodb_prin ...

  9. fio 测试 磁盘I/O: ls -1 /sys/block/sda/queue/ | awk '{cmd="cat "i$0; print i$0; system(cmd) }' i=`pwd`'/'

    小型计算机系统接口(SCSI,Small Computer System Interface) SAS(Serial Attached SCSI,串列SCSI) SCSI 硬盘名称: sd[a-p] ...

  10. SpringBoot-Jar打包方式

    发布打包 Jar类型打包方式 1.使用mvn celan  package 打包 2.使用java –jar 包名 war类型打包方式 1.使用mvn celan package 打包 2.使用jav ...