最近项目需要将图片以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. readonly enable

    <input type="text" id="UserName" style="width:20%;" disabled=" ...

  2. Copycat - AppendRequest

    对于Command,Configuration都要通过appendEntries的方式,把Entries同步给follower LeaderState.configure /** * Commits ...

  3. DBGridEh表尾显示合计 .....

    设置如下就可以了..... FooterRowCount  : 1 SumList--------Active:=true 双击 DBGridEh  加入所需要的列....然后在 需要合计的..... ...

  4. 《linux 用户管理》- useradd/userdel/usermod/groupadd/who/w

    一:概念 在 Linux 中,使用一个 32位整数 来记录每一个用户(USER ID 简单 UID),这意味着在 Linux 中,可以有 40亿 个不同的用户. 系统. 在 /etc/passwd  ...

  5. Django 之Redis配置

    目的:访问服务器频繁的读取数据库 ,会耗损服务器性能及降低用户体验,为此引入Redis 1,安装 redis(2.10.6兼容性更好) pip install redis 2,settings.py配 ...

  6. Javascript 面向对象编程(一):封装 作者:yuan一峰

    学习Javascript,最难的地方是什么? 我觉得,Object(对象)最难.因为Javascript的Object模型很独特,和其他语言都不一样,初学者不容易掌握. 下面就是我的学习笔记,希望对大 ...

  7. js中常用的offset client screen对象

    javascript中offsetWidth.clientWidth.width.scrollWidth.clientX.screenX.offsetX.pageX offsetWidth //返回元 ...

  8. bug:*** Collection <__NSArrayM: 0x1c444d440> was mutated while being enumerated.

    崩溃提示:Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <CAL ...

  9. C# cmd bcp 导出数据

    背景需求:应用系统间数据自动同步处理,要求高效无人工干预 技术实现:C#启动cmd,通过BCP命令传入必要参数,实现数据导出 /// <summary> /// cmd下,启动应用程序命令 ...

  10. python获取指定目录下特定格式的文件名

    之前一直用windows下的bat脚本获取一个目录下的指定格式的文件名,如下所示: dir *.jpg /b/s > train.set pause 十分简单,将这个bat文件放到你想要获取文件 ...