java将图片输出base64位码显示

注意需要过滤:\r \n数据

jkd1.7的

import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;
  /**
     * 网络图片转换Base64的方法
     *
     * @param netImagePath     
     */
    public static String NetImageToBase64(String netImagePath) {
        final ByteArrayOutputStream data = new ByteArrayOutputStream();
        String strNetImageToBase64 = null;
        try {
            // 创建URL
            URL url = new URL(netImagePath);
            final byte[] by = new byte[1024];
            // 创建链接
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return strNetImageToBase64;
                //连接失败/链接失效/图片不存在
            }

            Thread thread =new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream is = conn.getInputStream();
                        // 将内容读取内存中
                        int len = -1;
                        while ((len = is.read(by)) != -1) {
                            data.write(by, 0, len);
                        }
                        // 关闭流
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
            // 对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            strNetImageToBase64 = encoder.encode(data.toByteArray());
            // 删除 \r\n
            strNetImageToBase64 = strNetImageToBase64.replaceAll("\n", "").replaceAll("\r", "");
            strNetImageToBase64 = "data:image/jpeg;base64," + strNetImageToBase64;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return  strNetImageToBase64;
    }

  

1.8java.util.Base64包

/**
     * 转换为base64格式
     *
     * @param image
     * @return
     */
    public static String encode2Base64Jpg(BufferedImage image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();// io流
        try {
            ImageIO.write(image, "jpg", baos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = baos.toByteArray();// 转换成字节
        Base64.Encoder encoder = Base64.getEncoder();
        String jpgBase64 = encoder.encodeToString(bytes).trim();// 转换成base64串
        jpgBase64 = jpgBase64.replaceAll("\n", "").replaceAll("\r", "");// 删除 \r\n

        return "data:image/jpeg;base64," + jpgBase64;
    }

  

其他本地图片处理

/**
     * 图片转化成base64字符串
     * @param imgPath
     * @return
     */
    public static String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        String imgFile = imgPath;// 待处理的图片
        InputStream in = null;
        byte[] data = null;
        String encode = null; // 返回Base64编码过的字节数组字符串
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        try {
            // 读取图片字节数组
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            encode = encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return encode;
    }

  

原文地址:https://www.cnblogs.com/achengmu/p/11813995.html

时间: 2024-11-03 17:50:40

java将图片输出base64位码显示的相关文章

java实现图片与base64字符串之间的转换

java实现图片与base64字符串之间的转换 package com.zxc.testjava.ant; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import sun.misc.BASE64Decoder; import sun.misc

JAVA 将图片转换为Base64编码

这里使用的jar包是commons-codec-1.10.jar; 示例代码 1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 7 import org.apache.commons.codec.binary.Base64;

Java实现图片倒影的源码

把做工程过程常用的一些代码段做个记录,下面代码是关于Java实现图片倒影的的代码,应该对大家有较大用处. public class ButtonImageActivity extends Activity { private ImageView image_btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.lay

cocos2d-x 3.X 接收图片 base64 转码显示

base64是一种常见的网络传输编码方式,可以对图片.文字等格式转化为二进制流.Cocos2d中自带base64码的转码.解码函数: int cocos2d::base64Encode (const unsigned char * in, unsigned int inLength, char ** out ) int cocos2d::base64Decode (const unsigned char * in, unsigned int inLength, unsigned char **

java中图片文件的传输及显示(Socket以及ServerSocket演示)

//客户端部分 package testSix; import java.awt.Graphics; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.util.Iterator; import javax.imageio.ImageIO; impo

Java对图片Base64转码--HTML对Base64解码

Java对图片Base64转码 package base64; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import sun.misc.BASE64Decod

Java对图片Base64转码--HTML对Base64解码 [ Java加强版 ]

Java对图片Base64编码 package base64; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Rand

java 使用qrcode生成二维码图片或者base64字符串

通过传入字符串,生成二维码图片或者base64格式字符串 1 public static String barcode2Base64(String msg) throws Exception{ 2 Qrcode x = new Qrcode(); 3 //N代表数字,A代表a-z,B代表其他字符 4 x.setQrcodeEncodeMode('B'); 5 //设置纠错等级 6 x.setQrcodeErrorCorrect('M'); 7 //设置版本号(1-40) 8 x.setQrcod

zxing生成二维码转base64 img直接显示 Image对象转Base64码(java)

public static String encodeToBase64(String content){ MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型 BitMatrix bitMatrix = null; try { bitMat